#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;
}
Comments
Leave a comment