Answer to Question #328551 in C++ for naqib ullah

Question #328551

Write a program that creates and then reads a matrix of 4 rows and 4 columns of type int. while reading; the program should not accept values greater than 100. For any entered value greater than 100, the program should ask for input repeatedly. After reading all numbers, the system should find the largest number in the matrix and its location or index values. The program should print the largest number and its location (row and column).

1
Expert's answer
2022-04-14T06:16:12-0400
#include <iostream>
#include <vector>


using namespace std;


void main()
{
	const int N = 4, M = 4;
	vector<vector<int> >vec(N, vector < int>(M));
	cout << "Please, enter values for the matrix: \n";
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < M; j++)
		{
			int tmp;
			do
			{
				cout << "Please, enter value [" << i << "][" << j << "]: ";
				cin >> tmp;
				if (tmp > 100)
					cout << "Value should be less than 100!" << endl;
			} while (tmp > 100);
			vec[i][j] = tmp;
		}
	}
	cout << "\nEntered array: \n";
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < M; j++)
		{
			cout << vec[i][j]<<" ";
		}
		cout << endl;
	}
	int larg = vec[0][0];
	int row = 0, col = 0;
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j < M; j++)
		{
			if (vec[i][j] > larg)
			{
				larg = vec[i][j];
				row = i;
				col = j;
			}
		}
	}
	cout << "The largest number is " << larg
		<< "\nIt`s location in [" << row << "][" << col << "]";
}

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