In: Computer Science
Write a C 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.
When using for loops, please write them like this because this is the only way my compiler takes them.
int i = 0;
for( i < 6; i++)
I have to declare outside of for loop for some reason
"for (initial value; condition; incrementation or decrementation ) " this is the syntax for forloop so we can't decalre initial value outside forloop use while loop instead. I have tested in codechef using custom input
#include <stdio.h>
//defining functions
double average (double num[]);
double averageofall (double num[3][5]);
double largestvalue (double num[3][5]);
void main ()
{
double num[3][5];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 5; j++){
printf("Enter value for set[%d][%d]: ", i+1, j+1);
scanf (" %lf ", &num[i][j]);
}
}
//average of each set
for (int i = 0; i < 3; i++){
printf( "\n average of set %d is %.2lf ",i+1,average(num[i])) ; // .2lf for 2 digits after the decimal point
}
//average of all values
printf("\n average of all values is %.2lf",averageofall(num));
// largest of all values
printf("\n largest of all values is %.2lf",largestvalue(num));
}
double average (double num[])
{
double sum = 0;
for (int i = 0; i < 5; i++)
{
sum = sum + num[i];
}
return sum/5;
}
double averageofall(double num[3][5])
{
double sum = 0;
for (int i = 0; i < 3; i++)
{
for(int j=0 ;j<5;j++){
sum = sum + num[i][j];
}
}
return sum/15;
}
double largestvalue (double num[3][5])
{
double max = 0;
for (int i = 0; i < 3; i++)
{
for(int j=0 ;j<5;j++){
if(num[i][j] > max){
max = num[i][j];
}
}
}
return max;
}
if you like to use while loop here is the code
#include <stdio.h>
//defining functions
double average (double num[]);
double averageofall (double num[3][5]);
double largestvalue (double num[3][5]);
void main ()
{
double num[3][5];
int i,j =0;
while (i < 3)
{
while (j < 5){
printf("Enter value for set[%d][%d]: ", i+1, j+1);
scanf (" %lf ", &num[i][j]);
j++;
}
j=0;
i++;
}
//average of each set
i = 0 ;
while (i < 3){
printf( "\n average of set %d is %.2lf ",i+1,average(num[i])) ; // .2lf for 2 digits after the decimal point
i++;
}
//average of all values
printf("\n average of all values is %.2lf",averageofall(num));
// largest of all values
printf("\n largest of all values is %.2lf",largestvalue(num));
}
double average (double num[])
{
double sum = 0;
int i = 0;
while (i < 5)
{
sum = sum + num[i];
i++;
}
return sum/5;
}
double averageofall(double num[3][5])
{
double sum = 0;
int i,j = 0 ;
while ( i < 3)
{
while(j<5){
sum = sum + num[i][j];
j++;
}
j=0;
i++;
}
return sum/15;
}
double largestvalue (double num[3][5])
{
double max = 0;
int i,j = 0 ;
while (i < 3)
{
while(j<5){
if(num[i][j] > max){
max = num[i][j];
}
j++;
}
j=0;
i++;
}
return max;
}