Question

Program 6: Sums and Averages. Assigned Wednesday, 10/05/16 Program Due Wednesday, 10/12/16 , at start of...

Program 6: Sums and Averages.                                                  

Assigned                 Wednesday,  10/05/16  
Program Due              Wednesday,  10/12/16 ,  at start of class.

Write a program in c++ that asks the user to input a list of numbers, 
one at a time, until the user is finished.

The program should keep track of how many numbers, how many positive numbers,
and how many negative numbers were entered. It should also calculate the
sums for each of these (positives, negatives, and total).

After exiting the loop, the program should calculate the averages for each
(positives, negatives, and total).

Then output the sums and their averages, with labels describing each output
appropriately.

Note that there may be no positives, or no negatives, or no numbers at all
in a list! Also, a 0 counts as a number in the overall list, but it doesn't
counts as positive or negative.

Allow the user to enter as many lists as they like.

Note that there is no data check for a bad number -- any integer is valid!

Data to test/run:

---------------------------------------------------------------

Dataset 1:

(empty)

---------------------------------------------------------------

Dataset 2:

  10
 -13
   0
  26
 -17

---------------------------------------------------------------

Dataset 3:

 -10
 -20
 -45

---------------------------------------------------------------

Dataset 4:

 10
 30
 80

---------------------------------------------------------------

Dataset 5:

 0
 0
 0
 
---------------------------------------------------------------

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

Program:

#include<iostream>

using namespace std;

int main()
{
   int num, NoOfPositiveNumbers=0,NoOfNegativeNumbers=0, TotalNumbers=0;
   float sumOfPositives=0,sumOfNegatives=0,total=0;
   float avgOfPositives=0,avgOfNegatives=0,avg=0;
  
   bool repeat =true;
  
   //read numbers from user until the user enters 77777( 5 sevens)
   //i used 77777 as sentinel value to exit the loop
   cout<<"Enter numbers (77777 to exit): ";
   do
   {
       cin>>num;
      
       //check if number enter is not sentinel and if number is negative
       //if so increment the count of negatives and add it to sum of negatives
       //also get the total here
       if(num<0 && num!=77777)
       {
           NoOfNegativeNumbers=NoOfNegativeNumbers+1;
           sumOfNegatives=sumOfNegatives+num;
           total=total+num;
           TotalNumbers=TotalNumbers+1;
       }
      
       //check if number enter is not sentinel and if number is positive
       //if so increment the count of positives and add it to sum of positives
       //also get the total here
       else if(num>0 && num!=77777)
       {
           NoOfPositiveNumbers=NoOfPositiveNumbers+1;
           sumOfPositives=sumOfPositives+num;
           total=total+num;
           TotalNumbers=TotalNumbers+1;
       }
      
       //check if number enter is not sentinel and if number is 0
       //if so increment the count of total
       else if(num==0 && num!=77777)
       {
           TotalNumbers=TotalNumbers+1;
           total=total+num;
       }
      
       //if sentinel value is entered exit the loop
       else if(num==77777)
       {
           repeat=false;
       }
          
   }while(repeat);
   //calculate the averages and display results
   if(TotalNumbers>0)
   {
       if(NoOfPositiveNumbers==0)
       {
           avgOfPositives=0;
       }
       else
       {
           avgOfPositives=sumOfPositives/NoOfPositiveNumbers;
       }
      
       if(sumOfNegatives==0)
       {
           avgOfNegatives=0;
       }
       else
       {
           avgOfNegatives=sumOfNegatives/NoOfNegativeNumbers;
       }
          
       avg=total/TotalNumbers;
  
       cout<<"\nTotal Numbers: "<<TotalNumbers<<"\tSum: "<<total<<"\tAverage: "<<avg;
       cout<<"\nTotal Positives: "<<NoOfPositiveNumbers<<"\t"<<"Sum of Positives: "<<sumOfPositives<<"\tAverage of Positives: "<<avgOfPositives;
       cout<<"\nTotal Negatives: "<<NoOfNegativeNumbers<<"\t"<<"Sum of Negatives: "<<sumOfNegatives<<"\tAverage of Negatives: "<<avgOfNegatives;
   }
   else
   {
       cout<<"\nThe dataset is empty.";
   }
  
  
   return 0;
}

Output:

