Answer to Question #199612 in C++ for Shuvo

Question #199612

Write a C++ program for performing the following tasks with an array declared as

 int values[ROWS][COLUMNS];

where ROWS = 4 and COLUMNS = 5. Now perform the following tasks.

• Fill entries with a random number in the range 1 - 1000 inclusive. Remember to use rand() function, and the smallest value is 1 and the largest value is 1000.

• Compute the sum of all elements.

• Compute and print the average of all elements.

• Find and print the largest element in the array.

• Find and print the smallest element in the array

• Print the array elements in a table form with each line representing a row. Uset setw() manipulator with a width of 5 for each column. Remember, you are printing five columns in each row.


1
Expert's answer
2021-05-27T19:09:02-0400
#include <iostream>
#include<iomanip>
using namespace std;
int main()
{
    int ROWS=4;
    int COLUMNS=5;
    int values[ROWS][COLUMNS];
    int sum=0;
    for(int i =0;i<ROWS;i++){
        for(int j =0;j<COLUMNS;j++){
        values[i][j]=1+(rand()%1000);
        sum=sum+values[i][j];
        cout <<setw(5)<< values[i][j];
        }
        cout<<endl;
    }
    int min=values[0][0];
    for(int i =0;i<ROWS;i++){
        for(int j =0;j<COLUMNS;j++){
            if (values[i][j]<min){
                min=values[i][j];
            }
                
        }
    }
    
    int max=values[0][0];
    for(int i =0;i<ROWS;i++){
        for(int j =0;j<COLUMNS;j++){
            if (values[i][j]>max){
                max=values[i][j];
            }
                
        }
    }
    cout<<"Sum is: "<<sum<<endl;
    cout<<"Average is: "<<sum/(ROWS*COLUMNS)<<endl;
    cout<<"Largest element is: "<<max<<endl;
    cout<<"Smallest element is: "<<min<<endl;
    
    return 0;
}

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