Need help!! why am i getting uninitialized local variable?


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
  #include <iostream>
#include <string>

using namespace std;

double mean (int grades[], int userSize);


int main ()
{
	const int size = 100;
	int grades[size];
	int userSize;
	double avg;

	cout << "Enter number of grades to be entered: ";
	cin >> userSize;
	if (userSize > 100)
	{
		cout << "Too many grades" << endl;
		return 0;
	}

	for (int i = 0; i < userSize; i++)
	{
		cout << "Please enter grade: " << endl;
		cin >> grades[i];
		if (cin.fail() || grades[i] < 0 || grades[i] > 100)
		{
			cout << "Please enter a valid grade within";
			cout << "the range of 0 - 100" << endl;
			cin.clear();
		}
		mean (grades,userSize);
	}
	
	cout << "The mean of the grades entered is: " << avg << endl;

	cin.ignore(2);
	return 0;
}

double mean (int grades[ ], int userSize)
{
	double sum, avg;
	int j;
	for (j = 0; j < userSize; j++);
	{
		
		sum += grades[j];
		avg = sum / userSize;
		
	}
	return avg;
}


i cannot figure out why I am getting this error for 'avg' in main and for 'sum' in the mean func: 1>c:\users\nic.hall\documents\visual studio 2010\projects\wk6ilab\wk6ilab\wkilab.cpp(37): warning C4700: uninitialized local variable 'avg' used
Quiz:
1
2
double sum;
sum += 5;
What is the value of sum now?
closed account (SECMoG1T)
Yeah you need to take care of that else your results will be undefined. . Yeah

1
2
3
4
5
6
double sum, avg;
//for example the above variables contain undefined values, using such variables inan expression
//Such as:-
    sum += grades[j];/// here you are adding grades to undefined value hence it is highly likely you will end 
                                   ////up with the wrong results



A little advice : always intialize your local variables before using to avoid these hard to identify bugs.
E.g
int val=0;
double dval=0.0;
string str ("");
A little advice : always intialize your local variables before using to avoid these hard to identify bugs.
It is better to declare and initialize to a sensible value in one expression:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
double mean (int grades[], int userSize)
{
	double sum = 0.0;
	for (int j = 0; j < userSize; ++j)
	{
		sum += grades[j];
	}
	double avg = sum / userSize; 
	return avg;
}

/*
double mean (int grades[ ], int userSize)
{
    return std::accumulate(grades, grades + userSize, 0.0) / userSize;
}
*/
Also if you would initialize loop counter inside loop, compiler would tell you about the fact that your loop does nothing:
1
2
3
4
5
6
for (j = 0; j < userSize; j++); //←Look, semicolon
//It is equivalent to:
for (j = 0; j < userSize; j++)
{
    //does absolutely nothing
}
Last edited on
closed account (SECMoG1T)
Ohh yeah that's a great advice there @minnippa , i definitely agree , I can see what I missed haha.. thank you.
Last edited on
@MiiNiPaa, I was wondering why I had been getting an error when I did exactly as you suggest and was initializing 'j' inside the loop. I did not notice the semicolon! Thank you!

@All, thank you for your help, that has solved my issue.

Now if I could impose one more question: I do not remember how to call the output from the mean function(avg) back in main... I am getting an error as of now which says that avg is an undeclared identifier with the way I am currently doing it:

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
#include <iostream>

using namespace std;




int main ()
{
	const int size = 100;
	int grades[size];
	int userSize;
	float mean (int grades[], int userSize, float avg);

	cout << "Enter number of grades to be entered: ";
	cin >> userSize;
	if (userSize > 100)
	{
		cout << "Too many grades" << endl;
		return 0;
	}

	for (int i = 0; i < userSize; i++)
	{
		cout << "Please enter grade: " << endl;
		cin >> grades[i];
		if (cin.fail() || grades[i] < 0 || grades[i] > 100)
		{
			cout << "Please enter a valid grade within";
			cout << "the range of 0 - 100" << endl;
			cin.clear();
		}
		mean (grades,userSize, avg);
	}
	
	cout << "The mean of the grades entered is: " << avg << endl;

	cin.ignore(2);
	return 0;
}

