In: Computer Science
Write a program that prompts the user to enter three sets of five double numbers each. (You may assume the user responds correctly and doesn’t enter non-numeric data.)
The program should accomplish all of the following:
a. Store the information in a 3×5 array.
b. Compute the average of each set of five values.
c. Compute the average of all the values.
d. Determine the largest value of the 15 values.
e. Report the results.
Each major task should be handled by a separate function using the traditional C approach to handling arrays.
Accomplish task “b” by using a function that computes and returns the average of a one-dimensional array; use a loop to call this function three times.
The other tasks should take the entire array as an argument, and the functions performing tasks “c” and “d” should return the answer to the calling program.
Must be done in C for DEV C++
Thanks for the question.
Here is the completed code for this problem. Comments are included,
go through it, learn how things work and let me know if you have
any doubts or if you need anything to change.
If you are satisfied with the solution, please rate the answer. Thanks!
===========================================================================
#include<iostream>
#include<iomanip>
using namespace std;
//computes and returns the average of a one-dimensional
array;
double averageRow(double arr[], int size){
double total = 0;
for(int i=0; i<size; i++) total +=arr[i];
return total/size;
}
//c. Compute the average of all the values.
double averageAll(double arr[][5], int rows,int cols){
double total = 0;
for(int row=0;row<rows;row++){
for(int
col=0;col<cols;col++){
total +=
arr[row][col];
}
}
return total/(rows*cols);
}
//d. Determine the largest value of the 15 values.
double largest(double arr[][5], int rows,int cols){
double max = arr[0][0];
for(int row=0;row<rows;row++){
for(int
col=0;col<cols;col++){
if(max<arr[row][col])
max =
arr[row][col];
}
}
return max;
}
int main(){
//a. Store the information in a 3×5 array.
double matrix[3][5];
for(int row=0; row<3; row++){
cout<<"Set
#"<<row+1<<endl;
for(int col=0; col<5;
col++){
cout<<"Enter number #"<<col+1<<": ";
cin >>
matrix[row][col];
}
}
//b. Compute the average of each set of five
values.
for(int row=0; row<3; row++){
cout<<"\nAverage of
set#"<<row+1<<": "
<<averageRow(matrix[row],5)<<endl;
}
//c. Compute the average of all the values.
cout<<"\nAverage of all values:
"<<averageAll(matrix,3,5)<<endl;
// d. Determine the largest value of the 15
values.
cout<<"\nLargest value of the 15 values is:
"<<largest(matrix,3,5)<<endl;
return 0;
}