Answer to Question #100715 in C++ for hajer

Question #100715

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
2019-12-25T10:25:46-0500


#include <iostream>

int row2num(int *row) {
    int num = 0;
    for (int j = 0; row[j] != -1; j++) {
        num = num * 10 + row[j];
    }
    return num;
}

int main() {
    const int MAXN = 25;
    int N = 0;
    std::cout << "Enter N (1-" << MAXN << "): ";
    std::cin >> N;
    while (!(N > 0 && N <= MAXN)) {
        std::cout << "Try again: ";
        std::cin >> N;
    }

    // obviously, a 32-bit or 64-bit integer has less than 26 digits
    const int MAXD = 26;
    int numbers[MAXN][MAXD + 1], temp1, width, temp2;
    for (int i = 0; i < N; i++) {
        std::cout << "Enter " << i + 1 << "-th number (nonnegative): ";
        temp1 = -1;
        std::cin >> temp1;
        while (temp1 < 0) {
            std::cout << "Try again: ";
            std::cin >> temp1;
        }

        temp2 = temp1;
        for (width = 0; temp1 != 0; width++) {
            temp1 /= 10;
        }
        if (temp2 == 0)
            width = 1;

        numbers[i][width] = -1;
        for (int j = 0; j < width; j++) {
            numbers[i][width - 1 - j] = temp2 % 10;
            temp2 /= 10;
        }
    }

    int numbers2[MAXN][MAXD + 1];
    for (int i = 0, j; i < N; i++) {
        for (j = 0; numbers[i][j] != -1; j++) {
            if (numbers[i][j] == 9)
                numbers2[i][j] = 0;
            else
                numbers2[i][j] = numbers[i][j];
        }
        numbers2[i][j] = -1;
    }

    int numbers3[MAXN][MAXD + 1];
    if (N % 2 != 0) {
        for (int j = 0; j <= MAXD; j++)
            numbers3[N - 1][j] = numbers2[N - 1][j];
    }
    for (int i = 1; i < N; i += 2) {
        for (int j = 0; j <= MAXD; j++) {
            numbers3[i][j] = numbers2[i - 1][j];
            numbers3[i - 1][j] = numbers2[i][j];
        }
    }

    for (int i = 0; i < N; i++) {
        std::cout << i + 1 << " | ";
        std::cout.width(MAXD);
        std::cout << row2num(reinterpret_cast<int *>(numbers + i));
        std::cout << " | ";
        std::cout.width(MAXD);
        std::cout << row2num(reinterpret_cast<int *>(numbers2 + i));
        std::cout << " | ";
        std::cout.width(MAXD);
        std::cout << row2num(reinterpret_cast<int *>(numbers3 + i));
        std::cout << std::endl;
    }
}

/*
Enter N (1-25): 0
Try again: 26
Try again: 5
Enter 1-th number (nonnegative): -1
Try again: 123
Enter 2-th number (nonnegative): 0
Enter 3-th number (nonnegative): 1987
Enter 4-th number (nonnegative): 23456
Enter 5-th number (nonnegative): 999912
1 |                        123 |                        123 |                          0
2 |                          0 |                          0 |                        123
3 |                       1987 |                       1087 |                      23456
4 |                      23456 |                      23456 |                       1087
5 |                     999912 |                         12 |                         12
*/

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