编写一个程序,读取十个整数,并输出所有大于零的数字之和,所有数字之和小于零(将为负数或零),以及所有数字的总和数字,无论是正数,负数还是零。用户每次只输入十个数字,用户可以按任何顺序输入。您的程序不应要求用户分别输入正数和负数。现在修改这个程序,使它输出所有正数的总和,所有正数的平均值,所有非正数的总和,所有非正数的平均值,所有正数和非正数之和,以及输入的所有数字的平均值。
这就是我到目前为止......
#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;
}
基本上它只是打印&#34;请输入10个整数(每个数字应以空格或Enter分隔)\ n&#34;我输入数字后多次出现。
答案 0 :(得分:1)
代码的问题在于行average_pos = positive_sum / positiveCount;
average = sum / count;
和average_neg = negative_sum / negativeCount;
中出现零除以零除法。为了避免这种情况,您只能在if
,positiveCount
和negativeCount
count
are greater than 0
条件
此外,在您的代码中,您要将sum
除以行count
中的average = sum / count;
,但代码中的count
不会增加for
。
您的#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;
}
循环也提前结束。
正确的修改代码:
{{1}}
答案 1 :(得分: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;
}
答案 2 :(得分:-1)
试试这个
#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;
}
希望这有助于......:)