Answer to Question #100800 in C++ for sabra

Question #100800
8/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
2020-01-02T11:24:22-0500
#include <iostream>
#include <string>
using namespace std;

int main()
{
    char array[7][256];
    int number;
    cout << "Specify seven numbers\n";
    for (int i = 0; i < 7; i++)
    {
        cin >> number;
        if (number < 0)
            number = -number;
        string numerical = to_string(number); // C++11 feature
        for (string::size_type j = 0; j < numerical.size(); j++)
            array[i][j] = numerical[j];
        array[i][numerical.size()] = '\0';
    }

    for (int i = 0; i < 7; i++)
    {
        char *ch = array[i];
        while (*ch != '\0')
        {
            if (*ch == '9')
                *ch = '0';
            ++ch;
        }
    }

    char *repl = 0;
    for (int i = 0; i < 7; i++)
    {
        for (int j = 0; array[i][j]; j++)
        {
            if ((i + j) % 2)
                array[i][j] = *repl;
            else
                repl = &array[i][j];
        }
    }

    for (int i = 0; i < 7; i++)
    {
        for (int j = 0; array[i][j]; j++)
        {
            cout << array[i][j] << '\t';
        }
        cout << '\n';
    }

    return 0;
}

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