简体   繁体   English

我想找到最大值、最小值和平均值,但它为我分配了 0

[英]i want to find maximum, minimum and average but it assigns me something for 0

I try to write in an array of numbers greater than 54. And then output its sum, average, maximum and minimum accordingly.我尝试写入大于 54 的数字数组。然后相应地输出其总和、平均值、最大值和最小值。 still i tried do - while through but also failed.. ( But I always get 0, how can I fix it? Thank!我仍然尝试这样做 - 虽然通过但也失败了..(但我总是得到 0,我该如何解决?谢谢!

{
    int arr_first [10];
    int min = arr_first[0];
    int max = arr_first[0];
    int sum = 0;
    int aritm_mean = 0;
    int number;
    int maxArrayNumber = 54;

    cout << "Fill the array (10 numbers): " << endl;

    for ( int i = 0; i < 10;  ) {
         cin >> number;

        if ( number < maxArrayNumber ) {
            arr_first[number];
            i++;
        }
    }


    for ( int j = 0; j < 10;  ) {
        sum = sum + arr_first[number];
        cout << "Sum = " << sum << endl;
        j++;
    }

    for ( int j = 0; j < 10; j++ ) {
        sum += arr_first[number];
        aritm_mean = sum / 10;
    }
    cout << "Arithmetic mean = " << aritm_mean << endl;


    for ( int j = 0; j < 10; j++ ) {
        if ( max >= arr_first[0] ) {
            max = arr_first[number];
        }
    }
   cout << "Max number = " << max << endl;

    for ( int j = 0; j < 10; j++ ) {
        if ( min <= arr_first[0] ) {
            min = arr_first[number];
        }
    }
   cout << "Min number = " << min << endl;
 }

You at least need to fix the possible out of bound access:您至少需要修复可能的越界访问:

for ( int i = 0; i < 10;  ) {
     cin >> number;

    if ( number < maxArrayNumber ) {
        arr_first[number];    //<-- possible out of bound access
        i++;
    }
}

Out of bound access, should be fixed by:越界访问,应通过以下方式修复:

arr_first[i] = number; 

ok you have a lot of problems in this code.好的,你在这段代码中有很多问题。 your firts loop should be like this:你的第一个循环应该是这样的:

for (int i = 0; i < 10;i++ ) {
cin >> number;

if (number < maxArrayNumber) {
    arr_first[i]=number;
}

you have to use i++ like that.你必须像那样使用 i++。 after that you shouldn't put "sum" in the loop!之后你不应该把“sum”放在循环中!

    cout << "Sum = " << sum << endl;

this one should be outside that loop.这个应该在那个循环之外。

and for you max and min:对你来说最大和最小:

if (arr_first[j] > max )

it should be like this.它应该是这样的。 you wrote it upside down你把它倒过来写

And im so sorry i cant speak En well.我很抱歉我的英语说得不好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM