### Average and minimum value from Array

I'm trying to calculate the average and a minimum value from an array after I create random numbers. I tried a lot of different methods and nothing works. I don't know what I'm doing wrong so I kindly ask you to redirect me on the right path.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495`` ``````#include #include #include using namespace std; const int n = 30; char menu() { cout << "Menu" << endl; cout << "========================" << endl; cout << "1 ... GENERATE WEIGHTS" << endl; cout << "2 ... AVERAGE WEIGHT" <
Last edited on

Maybe you should review the main function. Because it might be a bit malfunctioning
The function is good and it runs. I'm trying to solve case 2 and 4. Thanks for the help.
A function that is "good" should not say:
 ```52:23: warning: 'weight[30]' is used uninitialized in this function [-Wuninitialized] 69:25: warning: array subscript is above array bounds [-Warray-bounds] 77:17: warning: array subscript is above array bounds [-Warray-bounds] ```

`int weight[n];` does not have element `weight[n]`. The last element of that array is `weight[n-1]`.

Therefore, lines 52, 69, and 77 have an out of range error that causes undefined behaviour. Absolutely not good.

Line 66 changes the contents of the array.

The sum is initialized to 0 before the main loop. If you calculate average more than once, then you keep adding to previous sum. Same with minimum.
I get no errors while running in Eclipse. Will check, thank you.