float mean (int grades[ ], int userSize, float avg = 0.00)
{
	float sum = 0.0;
	
	for (int j = 0; j < userSize; j++)
	{
		
		sum += grades[j];
		avg = sum / userSize;
		
	}
	return avg;
}
1
2
3
4
5
6
7
int main ()
{
	const int size = 100;
	int grades[size];
	int userSize;
	float mean (int grades[], int userSize, float avg);


I'm assuming you didn't mean to declare "mean" inside main.

You did not declare "avg" in your code (not that I can see).

I highlighted any changed I made in bold

Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

float avg = 0.0;

	for (int i = 0; i < userSize; i++)
	{
		cout << "Please enter grade: " << endl;
		cin >> grades[i];
		if (cin.fail() || grades[i] < 0 || grades[i] > 100)
		{
			cout << "Please enter a valid grade within";
			cout << "the range of 0 - 100" << endl;
			cin.clear();
		}
		avg = mean (grades,userSize, avg);
	}



Maybe change your function definition and remove the "avg" argument - you aren't using it inside your function for anything.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
float mean (int grades[ ], int userSize)
{
	float sum = 0.0;
	for (int j = 0; j < userSize; j++)
	{
		
		sum += grades[j];
		avg = sum / userSize;
		
	}
	return (sum / userSize);
//could also do float avg = sum / userSize;
//return avg;
}
BINGO, light bulb! now it's beginning to make more sense, thank you!
Well I think I finally have the entire program figured!
Thanks to all for your much needed help, here it is:

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
#include <iostream>

using namespace std;

int main ()
{
	const int size = 100;
	int grades[size];
	int userSize;
	float avg = 0.0;
	float mid = 0.0;

	float mean (int grades[], int userSize, float avg);
	float median (int grades[], int userSize, float mid);
	

	cout << "Enter number of grades to be entered: ";
	cin >> userSize;
	if (userSize > 100)
	{
		cout << "Too many grades" << endl;
		return 0;
	}

	for (int i = 0; i < userSize; i++)
	{
		cout << "Please enter grade: " << endl;
		cin >> grades[i];
		if (cin.fail() || grades[i] < 0 || grades[i] > 100)
		{
			cout << "Please enter a valid grade within";
			cout << "the range of 0 - 100" << endl;
			cin.clear();
		}
		avg = mean (grades,userSize, avg);
		
	}
	mid = median (grades, userSize, mid);
	cout << "The mean of the grades entered is: " << avg << endl;
	cout << "The median of the grades entered is: " << mid << endl;
	cin.ignore(2);
	return 0;
}

float mean (int grades[ ], int userSize, float avg = 0.00)
{
	float sum = 0.0;
	
	for (int j = 0; j < userSize; j++)
	{
		sum += grades[j];		
	}
	return (sum / userSize);
}
float median (int grades[ ], int userSize, float mid = 0.0)
{
	for (int outer = 0; outer < userSize; outer++)
	{
		for (int inner = 0; inner < userSize - 1; inner++)
		{
			if (grades[inner] > grades[inner + 1])
			{
				int temp;
				temp = grades[inner];
				grades[inner] = grades[inner + 1];
				grades[inner + 1] = temp;
			}
		}
	}
	if (userSize % 2 == 0)
	{
		mid = (grades[userSize/2] + (grades[(userSize/2)-1]) /2);
	}
	else
	{
		mid = grades[userSize / 2];
	}
	return mid;
}


Ok, so showing my elevated noob status I have found out something all of you probably already knew... I am NOT finished!

While running some test cases, I have found that I am not clearing or ignoring the invalid inputs for grades. I have looked at many different examples for making this happen and see nothing wrong with the way mine is coded to do so, and yet...

EDIT: I figured it out! I needed to de-index the array:
1
2
3
4
5
6
7
8
9
10
11
for (int i = 0; i < userSize; i++)
	{
		cout << "Please enter grade: " << endl;
		cin >> grades[i];
		if (cin.fail() || grades[i] < 0 || grades[i] > 100)
		{
			cout << "Please enter a valid grade within";
			cout << "the range of 0 - 100" << endl;
			cin.ignore(grades[i]);
			i--;
		}


Also, when the program outputs the array it is adding on "-858993460" many times immediately before outputting the mean and median statements... any thoughts on that?
Last edited on
For the last part, you need to initialize any elements in that array that weren't entered by the user, or you will get garbage values (whatever was in the memory previously).
If your array was 100 big, and the user only entered 50 of them, then the other 50 elements will contain garbage values.
Try int grades[100] = {0};
That will initialize every value in the array to 0;
Well, that made the unwanted output values all 0, however they are still being output. It's not all leftover elements which weren't used either, it is usually only about 4 or 5...
I don't see where the program would output the array, but be sure you aren't going out of bounds of the array (like doing array[105] when your array is only of size 100).
Well I found out why, I had copied a loop I made for an arbitrary question in some old homework and forgot to change the parameters... derrrr!

1
2
3
4
5
6
7
8
9
10
11
12
int i, j;
	for (i = 0, j = 0; i < userSize; i++, j++)
	{
		cout << grades[i] << " ";
		
		if (j == 4)
		{
			j = 0;
			cout << "\n";
		}
		
	}


All is right with the program now!

A million thank you's to you all!!

EDIT: And I see now that I did not show you that after I added it.
Here is the final:

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
96
97
98
99
100
101
102
103
104
105
#include <iostream>
#include <iomanip>

using namespace std;
/*This program allows the user to input some number of grades,
up to a max of 100. The program will then use functions to determine
the mean and median of the grades. Finally the program will output 
the sorted grades from least to greatest. Inputs are the number of
grades, and the grades themselves. Outputs are the mean, median, 
and the grades in sorted form.*/
int main ()
{
	const int size = 100;
	float grades[size] = {0};
	int userSize;
	float avg = 0.0;
	float mid = 0.0;

	float mean (float grades[], int userSize, float avg);
	float median (float grades[], int userSize, float mid);
	

	cout << "Enter number of grades to be entered: ";
	cin >> userSize;
	if (userSize > 100)
	{
		cout << "Too many grades" << endl;
		return 0;
	}

	for (int i = 0; i < userSize; i++)
	{
		cout << "Please enter grade: " << endl;
		cin >> grades[i];
		if (cin.fail() || grades[i] < 0 || grades[i] > 100)
		{
			cout << "Please enter a valid grade within";
			cout << "the range of 0 - 100" << endl;
			cin.clear();
			i--;
			continue;
		}
		avg = mean (grades,userSize, avg);
		
	}
	mid = median (grades, userSize, mid);
	int i, j;
	for (i = 0, j = 0; i < userSize; i++, j++)
	{
		cout << grades[i] << " ";
		
		if (j == 4)
		{
			j = 0;
			cout << "\n";
		}
		
	}
	cout << endl;
	cout << "The mean of the grades entered is: " << fixed << setprecision(2) << avg << endl;
	cout << "The median of the grades entered is: " << fixed << setprecision(2) << mid << endl;
	cin.ignore(2);
	return 0;
}
/*The 'mean' function is to find the middle or mean of the grades array.
The inputs to this function are all of the array values, the size
of the array, and the output is the mean of the array 'grades'*/
float mean (float grades[ ], int userSize, float avg = 0.00)
{
	float sum = 0.0;
	
	for (int j = 0; j < userSize; j++)
	{
		sum += grades[j];		
	}
	return (sum / userSize);
}
/*The 'median' function is sort the array from least to greatest, 
then find the median of the grades. The inputs are the array values, 
the size of the array, and the output is the median value*/
float median (float grades[ ], int userSize, float mid = 0.0)
{
	for (int outer = 0; outer < userSize; outer++)
	{
		for (int inner = 0; inner < userSize - 1; inner++)
		{
			if (grades[inner] > grades[inner + 1])
			{
				float temp;
				temp = grades[inner];
				grades[inner] = grades[inner + 1];
				grades[inner + 1] = temp;
			}
		}
	}
	if (userSize % 2 == 0)
	{
		mid = ((grades[userSize/2] + (grades[(userSize/2)-1])) /2);
	}
	else
	{
		mid = grades[userSize / 2];
	}
	return mid;
}
Last edited on
Topic archived. No new replies allowed.