我应该输出值的总数SUM,AVERAGE,MIN,MAX和负值的总数。我得到了总和,我无法获得十进制的平均值;至于MAX,MIN和负数总数,我的代码无法正常工作。您能为我指出正确的方向,如何修正我的代码以计算负值的数量以及使用以下给定值的MIN / MAX:14.7,-2.8、105.2,-56.8、0、8.4,-20.4
int sum = 0;
double number;
double values;`enter code here`
int averagevalues;
int negativevalues;
float min=500, max=0;
cout << "Hello everybody!! \n";
cout << "How many values would you like to enter? XD \n";
cin >> values;
for(int i=0;i<values;i++)
{
cout << "Enter number: \n";
cin >> number;
sum=sum+number;
}
cout<<"The sum of all values is: "<< sum<<endl;
averagevalues = static_cast<double>(sum)/values;
cout<<"The average value is: "<< averagevalues<<endl;
for (int i = 0; i<values;i++)
{
if (values < min)
{
min = values;
}
if (values > max)
{
max = values;
}
}
for (int i = 0;i<values;i++)
{
if (values < 0)
{
negativevalues = values++;
}
}
cout << "The MIN of the numbers you gave me is: " << min <<endl;
cout << "The MAX of the given numbers is: " << max <<endl;
cout << " Total number of negative values is: " << negativevalues <<endl;
最佳答案
在代码末尾的循环中,变量values
只是用户输入的值的数量。因此,您没有在比较用户输入的数字。
尝试分配一个双精度数组(或所需的任何类型)来存储数字,然后将它们与最小值和最大值进行比较。
关于c++ - 负值的总和,平均值,最小值,最大值和总数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42926453/