Answer to Question #100832 in C++ for zahra

Question #100832
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. solve with do while
1
Expert's answer
2020-01-03T03:10:54-0500
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main() {
    int **numbers = 0;
    int count = 0, size = 0;

    {
        int number;
        ostringstream buf;
        string digits;
        do {
            cout << "Enter another number (or < 0 to stop): ";
            cin >> number;
            if (number >= 0) {
                if (count >= size) {
                    int size2 = (size == 0) ? 2 : size * 2;
                    int **numbers2 = new int *[size2];
                    if (count > 0) {
                        int i = 0;
                        do {
                            numbers2[i] = numbers[i];
                            i++;
                        } while (i < count);
                        delete[] numbers;
                    }
                    size = size2;
                    numbers = numbers2;
                }
                buf.clear();
                buf.str("");
                buf << number;
                digits = buf.str();
                int subsize = digits.size() + 1;
                int i = count, j = 1, j2 = 0;
                numbers[i] = new int[subsize];
                numbers[i][0] = digits.size();
                do {
                    numbers[i][j] = digits[j2] - '0';
                    j++;
                    j2++;
                } while (j < subsize);
                count++;
            }
        } while (number >= 0);
    }

    {
        int i = 0;
        do {
            int j = 1;
            int subsize = numbers[i][0] + 1;
            do {
                if (numbers[i][j] == 9) {
                    numbers[i][j] = 0;
                }
            } while (++j < subsize);
        } while (++i < count);
    }

    {
        int i = 0;
        bool swapped;
        do {
            swapped = false;
            if (i + 2 <= count) {
                int *temp = numbers[i];
                numbers[i] = numbers[i + 1];
                numbers[i + 1] = temp;
                swapped = true;
            }
            i += 2;
        } while (swapped);
    }

    {
        int i = 0;
        do {
            int j = 1;
            int subsize = numbers[i][0] + 1;
            do {
                cout << numbers[i][j];
            } while (++j < subsize);
            cout << endl;
            delete[] numbers[i];
        } while (++i < count);
        delete[] numbers;
    }
}

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