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).
#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 << "]";
}
Comments
Leave a comment