Question

How do I make this code not include negative numbers in the total or average if...

How do I make this code not include negative numbers in the total or average if they are entered? (C++)

For example, if you enter 4, 2, and -2, the average should not factor in the -2 and would be equal to 3. I want the code to factor in positive numbers only.

-----

#include

using namespace std;

int main()
{
   int x, total = 0, count = 0;
  
   cout << "Type in first value ";
   cin >> x;
   while (x != -1)

   {
       total += x;
       count++;
       cout << "Type in next value ";
       cin >> x;
   }
  
   cout << "The average of these numbers is " << (double) total/count << endl;
  
   cout << endl;
   return 0;
}

Homework Answers

Answer #1

#include

using namespace std;

int main()
{
   int x, total = 0, count = 0;
  
   cout << "Type in first value ";
   cin >> x;
   while (x != -1)

   {

if(x>=0)

{

       total += x;
       count++;
}
       cout << "Type in next value ";
       cin >> x;
   }
  
   cout << "The average of these numbers is " << (double) total/count << endl;
  
   cout << endl;
   return 0;
}

Know the answer?
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
Similar Questions
This code it's not working, fix it for me please #include <iostream> using namespace std; class...
This code it's not working, fix it for me please #include <iostream> using namespace std; class People {    string name;    double height; public:    void setName(string name)    {        this->name = name;    }    void setHeight(double height)    {        this->height = height;    }    double getHeight() {        return height;    }    string getName()    {        return name;    } }; int main() {    const int size...
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double...
Need someone to fix my code: #include<iostream> using namespace std; struct student { double firstQuizz; double secondQuizz; double midTerm; double finalTerm; string name; }; int main() { int n; cout<<"enter the number of students"<<endl; cin>>n; struct student students[n]; int i; struct student istudent; for(i=0;i<n;i++) {    cout<<"Student name?"; cin >> istudent.name; cout<<"enter marks in first quizz , second quizz , mid term , final term of student "<<i+1<<endl; cin>>students[i].firstQuizz>>students[i].secondQuizz>>students[i].midTerm>>students[i].finalTerm; } for(i=0;i<n;i++) { double marks=0; double score=students[i].firstQuizz+students[i].secondQuizz+students[i].midTerm+students[i].finalTerm; marks=(students[i].firstQuizz*0.25)+(students[i].secondQuizz*0.25)+(students[i].midTerm*0.25)+(students[i].finalTerm*0.50); double totalArrgegateMarks =...
im trying to make a program that will use a while loop to repeatedly ask a...
im trying to make a program that will use a while loop to repeatedly ask a user for a test score. Use a counter to exit the loop when the user has entered 10 test scores. The loop is to figure out the total of all the scores and the highest score. it must use a while loop please explain problems in code the bottom code is my work done but does not work. #include <iostream> using namespace std; int...
No matter what I do I cannot get this code to compile. I am using Visual...
No matter what I do I cannot get this code to compile. I am using Visual Studio 2015. Please help me because I must be doing something wrong. Here is the code just get it to compile please. Please provide a screenshot of the compiled code because I keep getting responses with just code and it still has errors when I copy it into VS 2015: #include <iostream> #include <conio.h> #include <stdio.h> #include <vector> using namespace std; class addressbook {...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream>...
C++ Please and thank you. I will upvote Read the following problem, and answer questions. #include<iostream> using namespace std; void shownumbers(int, int); int main() { int x, y; cout << "Enter first number : "; cin >> x; cout << "Enter second number : "; cin >> y; shownumbers(x, y); return 0; } void shownumbers(int a, int b) { bool flag; for (int i = a + 1; i <= b; i++) { flag = false; for (int j =...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment...
C++ please Write code to implement the Karatsuba multiplication algorithm in the file linked in Assignment 2 (karatsuba.cpp) in Canvas (please do not rename file or use cout/cin statements in your solution). As a reminder, the algorithm uses recursion to produce the results, so make sure you implement it as a recursive function. Please develop your code in small The test program (karatsuba_test.cpp) is also given. PLEASE DO NOT MODIFY THE TEST FILE. KARATSUBA.CPP /* Karatsuba multiplication */ #include <iostream>...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void...
in C++ Need a heap-sort function #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Your code here ----------------- } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc...
Quick sort func in C++ #include <iostream> #include <stdlib.h> #include <string> using namespace std; void MyFunc ( int *array ) { // Code here } int main(int argc,char **argv) { int *Sequence; int arraySize; // Get the size of the sequence cin >> arraySize; // Allocate enough memory to store "arraySize" integers Sequence = new int[arraySize];    // Read in the sequence for ( int i=0; i<arraySize; i++ ) cin >> Sequence[i]; // Run your algorithms to manipulate the elements...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath>...
Please write variables and program plan (pseudocode) of the C++ programming below: #include <iostream> #include <cmath> using namespace std; void divisors(int num); int main () {    char repeat;    int num;       while (repeat !='n')    {        cout << "Enter a number: ";        cin >> num;        divisors(num);        cout << "Continue? (y or n): ";        cin >> repeat;    }    return 0; } void divisors(int num) {   ...
Consider the following program: #include #include #include using namespace std; int main() { int num1; int...
Consider the following program: #include #include #include using namespace std; int main() { int num1; int num2; cout << fixed << showpoint << setprecision(2); cout << "Enter two integers: "; cin >> num1 >> num2; cout << endl; if (num1 != 0 && num2 != 0) cout << sqrt(abs(num1 + num2) + 0.5) << endl; else if (num1 != 0) cout << floor(num1 + 0.5) << endl; else if (num2 != 0) cout << ceil(num2 + 0.5) << endl; else...