C.Users Vicky Desktop\DevC++Programs ListOfNumbers.exe Enter a number 〈????? to exit); ??777 The dataset is empty Process exiC.Users Vicky Desktop\DevC++Programs ListOfNumbers.exe 13 26 17 Total Numbers: 5 Total Positiues 2 Total Negatives: 2 Sum: 6C.Users Vicky Desktop\DevC++Programs ListOfNumbers.exe 20 45 Total Numbers: 3 Total Positives 0 Total Negatives: 3 Sum: 75 SuC.Users Vicky Desktop\DevC++Programs ListOfNumbers.exe Enter numbers 〈????? to exit): 10 30 80 Total Numbers 3 Total PositiveC:Users Vicky Desktop\DevC++Programs ListOfNumbers.exe Enter numbers 〈????? to exit): 0 Total Numbers: 3 Total Positives 0 To

Add a comment
Know the answer?
Add Answer to:
Program 6: Sums and Averages. Assigned Wednesday, 10/05/16 Program Due Wednesday, 10/12/16 , at start of...
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
  • Positive and negative: Return these four results using C++ reference parameter Write a function, named sums(),...

    Positive and negative: Return these four results using C++ reference parameter Write a function, named sums(), that has two input parameters; an array of floats; and an integer, n, which is the number of values stored in the array. Compute the sum of the positive values in the array and the sum of the negative values. Also count the number of positives and negative numbers in each category. Write a main program that reads no more than 10 real numbers...

  • Java programming 1. Write a program that reads an unspecified number of integers, determines how many...

    Java programming 1. Write a program that reads an unspecified number of integers, determines how many positive and negative value 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 Here are sample runs: (red indicates a user input) Enter an integer, the input ends if it is 0: 1 2 -1 3 0 The number of positives is...

  • Using basic c++ 2. Count the positive and negative numbers using ***while loop*** • Write a...

    Using basic c++ 2. Count the positive and negative numbers using ***while loop*** • Write a program that reads unspecified number of integers , determines how many negative and positive values have been read. Also calculate total and average. Your program will end with input 0. • Output Enter an integer, the input ends if it is 0: 25 34 -89 72 -35 -67 21 48 0 The number of positives is 5 The number of negatives is 3 The...

  • Use Python 3 11a is using the binary search program that counts steps amend the program...

    Use Python 3 11a is using the binary search program that counts steps amend the program so that a list of size 10000 is made - using unique random numbers in the range 1 through 20000. Test how many steps to find the number 9467. Using your solution to Lab 11A, write a program that counts how many steps to find the number 9467 in a list of size 20000 (using unique random numbers in the range 1 through 321000)....

  • C++ Programming Help Please! NOTE: Please READ All Steps very carefully. DO #10! (10 is based...

    C++ Programming Help Please! NOTE: Please READ All Steps very carefully. DO #10! (10 is based off of 9). Program Info:   #9. Write a program that reads in ten whole numbers and that outputs the sum of all the numbers greater than zero, the sum of all the numbers less than zero (which will be a negative number or zero), and the sum of all the numbers, whether positive, negative, or zero. The user enters the ten numbers just once...

  • 10) Write a program that inputs five numbers and determines the number of negative numbers input,...

    10) Write a program that inputs five numbers and determines the number of negative numbers input, the number of positive numbers input and the number of zeros input. An easy way to create a counter is given in the example below: int counter 0; initializes the counter counter counter +1;//increments the counter each time is used. Note: Output should read as shown below where # replaces the number of inputs and it is displayed on a table format] (40 pts.)...

  • Write a program that calculates the average of a stream of non-negative numbers. The user can...

    Write a program that calculates the average of a stream of non-negative numbers. The user can enter as many non-negative numbers as they want, and they will indicate that they are finished by entering a negative number. For this program, zero counts as a number that goes into the average. Of course, the negative number should not be part of the average (and, for this program, the average of 0 numbers is 0). You must use a method to read...

  • You will be given a file that will contain averages for classes which are divided into...

    You will be given a file that will contain averages for classes which are divided into sections in the following format. The name of this file will be "grades.txt". A 93B 82A 86C 75 F 83 You are to write a program that computes the averages for the sections of classes, they are divided into groups that are denoted by a capital letter. You are to use arrays to calculate the average. I recommend using two arrays one to keep...

  • Write a program that writes a series of random numbers to a file. Each random number...

    Write a program that writes a series of random numbers to a file. Each random number should be in the range of 1 through 500 inclusive. 1.Use a file called randoms.txt as a input file and output file a. If you go to open the file and its not there then ask the user to create the file     and then quit 2. Ask the user to specify how many random numbers the file will hold. a.Make sure that the...

  • Analyze a family's spending habits by creating a program that performs arithmetic calculations. The program should...

    Analyze a family's spending habits by creating a program that performs arithmetic calculations. The program should ask the user to enter information for the following questions. How much does the family spend on groceries per month? How much does the family spend on dining out per month? How much does the family spend on entertainment per month? How much does the family spend on rent/mortgage per month? How much does the family spend on utilities per month? How many family...

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