# Answer to Question #71581 in C++ for m

Question #71581

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.

using while, for and array if possible

using while, for and array if possible

Expert's answer

#include <iostream>

#include <stdlib.h>

#include <string>

using namespace std;

void print(string** Matrix, int n, int m)

{

for(int i = 0; i < n; ++i)

{

for(int j = 0; j < m; ++j)

cout << Matrix[i][j] << " ";

cout << "\n";

}

}

void ninesToZiros(string** Matrix, int n, int m)

{

int k = 0, strn = 0;

for(int i = 0; i < n; ++i)

{

for(int j = 0; j < m; ++j)

{

k = 0;

strn = Matrix[i][j].length();

while(k < strn)

{

if(Matrix[i][j][k] == '9')

{

if(k == 0 && strn != 1)

{

Matrix[i][j].erase(0, 1);

--strn;

--k;

}

else

{

Matrix[i][j][k] = '0';

}

}

++k;

}

}

}

}

void oddByEven(string** Matrix, int n, int m)

{

int oddi = -1, oddj = -1, eveni = -1, evenj = -1;

if(n != 1 || m != 1)

for(int i = 0; i < n; ++i)

{

for(int j = 0; j < m; ++j)

{

if(i % 2 != 0 && j % 2 != 0)

{

if(eveni != -1)

{

Matrix[i][j] = Matrix[eveni][evenj];

}

else{

oddi = i;

oddj = j;

}

}

else if(i % 2 == 0 && j % 2 == 0)

{

if(oddi != -1)

{

Matrix[oddi][oddj] = Matrix[i][j];

}

else{

eveni = i;

evenj = j;

}

}

}

}

}

int main()

{

string **Matrix;

int n, m;

cout << "Please enter a row count: "; cin >> n;

cout << "Please enter a column count: "; cin >> m;

Matrix = new string * [n];

for(int i = 0; i < n; ++i)

{

Matrix[i] = new string [m];

for(int j = 0; j < m; ++j)

{

cout << "Please enter the number[" << (i+1) << "][" << (j+1) <<"]: "; cin >> Matrix[i][j];

if(!atoi(Matrix[i][j].c_str()))

{

cout << "Invalid input! Please enter an integer number\n";

--j;

}

}

}

cout << "Original matrix:\n";

print(Matrix, n, m);

ninesToZiros(Matrix, n, m);

cout << "Matrix with 0 instead 9:\n";

print(Matrix, n, m);

oddByEven(Matrix, n, m);

cout << "Matrix with even summation indices instead odd:\n";

print(Matrix, n, m);

return 0;

}

#include <stdlib.h>

#include <string>

using namespace std;

void print(string** Matrix, int n, int m)

{

for(int i = 0; i < n; ++i)

{

for(int j = 0; j < m; ++j)

cout << Matrix[i][j] << " ";

cout << "\n";

}

}

void ninesToZiros(string** Matrix, int n, int m)

{

int k = 0, strn = 0;

for(int i = 0; i < n; ++i)

{

for(int j = 0; j < m; ++j)

{

k = 0;

strn = Matrix[i][j].length();

while(k < strn)

{

if(Matrix[i][j][k] == '9')

{

if(k == 0 && strn != 1)

{

Matrix[i][j].erase(0, 1);

--strn;

--k;

}

else

{

Matrix[i][j][k] = '0';

}

}

++k;

}

}

}

}

void oddByEven(string** Matrix, int n, int m)

{

int oddi = -1, oddj = -1, eveni = -1, evenj = -1;

if(n != 1 || m != 1)

for(int i = 0; i < n; ++i)

{

for(int j = 0; j < m; ++j)

{

if(i % 2 != 0 && j % 2 != 0)

{

if(eveni != -1)

{

Matrix[i][j] = Matrix[eveni][evenj];

}

else{

oddi = i;

oddj = j;

}

}

else if(i % 2 == 0 && j % 2 == 0)

{

if(oddi != -1)

{

Matrix[oddi][oddj] = Matrix[i][j];

}

else{

eveni = i;

evenj = j;

}

}

}

}

}

int main()

{

string **Matrix;

int n, m;

cout << "Please enter a row count: "; cin >> n;

cout << "Please enter a column count: "; cin >> m;

Matrix = new string * [n];

for(int i = 0; i < n; ++i)

{

Matrix[i] = new string [m];

for(int j = 0; j < m; ++j)

{

cout << "Please enter the number[" << (i+1) << "][" << (j+1) <<"]: "; cin >> Matrix[i][j];

if(!atoi(Matrix[i][j].c_str()))

{

cout << "Invalid input! Please enter an integer number\n";

--j;

}

}

}

cout << "Original matrix:\n";

print(Matrix, n, m);

ninesToZiros(Matrix, n, m);

cout << "Matrix with 0 instead 9:\n";

print(Matrix, n, m);

oddByEven(Matrix, n, m);

cout << "Matrix with even summation indices instead odd:\n";

print(Matrix, n, m);

return 0;

}

Need a fast expert's response?

Submit orderand get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

## Comments

## Leave a comment