Answer to Question #83481 in C++ for 22

Question #83481
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-05T06:55:10-0500
#include <iostream>
#include <string>

using namespace std;

int** createArray(int, int);
void fillArray(int**, int, int);
void printArray(int**, int, int);
int replaceDigits(int, char, char);
void replaceOddByEven(int**, int, int);
void foundNextEven(int, int, int&, int&);
void clearMemory(int**, int);

int main()
{
    int size_1D;
    int size_2D;

    int **array;

    cout << "---This is a program representing work with a two-dimensional array---\n\n";
    cout << "Enter the size of the first dimension: ";
    cin >> size_1D;
    cout << "Enter the size of the second dimension: ";
    cin >> size_2D;

    array = createArray(size_1D, size_2D);

    fillArray(array, size_1D, size_2D);

    cout << "\nOriginal array:";
    printArray(array, size_1D, size_2D);

    cout << "Replacing numbers ...\n";
    for (int i = 0; i < size_1D; i++)
    {
        for (int j = 0; j < size_2D; j++)
        {
            array[i][j] = replaceDigits(array[i][j], '9', '0');
        }
    }

    cout << "\nModified array:";
    printArray(array, size_1D, size_2D);

    cout << "Replace odd by even...\n";
    replaceOddByEven(array, size_1D, size_2D);

    cout << "\nModified array:";
    printArray(array, size_1D, size_2D);

    clearMemory(array, size_1D);

    return 0;
}

int** createArray(int first_d, int second_d)
{
    int **array = new int* [first_d];
    for(int i = 0; i < first_d; i++)
    {
        array[i] = new int[second_d];
    }
    return array;
}
void fillArray(int **array, int first_d, int second_d)
{
    cout << endl;
    for (int i = 0; i < first_d; i++)
    {
        cout << "Enter the values of the " << (i + 1) << " line:\n";
        for (int j = 0; j < second_d; j++)
        {
            cin >> array[i][j];
        }
    }
}
void printArray(int **array, int first_d, int second_d)
{
    cout << endl;
    for (int i = 0; i < first_d; i++)
    {
        for (int j = 0; j < second_d; j++)
        {
            cout << array[i][j] << "\t";
        }
        cout << "\n";
    }
    cout << endl;
}
int replaceDigits(int number, char to_replace, char replaced_by)
{
    string digits = to_string(number);
    string resultDigits;
    for (int i = 0; i < digits.length(); i++)
    {
        char c = digits.at(i);
        if (c != to_replace)
        {
            resultDigits.push_back(c);
        }
        else
            resultDigits.push_back(replaced_by);
    }

    return stoi(resultDigits);
}
void replaceOddByEven(int **array, int first_d, int second_d)
{
    int even_x = 0, even_y = 0;

    for (int i = 0; i < first_d; i++)
    {
        for (int j = 0; j < second_d; j++)
        {
            if ((i + j) % 2 == 1)
            {
                foundNextEven(first_d, second_d, even_x, even_y);
                array[i][j] = array[even_x][even_y];
                if (even_y == second_d - 1)
                {
                    even_x++;
                    even_y = 0;
                }
                else
                    even_y++;
            }
        }
    }
}
void foundNextEven(int first_d, int second_d, int &even_x, int &even_y)
{
    for (; even_x < first_d; even_x++)
    {
        for (; even_y < second_d; even_y++)
        {
            if ((even_x + even_y) % 2 == 0)
            {
                return;
            }
        }
        if (even_y == second_d) even_y = 0;
    }
}
void clearMemory(int **array, int first_d)
{
    for (int i = 0; i < first_d; i++)
    {
        delete[] array[i];
    }
    delete[] array;
}

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