Question

(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 compiler says it takes too long. Could I have someone take a look at my code. Thanks!

#include <iostream>
using namespace std;

   int main()
   {
       int numberOfPositives = 0, numberOfNegatives = 0, total = 0, count = 0;
        int number;
       float average;

       cout << "Enter an integer: ";
       cin >> number;

       while (number != 0) {
           total += number;
           count++;

           if (number > 0) {
               numberOfPositives++;
           }

           else if (number < 0) {
               numberOfNegatives++;
           }

           else if (total = 0) {
               cout << "No numbers are entered except 0.";
           }
           average = total / count;
       }
           cout << "The number of positives is " << numberOfPositives;
           cout << "The number of negatives is " << numberOfNegatives;
           cout << "The total is " << total;
           cout << "The average is " << average;
           return 0;
       }
       


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

ANSWER : Here I am giving the code and output if you have any problem then comment or like my solution. Here I am taking a list to store the numbers which user enters because we have to find total , avarage and numbers of postive and negatives and it not possible without storing the data.

CODE:

#include <iostream>
#include <list>
#include <iterator>
using namespace std;

int main()
{
int numberOfPositives = 0, numberOfNegatives = 0, total = 0, count = 0;
int number;
float average;
list <int> lst; // taking a list because user can enter many numbers untill 0 is not inputed.
while(1){
cout << "Enter an integer: ";
cin >> number;
if(number ==0)
break;
else{
lst.push_back(number);
}
}
list <int> :: iterator it; // iterator of the list.
for(it = lst.begin(); it != lst.end(); ++it) {
total += *it;
count++;

if (*it > 0) {
numberOfPositives++;
}

else if (*it < 0) {
numberOfNegatives++;
}

else if (total = 0) {
cout << "No numbers are entered except 0.";
}
}

average = (float)total / count;
  
cout << " The number of positives is " << numberOfPositives;
cout << " The number of negatives is " << numberOfNegatives;
cout << " The total is " << total;
cout << " The average is " << average;
return 0;
}


OUTPUT:

Add a comment
Know the answer?
Add Answer to:
(Count positive and negative numbers and compute the average of numbers) Write a program that reads...
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
  • 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...

  • Correct the five syntax, run-time or logic errors that are found in the CountAndAverageNumbers.java file. Link...

    Correct the five syntax, run-time or logic errors that are found in the CountAndAverageNumbers.java file. Link to the file: CountAndAverageNumbers.javaPreview the document. Make sure you include comments in your code where errors were corrected. If you do not flag each error with a comment, points will be deducted. Upload the corrected .java file here. The output of the corrected file looks like the following: Debug4Output.PNG This program reads an unspecified number of integers, determines how many positive and negative values...

  • 1. Please provide a C++ program which faithfully reads a list of non-negative integer scores, ultimately...

    1. Please provide a C++ program which faithfully reads a list of non-negative integer scores, ultimately terminating the list reading when the sentinel value (lets use -9999) is entered. The program should then correctly report the number of non-negative scores entered and the arithmetic mean (average) of those scores 2. Demonstrate your programs behavior in response to errors on input (that is, show that it faithfully rejects improper input such as alphabetic characters, decimal points, and commas). Here are some...

  • 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...

  • (While-loop controlled by a sentinel value) Write a program that reads an unspecified number of integers....

    (While-loop controlled by a sentinel value) Write a program that reads an unspecified number of integers. Your program ends with the input 0. Determine and display how many positive and negative values have been read, the maximum and minimum values, and the total and average of the input values (not counting the final 0).

  • This is a Java program Write a program that reads an unspecified number of integers, determines...

    This is a Java program Write a program that reads an unspecified number of integers, determines home 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 to 2 decimal places. System.out.println(countPositive); System.out.println(countNegative); System.out.println(total); System.out.printf("%.2f",total * 1.0 / count); Assume inputs are always integer [-912985158, 912985158] and ends with 0. Input 1 2 -1 3...

  • read in numbers into array , print out, sort - my program reads in and prints...

    read in numbers into array , print out, sort - my program reads in and prints out but crashes before the sort - crashes after it prints out the scores - *important I have to use pointers!!!! c++ #include <iostream> using namespace std; void sortarray(int *tarray, int ); void displayarray(int *[], int); void averageshowarray(int *[], int); int main() {     int Size;     int count;     cout << "enter the size: " << endl;     cin >> Size;     int...

  • Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel –...

    Please Help! Write a C++ program to read integers until 9999 is entered (called sentinel – sentinel is used to indicate the end of input and must not to be considered as the valid data for the computation). Perform the following operations for the integers entered by the user. Display the sum of the numbers less than 100 Display the smallest of the positive integers Display the largest of the negative integers Display how many integers are between 100 and...

  • Question text Find four problems with this program: ------------------------------------------- using namespace std; // this program inputs...

    Question text Find four problems with this program: ------------------------------------------- using namespace std; // this program inputs three integers, determines if negative, positive, or zero int main() { cout << "Enter an integer" << endl; cin >> input;    if (input < 0); cout << "The number is negative."  endl; else if (input > 0) cout << "The number is positive" << endl; else cout << "The number is zero" << endl;    return 0; } programm c++ please and thank you

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