Answer to Question #294216 in C++ for Ran

Question #294216

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


1
Expert's answer
2022-02-05T14:56:30-0500
#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;
	}
}	

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