Question

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 total is 9

The average is 1.125

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
using namespace std;

int main(){
   int n, positive = 0, negative = 0, total = 0, count = 0;
   cout<<"Enter an integer, the input ends if it is 0: ";
   cin>>n;
   while(n!=0){
      if(n>0){
         positive+=1;
      }
      else{
         negative += 1;
      }
      total+= n;
      count += 1;
      cin>>n;
   }
   cout<<"The number of positives is "<<positive<<endl;
   cout<<"The number of negatives is "<<negative<<endl;
   cout<<"The total is "<<total<<endl;
   cout<<"The average is "<<(total*1.0/count)<<endl;
   return 0;
}

Add a comment
Know the answer?
Add Answer to:
Using basic c++ 2. Count the positive and negative numbers using ***while loop*** • Write a...
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
  • (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...

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

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

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

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

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

  • Please write a java program that inputs positive numbers using a while loop until a negative...

    Please write a java program that inputs positive numbers using a while loop until a negative number is input and finds the sum of the numbers input

  • Objectives: Use the while loop in a program Use the do-while loop in a second program...

    Objectives: Use the while loop in a program Use the do-while loop in a second program Use the for loop in a third program Instructions: Part A. Code a for loop to print the Celsius temperatures for Fahrenheit temperatures 25 to 125. C = 5/9(F – 32).Output should be in a table format: Fahrenheit Celsius 25 ? . . . . . . . . Turn in the source and the output from Part A. Part B. Code a while...

  • In C++ write an application which takes positive or negative integers and returns the sum of...

    In C++ write an application which takes positive or negative integers and returns the sum of each place in the number, such that 246 results in 12 and -88 results in -16. The program is called and runs with no prompt for input. The program reads exactly one signed integer from standard input. The program displays the sum of each digit of the number as a signed integer. This should be done in two files called main.cc and makefile.The result...

  • Problem 4: Write a Java program that reads positive integers, find the largest of them, count...

    Problem 4: Write a Java program that reads positive integers, find the largest of them, count its occurrences and print out the average of all input integers up to two decimal places (小數 點第二位). Use the characters·Q' or'q, to end the input. Below are two sample runs: Enter an integer, or quit with Q or q:1 Enter an integer, or quit with Q or q: 2 Enter an integer, or quit with Q or q: 3 Enter an integer, or...

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