Question

Write C code to repeatedly ask the user for a number, then once the user enters...

Write C code to repeatedly ask the user for a number, then once the user enters 0, the code displays the min, max and average of all values entered.

To get proper credit, name the variables as listed below and follow the directions carefully. Do not use any break or similar type of statements.

To implement the above code, you will need to count the entries - name the counter variable num_count.

You will also need to use a variable, name it min_in so each time the user gives input, and the input is not 0, then compare min_in with the input, if the min_in is > input, then assign min_in to the input.

Similarly name another variable max_in, and compare it to the input, but assign max_in to the input only if max_in is < input.

To calculate the average, accumulate each input into a variable, total. Then after the loop the average is the total divided by the number of entries.

Once the loop is over, display the calculated values (min, max and the average) with a proper wording in three lines. The input is not restricted to integer value so declare the variables above with a proper type. Show the output using two decimal places, and align the three output values as in this sample output:

The minimum entry was: 2.30
The maximum entry was: 62.51
The average of all entries was: 52.81

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include<stdio.h>                                               //header files including
int main()
{
   int num_count=0;                                           //num_count variable for counting the entries
   float input_number,min_in,max_in,total;               //input_number,min_in,max_in,total variables
   printf("Enter a Number : ");      
   scanf("%f",&input_number);                       //taking input from user
   while(input_number!=0)                           //itearte the loop until the input number is equal to zero
       {
       if(num_count==0)                           //if num_count=0 means for first number make that as minimum and maximum and add it to total
           {
           min_in=input_number;               //assign input number as minimum
           max_in=input_number;               //assign input as maximum
           total=total+input_number;           //adding input number to total
           }
       else                                                   //else case
           {  
           if (min_in>input_number)               //checking the input is less than existing minimum value or not
               min_in=input_number;               //if yes store that value into min_in
           if (max_in<input_number)               //checking the input is greater than existing maximum value or not
               max_in=input_number;               //if yes store that value into max_in
           total=total+input_number;               //add input_number to total
           }
       num_count++;                           //input_number count will increase used for average calculation
       printf("Enter a Number : ");           //asking user to enter the number repeatedly
       scanf("%f",&input_number);
       }
   printf("The minimum entry was: %.2f\n",min_in);               //finally printing the minimum value of entries
   printf("The maximum entry was: %.2f\n",max_in);               //maximum value of entries
   printf("The average of all entries was: %.2f\n",(total/num_count));           //average value calculated by total/num_count all are rounded to two decimals
   return 0;
}

Code screenshots and output:

#include<stdio.h> int main() //header files including //num_count variable for counting the entries //input_number, min in,ma

Enter a Number : 2.50 Enter a Number : 9.897 Enter a Number : 17.89 Enter a Number : 62.50 Enter a Number : 98.08 Enter a Num

Note: if you have any queries please post a comment thanks a lot...always available to help you

Add a comment
Know the answer?
Add Answer to:
Write C code to repeatedly ask the user for a number, then once the user enters...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create two java programs. The first will ask the user to enter three integers. Once the...

    Create two java programs. The first will ask the user to enter three integers. Once the three integers are entered, ask the user to enter one of three functions. The “average” if they want the average of the three numbers entered, enter “min” if they want the minimum and “max” if they want the maximum. Display the answer. Ask the user if they want to calculate another function for the three numbers. If so, loop and ask what function they...

  • c++ QUESTION 9 Write a code that ask the user for the number n of decimal...

    c++ QUESTION 9 Write a code that ask the user for the number n of decimal to average. Then n times the code the user to enter all numbers that you save or accumulate each time. Finaly print to the console the average of the n values the user entered using the format: cout << " The average of the " << n << " number you entered is: " << averagedValue <<endl;

  • Python code Write a program that will ask the user to input two strings, assign the...

    Python code Write a program that will ask the user to input two strings, assign the strings to variables m and n If the first or second string is less than 10 characters, the program must output a warning message that the string is too short The program must join the strings m and n with the join function The output must be displayed. Please name the program and provide at least one code comment (10)

  • Exercise 1: Write a program which repeatedly reads numbers until the user enters "done". Once "done"...

    Exercise 1: Write a program which repeatedly reads numbers until the user enters "done". Once "done" is entered, print out the total, count, and average of the numbers. If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number. Enter a number: 4 Enter a number: 5 Enter a number: bad data Invalid input Enter a number: 7 Enter a number: done 16 3...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • ​Declare an array with 1000 elements of type int Then in a loop generate 1000 random numbers and assign one to each element in the array The random numbers should be between 1 and 50 Then, prompt the user to enter a number, store this number in a local

    Rules to follow are:Declare an array with 1000 elements of type intThen in a loop generate 1000 random numbers and assign one to each element in the arrayThe random numbers should be between 1 and 50do not use rand or srand, use code is providedThen, prompt the user to enter a number, store this number in a local variable, the number should be safety checked using the GetInteger() functionThen, iterate through the array and determine how many times the user's...

  • This program will ask the user to enter a number of players, then ask the user...

    This program will ask the user to enter a number of players, then ask the user for each player's name and score. Once all of the players have been entered, the program will display a bar chart with each of their scores scaled as a percentage of the maximum score entered. See below for details on exactly how this should work - an example transcript of how the program should work is shown below (remember that values entered by the...

  • MUST BE WRITTEN IN C++ All user input values will be entered by the user from...

    MUST BE WRITTEN IN C++ All user input values will be entered by the user from prompts in the main program. YOU MAY NOT USE GLOBAL VARIABLES in place of sending and returning data to and from the functions. Create a main program to call these 3 functions (5) first function will be a void function it will called from main and will display your name and the assignment, declare constants and use them inside the function; no variables will...

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

  • Write a C# or C++ code to do the following: -Ask user for name of file1...

    Write a C# or C++ code to do the following: -Ask user for name of file1 and file2 (These files should contains numbers representing rows and cols in a table- see below) -Read the content of the files into 2 arrays or something -Ask user which column to compare in array1 and which col in array2 to join the two tables. Loop through arrays and if the element in specified col of array1 matches the element in specified col of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT