简体   繁体   English

C++ ; 编写一个读取十个整数的程序

[英]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条件,仅当positiveCountnegativeCountcount are greater than 0时才执行除法

Also, in your code you are dividing sum by count in the line average = sum / count;此外,在您的代码中,您将sum除以countaverage = 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.

相关问题 写一个循环,读取十个数字,然后输出它们 - Write a loop that reads ten numbers and then outputs them C++,计算正负数并计算数字的平均值)编写一个程序,读取未指定数量的整数 - C++ ,Count positive and negative numbers and compute the average of numbers) Write a program that reads an unspecified number of integers C ++,构造一个读取两个实数和一个字符的程序 - C++, constructing a program that reads two real numbers and a character 一个 C++ 程序,它读取 100 名学生中获得的分数。它还计算平均值; 最低和最高分 - A C++ Program that reads the marks obtained of ten students out of 100. It also computes the average; lowest and highest marks C++ 程序将用户的十个数字放入一个数组并使用指针按顺序对数组进行排序 - C++ program than takes ten numbers from user into an array and sorts the array in order, using pointers 我的c ++程序会读取矩阵并打印出非零数字,这会生成运行时错误 - My c++ program that reads in a matrix and prints out the non-zero numbers is generating a runtime error c ++终止while循环,以&#39;|&#39;读取数字 - c++ Terminate while loop that reads numbers with '|' 编写一个程序,该程序读取所有命令行参数并打印平均值,c ++ - Write a program that reads in all the command line arguments and prints the average, c++ 我的程序只读取一半的数组c ++ - My program just reads half an array c++ 检查程序从计算机读取的文件 (C++) - Check what files a program reads from a computer (C++)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM