In: Computer Science
[C++ Coding question]
write a program which inputs data to a two-dimensional array:
- Input number of rows r aand number of columns c
- Create a two-dimensional array of integers int numbers[r][c]
-input data for each element of numbers
Your program must compute and display the largest number is each row and column of the number array
ANSWER: Here I am giving you the code and output if you have any problem then you can comment or like my solution.
CODE:
#include <iostream>
using namespace std;
int main()
{
int r,c;
cout<<"Input row and column for the matrix :";
cin>>r>>c;
int A[r+1][c+1] ;
A[r+1][c+1] = {0};
for(int i=0;i<r+1;i++)
for(int j=0;j<c+1;j++)
A[i][j]=0;
for (int x = 0; x < r; ++x)// Entering the values in the
array
{
for (int y = 0; y < c; ++y)
{
cout << "Enter in value for row " << x << ",
column " << y << ".\n";
cin >> A[x][y];
}
}
cout << "Input:" << endl;// Printing the you
array
for (int x = 0; x < r; ++x)
{
for (int y = 0; y < c; ++y)
{
cout << A[x][y] << "\t";
}
cout << "\n";
}
// Finds the max down each column in the array
for (int x = 0; x < r; ++x)
{
for (int y = 0; y < c; ++y)
{
if (A[x][c] < A[x][y])
A[x][c] = A[x][y];
}
}
// Finds the min across the array (along row)
for (int y = 0; y < r; ++y)
{
for (int x = 0; x < c; ++x)
{
// Assign the min to a value in that row.
A[r][y] = A[1][y];
if (A[r][y] > A[x][y])
A[r][y] = A[x][y];
}
}
for(int i=0;i<c;i++)
{
cout<<"\nMaximum in row "<< i+1<<":
"<<A[i][c];
}
for(int i=0;i<r;i++)
cout<<"\nMaximum in column "<<i+1<<":
"<<A[r][i];
return 0;
}
OUTPUT: