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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

const int n = 30;


char menu() {

	cout << "Menu" << endl;
	cout << "========================" << endl;
	cout << "1 ... GENERATE WEIGHTS" << endl;
	cout << "2 ... AVERAGE WEIGHT" <<endl;
	cout << "3 ... DAYS ABOVE AVERAGE" <<endl;
	cout << "4 ... MIN WEIGHT" << endl;
	cout << "5 ... EXIT" << endl;
	cout << "========================" << endl;
	cout << "Select: ";

	string selection;
	do
	{
		if (!getline(cin, selection))
		{
			return 0;
		}
	}while (selection.length() == 0);
	return selection[0];
}
//I know I can combine fillArrayWeight and printArray
void fillArrayOfWeight(int weight[], int n) {
	srand(time(NULL));
	for (int index = 0; index < n; index++) {
		weight[index] = rand() % 100 + 1;
	}
}

void printArray(int weight[], int n) {
	for (int index = 0; index < n; index++) {
		weight[index] = rand() % 100 + 1;
		cout << weight[index] << endl;
	}
}

int main(int argc, char *argv[]) {
	bool running = true;
	char izbira;
	int weight[n];
	double sum = 0;
	float average = 0.0;
	double min = weight[n]; //minimum value

	do {
		izbira = menu();
		switch (izbira) {
		//Generate weights
		case '1':
			fillArrayOfWeight(weight, n);
			cout << "Weights: " << endl;
			printArray(weight, n);
			cout << endl;
			break;
		//Average weight
		case '2' :
			fillArrayOfWeight(weight, n);
			cout << "Average weight: " << endl;
			for (int i = 0; i < n; i++) {
				sum = sum + weight[n];
			}
			average = ((float)sum) / n;
			cout << average << endl;
			break;
		//Minimum weight
		case '4':
			for (int i = 1; i < n; i++) {
				if (weight[n] < min) {
					min = weight[i];
				}
			}

			cout << min << endl;
			break;
		//Exit
		case '5':
			running = false;
			break;
		default:
			cout << "Wrong selection!\n";
		}
	} while (running);

	return 0;
}
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.
Registered users can post here. Sign in or register to post.