Question

C++ PROGRAMING Write a program that reads numbers from the input stream until a zero is...

C++ PROGRAMING

Write a program that reads numbers from the input stream until a zero is encountered. Display the average (rounded down to an integer), the maximum number and the minimum number. Assume that numbers entered will be between 1 and 1000. For example:

Enter a number: 33

Enter a number: 88

Enter a number: 77

Enter a number: 22

Enter a number: 66

Enter a number: 55

Enter a number: 0

Average: 56

Max: 88

Min: 22

Another example:

Enter a number: 0

No data, so no answers!

Run it 5 times with different data, including the two above examples.

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

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{

//Declaring variables.
   int value,sum=0,average,count=0,min=0,max=0;
  
   //Getting the number from the user.
   cout<<"Enter a Number :";
   cin>>value;
  
   //Assinging the value to min and max variables
   min=value;
   max=value;
  
   /* Checking whether the value is zero or not
   * If zero Display the message and exit the program.
   */
   if(value!=0)
   {
       do
       {
           /* For every valid input(Entered Number other than zero)
           * increment the count value by 1
           */
           count++;
          
           //Calculating the sum
           sum+=value;
          
           //Getting the number entered by the user
           cout<<"Enter a Number :";
           cin>>value;
          
           //calculating the maximum and minimum values
           if(value!=0)
           {
           if(value>max)
       max=value;
       if(value<min)
       min=value;
           }
                  
       }while(value!=0);//this loop repeats until user enters Zero.
   }
   else
   {
       //Displaying the error message
       cout<<"** NO data so no answers **"<<endl;
       exit(0);
   }
  
   //Calculating the average
   average=sum/count;
  
   //Displaying the average
   cout<<"Average :"<<average<<endl;
  
   //Displaying the maximum number
   cout<<"Max :"<<max<<endl;
  
   //Displaying the minimum number
   cout<<"Min :"<<min<<endl;
return 0;
}

_____________________________________

output:

CAProgram Files (86)\Dev-CpplMinGW641bin UntilUserEntersZeroAverageMaximumMinimum.exe D Enter a Number:33 Enter a Number :88

Add a comment
Know the answer?
Add Answer to:
C++ PROGRAMING Write a program that reads numbers from the input stream until a zero is...
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
  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • Write a program that first reads an integer for the array size, then reads numbers into...

    Write a program that first reads an integer for the array size, then reads numbers into the array, computes their average, and finds out how many numbers are above the average. Make sure to include pointer syntax. Example output: Enter array size: 10 10 random numbers between 1 and 10 generated are: 2 8 5 1 10 5 9 9 3 5 The average is: 5.7 Number of items above the average = 4 C++ Code only.

  • 2. Write a program that reads N integer numbers of array from the user and then...

    2. Write a program that reads N integer numbers of array from the user and then displays the sum, max number and the numbers of the array use four subroutines ( Read_Array(), Calculate_Sum(), Find_Max() and Display_Array()). Here is sample output Please enter number of elements: 4 Please enter the required numbers: 50 100 150 20 The entered numbers are: 50 100 150 20 Sum is : 320 Max is 150 by Mpsi ,sum and max to call subroutine and retrieve...

  • (Count positive and negative numbers and compute the average of numbers) Write a program that reads...

    (Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number. If you entire input is 0, display 'No numbers are entered except 0.' *So I think my code is looping because the...

  • Write a Java program that reads a file until the end of the file is encountered....

    Write a Java program that reads a file until the end of the file is encountered. The file consists of an unknown number of students' last names and their corresponding test scores. The scores should be integer values and be within the range of 0 to 100. The first line of the file is the number of tests taken by each student. You may assume the data on the file is valid. The program should display the student's name, average...

  • Add a method called median() to the ArrayIns class in the insertSort.java program (Listing 3.3). This...

    Add a method called median() to the ArrayIns class in the insertSort.java program (Listing 3.3). This method should return the median value in the array. (Recall that in a group of numbers half are larger than the median and half are smaller.) Do it the easy way. LISTING 3.3 The insertSort.java Program // insertSort.java // demonstrates insertion sort // to run this program: C>java InsertSortApp //-------------------------------------------------------------- class ArrayIns { private long[] a; // ref to array a private int nElems;...

  • In this exercise, write a complete Java program that reads integer numbers from the user until...

    In this exercise, write a complete Java program that reads integer numbers from the user until a negative value is entered. It should then output the average of the numbers, not including the negative number. If no non-negative values are entered, the program should issue an error message. You are required to use a do-while loop to solve this problem. Solutions that do not use a do-while loop will be given a zero. Make sure to add appropriate comments to...

  • C++ Write a program that reads three positive integers (> 0) from the command line (one...

    C++ Write a program that reads three positive integers (> 0) from the command line (one at a time), then computes and prints the smallest entered number. Use if-else statements for three integer comparison. Example: Enter an integer: 5 Enter an integer: 23 Enter an integer: 7 The smallest number is: 5 Example 2: Enter an integer: 3 Enter an integer: 3 Enter an integer: 6 The smallest number is: 3 "The input must be positive number. The program should...

  • I need help with this in C++ Program 2) Display and sum up all the numbers...

    I need help with this in C++ Program 2) Display and sum up all the numbers of factor of 5 in a given range. a) Request a number range (min/max values) separated by a space. b) Use a validation loop to ensure that both max and min are integers and max is greater than min. c) Output all the numbers of factor of 5 between the max and min. d) Output the sum of all the displayed numbers. e) Output...

  • (C programing, Not C++) Write a program in C that asks the user to input 10...

    (C programing, Not C++) Write a program in C that asks the user to input 10 numbers. Store these numbers in an array. Then ask the user to input a single number. Your program should execute a linear search on the array, trying to find that number. If the number exists in the array, have the program print out which position/element the number was found at. If the target number is not in the array, have the program print out...

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
ADVERTISEMENT