Question

In: Computer Science

Write a C program that prompts the user to enter three sets of five double numbers...

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

Solutions

Expert Solution

"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;
}



Related Solutions

Part 1 Write a program that prompts the user to enter three sets of five double...
Part 1 Write a program that prompts the user to enter three sets of five double numbers each. These numbers represent the five grades that each of three students has received. After prompting for the grades, the program: stores the data in a previously defined 3◊5 double array computes the average of each set of 5 grades after acquiring the grades for that one student determines the maximum and minimum values of each set of 5 grades acquiring the grades...
write a c++ program that prompts a user to enter 10 numbers. this program should read...
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
Write a C# program that prompts the user to enter in three values. The first indicates...
Write a C# program that prompts the user to enter in three values. The first indicates a starting (whole number positive) value, the second an ending value and the third an increase value. output a table of squares and square roots, every increase value from the start to the end value. For instance if the values were: start 3, end 20, and increase 4. the table would consist of 3,7,11,15,19 and their squares and square roots. Please include all code...
Write a c++ code that prompts the user to enter three float numbers and print them...
Write a c++ code that prompts the user to enter three float numbers and print them in acsending order
Write a test program that prompts the user to enter a sequence of numbers ending with...
Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes this method to return the largest number in the input. Use inheritance and polymorphism approach. in java please
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
You will write a program that prompts the user to enter a 7-digit phone numbers, and...
You will write a program that prompts the user to enter a 7-digit phone numbers, and finds the 3- and 4-letter words that map to the phone number, according to the restrictions outlined earlier. A sample run: unixlab% java MapNumbers Enter name of dictionary file: words10683 Enter a test word (3 letters): cat Test word maps to 228 Enter telephone number (7 digits, no 0's or 1's, negative to quit): 2282273 Options for first 3 digits: act cat bat Options...
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
Write a program in c++ using only if statements that prompts the user to enter an...
Write a program in c++ using only if statements that prompts the user to enter an integer for today’s day of the week (Sunday is 0, Monday is 1 …., and Saturday is 6) then displays today. Also, prompt the user to enter the number of days after today for a future day and display the future day of the week. The future day can be computed as follows: (today + number of days after today) % 7 Sample run...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT