Consider a square matrix A of size nxn and an integer x which is an element of A. find the row number R and column number C of X in A, and calculate the sum R and C. If the sum is even, find the sum of the digits of all even numbers in the matrix, and if the sum is odd then find the sum of digits of all odd numbers in matrix
#include<iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
unsigned int N;
cout << "Please, enter an order of matrices: ";
cin >> N;
int **m = new int*[N];
for (int i = 0; i<N; i++)
m[i] = new int[N];
srand(static_cast<unsigned int>(time(0)));
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
m[i][j] = rand() % 100;
}
}
cout << "Matrix is \n";
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cout<<m[i][j]<<" ";
}
cout << endl;
}
int value;
cout << "Please, enter a value of matrix:";
cin >> value;
int row, col;
bool ext = false;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (m[i][j] == value)
{
col = j;
ext = true;
break;
}
}
row = i;
if (ext == true)
break;
}
cout << "\nrow of value is " << row << " col is " << col;
double sum=0;
if ((row + col) % 2 == 0)
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (m[i][j] % 2 == 0)
{
sum += m[i][j];
}
}
}
cout << "\nSum of all even elements is " << sum;
}
else
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
if (m[i][j] % 2 != 0)
{
sum += m[i][j];
}
}
}
cout << "\nSum of all odd elements is " << sum;
}
}
Comments
Leave a comment