总和,平均值,最小值,最大值和负值总数

时间:2017-03-21 11:56:58

标签: c++

我应该输出值的总数,SUM,AVERAGE,MIN,MAX和负值的总数。我得到了SUM,我无法得到小数的平均值;至于MAX,MIN和负数的总数,我的代码不起作用。你能指出我如何修正我的代码以计算负值的数量的正确方向,以及使用这些给定值的MIN / MAX:14.7,-2.8,105.2,-56.8,0,0.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;

1 个答案:

答案 0 :(得分:0)

在代码末尾的循环中,变量values只是用户输入的值的数量。所以你不是在比较用户输入的数字。

尝试分配一个双打数组(或任何你想要的类型)来存储数字,然后将它们与最小值和最大值进行比较。