-1

I try to write in an array of numbers greater than 54. And then output its sum, average, maximum and minimum accordingly. still i tried do - while through but also failed.. ( But I always get 0, how can I fix it? Thank!

{
    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;
 }
  • 1
    Use your debugger – Michael Chourdakis Jan 14 '20 at 23:07
  • 2
    Note: you don't seem to use the array anywhere except to individually process the inputs. That means you don't need it. You can compute almost everything you're given as the user provides it. Is the new number less than the minimum? That's the new minimum. Is it bigger than the maximum? It's the new maximum. Add it to a running sum and when you're done divide it by the number of inputs to get the average. No memory wasted on the array and one loop does all of the work. Code you don't write has no bugs, so it's in your best interest to keep things as simple as possible. – user4581301 Jan 14 '20 at 23:14

2 Answers2

2

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; 
artm
  • 17,291
  • 6
  • 38
  • 54
-1

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. after that you shouldn't put "sum" in the loop!

    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.

scaryhamid
  • 345
  • 2
  • 9