[英]C++ ; Write a program that reads in ten whole numbers
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 or zero), and the sum of all the numbers, whether positive, negative, or zero.编写一个程序,读取十个整数并输出所有大于零的数字之和,所有小于零的数字之和(将是负数或零),以及所有数字之和,无论是正、负或零。 The user enters the ten numbers just once each and the user can enter them in any order.
用户只输入十个数字一次,用户可以按任意顺序输入它们。 Your program should not ask the user to enter the positive numbers and the negative numbers separately.
您的程序不应要求用户分别输入正数和负数。 Now modify this program so that it outputs the sum of all positive numbers, the average of all positive numbers, the sum of all non-positive numbers, the average of all non-positive numbers, the sum of all positive and non-positive numbers, and the average of all numbers entered.
现在修改这个程序,使其输出所有正数的总和、所有正数的平均值、所有非正数的总和、所有非正数的平均值、所有正数和非正数的总和,以及输入的所有数字的平均值。
This is what I have so far...这是我到目前为止...
#include <iostream>
using namespace std;
int main ()
{
int number, negative_sum=0, positive_sum=0, sum=0, average_neg,
average_pos, average;
int count=0, positiveCount=0, negativeCount=0;
cout << "Please Input 10 whole numbers(Each number should be separated by space or Enter)\n";
for(int i=0; i < 10; i++)
{
cin >> number;
if (number >= 0 )
{
positive_sum += number;
positiveCount++;
}
else
{
negative_sum += number ;
negativeCount++;
}
}
average_pos = positive_sum / positiveCount;
average_neg = negative_sum / negativeCount;
sum = positive_sum + negative_sum;
average = sum / count;
cout << "The Total sum of Positive Numbers is " << positive_sum << " and the average of \nThe postive numbers entered is ";
cout << average_pos<< endl;
cout << "The Total sum of Negative Numbers is " << negative_sum << " and the average of \nThe negative numbers entered is ";
cout << average_neg << endl;
cout << "The Total sum of numbers entered is " << sum << " and its average is "<< average << endl;
system("PAUSE");
return 0;
}
Basically it is just printing the "Please Input 10 whole numbers(Each number should be separated by space or Enter)\\n" part multiple times after I entered numbers.基本上它只是在我输入数字后多次打印“请输入 10 个整数(每个数字应以空格或 Enter 分隔)\\n”部分。
The problem with your code is that a zero divided by zero division occurs in the lines average_pos = positive_sum / positiveCount;
您的代码的问题在于,
average_pos = positive_sum / positiveCount;
行中出现零除以零除法average_pos = positive_sum / positiveCount;
average = sum / count;
and average_neg = negative_sum / negativeCount;
和
average_neg = negative_sum / negativeCount;
. . In order to avoid this you can add
if
conditions which perform the division only when positiveCount
, negativeCount
and count
are greater than 0
为了避免这种情况,您可以添加
if
条件,仅当positiveCount
、 negativeCount
和count
are greater than 0
时才执行除法
Also, in your code you are dividing sum
by count
in the line average = sum / count;
此外,在您的代码中,您将
sum
除以count
行average = sum / count;
but no where in your code is count
being incremented.但在您的代码中没有任何地方增加
count
。 Your for
loop was also ended prematurely.您的
for
循环也提前结束。
The Correct Modified Code :正确的修改代码:
#include <iostream>
using namespace std;
int main ()
{
int number, negative_sum=0, positive_sum=0, sum=0, average_neg=0,
average_pos=0, average=0;
int count=0, positiveCount=0, negativeCount=0;
cout << "Please Input 10 whole numbers(Each number should be separated by space or Enter)\n";
for(int i=0;i<10;i++)
{
//cout<<"i is"<<i<<endl;
cin >> number;
if (number >= 0 )
{
positive_sum += number;
positiveCount++; count++;
}
else
{
negative_sum += number ;
negativeCount++; count++;
}
}
if(positiveCount>0)
{
average_pos = positive_sum / positiveCount;
}
if(negativeCount>0)
{
average_neg = negative_sum / negativeCount;
}
sum = positive_sum + negative_sum;
if(count>0)
{
average = sum / count;
}
cout<<average_pos;
cout << "The Total sum of Positive Numbers is " << positive_sum << " and the average of \nThe postive numbers entered is ";
cout << average_pos<< endl;
cout << "The Total sum of Negative Numbers is " << negative_sum << " and the average of \nThe negative numbers entered is ";
cout << average_neg << endl;
cout << "The Total sum of numbers entered is " << sum << " and its average is "<< average << endl;
system("PAUSE");
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
int number, negative_sum=0, positive_sum=0, sum=0, average_neg = 0,
average_pos = 0, average = 0;
int count=0, positiveCount=0, negativeCount=0;
cout << "Please Input 10 whole numbers(Each number should be separated by space or Enter)\n";
for(int i=0; i < 10; i++)
{
cin >> number;
if (number >= 0 )
{
positive_sum += number;
positiveCount++;
}
else
{
negative_sum += number ;
negativeCount++;
}
}
average_pos = positive_sum / positiveCount;
average_neg = negative_sum / negativeCount;
sum = positive_sum + negative_sum;
average = sum / count;
cout << "The Total sum of Positive Numbers is " << positive_sum << " and the average of \nThe postive numbers entered is ";
cout << average_pos<< endl;
cout << "The Total sum of Negative Numbers is " << negative_sum << " and the average of \nThe negative numbers entered is ";
cout << average_neg << endl;
cout << "The Total sum of numbers entered is " << sum << " and its average is "<< average << endl;
system("PAUSE");
return 0;
}
Try this尝试这个
#include <iostream>
using namespace std;
int main ()
{
int number, negative_sum=0, positive_sum=0, sum=0, average_neg = 0,
average_pos = 0, average = 0;
int count=0, positiveCount=0, negativeCount=0;
cout << "Please Input 10 whole numbers(Each number should be separated by space or Enter)\n";
for(int i=0; i < 10; i++)
{
cin >> number;
if (number >= 0 )
{
positive_sum += number;
positiveCount++;
}
else
{
negative_sum += number ;
negativeCount++;
}
}
average_pos = positive_sum / positiveCount;
average_neg = negative_sum / negativeCount;
sum = positive_sum + negative_sum;
average = sum / count;
cout << "The Total sum of Positive Numbers is " << positive_sum << " and the average of \nThe postive numbers entered is ";
cout << average_pos<< endl;
cout << "The Total sum of Negative Numbers is " << negative_sum << " and the average of \nThe negative numbers entered is ";
cout << average_neg << endl;
cout << "The Total sum of numbers entered is " << sum << " and its average is "<< average << endl;
system("PAUSE");
return 0;
}
Hope this helps ... :)希望这可以帮助 ... :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.