Answer to Question #83480 in C++ for 22

Question #83480
Write a program that asks the user to enter integer numbers and store the numbers in a two
dimensional array, then replace any 9 digit by 0 digit. After that replace the numbers with the odd
summations indices by the even summation indices.
1
Expert's answer
2018-12-10T10:25:10-0500
#include <iostream>
#include <limits>
#include <string>

void enter2dArray()
{
    int N, M;
    std::cout << "N: ";
    std::cin >> N;
    if (std::cin.fail() || N < 1) {
        std::cin.clear();
        N = 1;
    }
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cout << "M: ";
    std::cin >> M;
    if (std::cin.fail() || N < 1 || M < 1) {
        std::cin.clear();
        M = 1;
    }
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cout << "Sequence of " << N * M << " integers: ";
    int **arr = new int *[N];
    for (int i = 0; i < N; ++i) {
        arr[i] = new int[M];
        for (int j = 0; j < M; ++j) {
            std::cin >> arr[i][j];
            if (std::cin.fail()) {
                std::cin.clear();
                std::cin.ignore(1, '\n');
                arr[i][j] = 0;
            }
        }
    }
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    std::cout << "2D Array:\n";
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < M; ++j) {
            std::cout << arr[i][j] << " ";
            if (j + 1 < M)
                std::cout << " ";
            else
                std::cout << std::endl;
        }
    }

    for (int i = 0; i < N; ++i)
        delete[] arr[i];
    delete[] arr;
}

void replaceDigits(unsigned a = 9, unsigned b = 0)
{
    int num;
    std::cout << "Num = ";
    std::cin >> num;
    if (std::cin.fail()) {
        num = -919939189; // default value
        std::cin.clear();
    }
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    std::string buf = std::to_string(num);
    const std::string sa = std::to_string(a);
    const std::string sb = std::to_string(b);
    std::cout << "Num1 = " << num << std::endl;
    std::cout << "Str1 = " << buf << std::endl;

    for (std::string::size_type n = buf.find(sa); n != std::string::npos;
         n = buf.find(sa, n))
        buf.replace(n, sa.size(), sb);
    num = std::stoi(buf);
    std::cout << "Str2 = " << buf << std::endl; // if num2 is not valid
    std::cout << "Num2 = " << num << std::endl;
}

void replaceElems()
{
    int N;
    std::cout << "N: ";
    std::cin >> N;
    if (std::cin.fail() || N < 10) {
        std::cin.clear();
        N = 10;
    }
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cout << "Series of " << N << " integers: ";
    int *row = new int[N];
    for (int i = 0; i < N; ++i) {
        std::cin >> row[i];
        if (std::cin.fail()) {
            std::cin.clear();
            std::cin.ignore(1, '\n');
            row[i] = -i;
        }
    }
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    std::cout << "New series:";
    for (int i = 0; i < N; ++i) {
        if (i % 2 == 1)
            row[i] = row[i - 1];
        std::cout << " " << row[i];
    }
    std::cout << std::endl;

    delete[] row;
}

int main()
{
    enter2dArray();
    std::cout << std::endl;
    replaceDigits();
    std::cout << std::endl;
    replaceElems();
    std::cout << std::endl;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS