uninitialized array?

this prog keeps saying grades needs to be initialized, but it shouldnt....help!
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
#include <iostream>
using namespace std;



float findAverage (int [], int);  // finds average of all grades
int   findHighest (int [], int&);  // finds highest of all grades
int   findLowest  (int [], int&);  // finds lowest of all grades

int main()

{
    const int SIZE=100;
    int  grades[SIZE];	// the array holding the grades.
    int  numberOfGrades; // the number of grades read.
    int pos; // index to the array.....LIKE COUNT
    int size;
    float avgOfGrades;// contains the average of the grades.
    int highestGrade;// contains the highest grade.
    int lowestGrade;// contains the lowest grade.


	pos = 0;
	cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;
	cin  >> grades[pos];

	while (grades[pos] != -99)
	{

       
		cout << "enter test score number " << (pos + 1) << ":";
		cin >> grades[pos];
	}

	numberOfGrades = grades[pos];  // Fill blank with appropriate identifier


	avgOfGrades = findAverage(grades, numberOfGrades);
	cout << endl << "The average of all the grades is " << avgOfGrades << endl;

	
	
	highestGrade = findHighest(grades, size);
	cout << endl << "The highest grade is " << highestGrade << endl;

	

	lowestGrade = findLowest(grades, size);
	cout << endl << "The highest grade is " << lowestGrade << endl;

	return 0;
}


float findAverage (int array[], int size)
{  
    float sum = 0;   // holds the sum of all the numbers

    for (int pos = 0; pos < size; pos++)
		sum = sum + array[pos];

    return (sum / size);  //returns the average
          
}



int   findHighest (int array[], int& size)
{
  
	const int SIZE=100;
	int highest;
	int grades[SIZE];
	highest = grades[0];
	for (int pos = 1; pos < SIZE; pos++)
	{
	   if (grades[pos] > highest)
		  highest = grades[pos];
	}
	return highest;
}




int   findLowest(int array[], int& size)
{
 
	const int SIZE=100;
	int lowest;
	int grades[SIZE];
	lowest = grades[0];
	for (int pos = 1; pos < SIZE; pos++)
	{
	   if (grades[pos] < lowest)
		  lowest = grades[pos];
	}
	return lowest;

}

int grades[SIZE]; this is not allowed.

if you want dynamic array you need to use new
and don't forget to delete it.
1
2
3
4
5
6
int *grades;
grades = new int[SIZE]

....

delete[] grades; // when you are done using grades 


since you are using const int SIZE = 100 . just do
int grades[100];
Last edited on
this is not allowed.
Yes, it is, because SIZE is a const int.

You're passing the array that's called 'grades' in main() to findHighest() and findLowest(), but in those functions you named the parameter as 'array'. That's not the problem. The problem is that you then define more arrays local to those functions and call them 'grades', and use them as if they were the one that you passed from main(), which it isn't.

Try running this program and you should understand:
1
2
3
4
5
6
7
8
9
void f(int Bob[],int size){
    for (int a=0;a<size;a++)
        std::cout <<Bob[a]<<' ';
}

int main(){
    int Alice[]={1,4,3,5,2};
    f(Alice,5);
}
there's more. the initialization of your grades in main needs to be modified a bit...

1
2
3
4
5
6
7
8
9
10
11
12
13
        pos = 0;
	cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;
	cin  >> grades[pos];

	while (grades[pos] != -99)
	{

       
		cout << "enter test score number " << (pos + 1) << ":";
		cin >> grades[pos];
	}

	numberOfGrades = grades[pos];  // Fill blank with appropriate identifier 

I want you to think:

-> what values does pos have to take during this procedure?

-> what values does pos actually take during this procedure?

-> should you stop the loop only when -99 is entered or also when the number of entries has reached the capacity of the array?

-> let's say that the user has given 10 entries. what would be better? to set the remaining (unused) elements of the array to some value that doesn't affect the output of the find<Average/Lowest/Highest> functions OR to store the number of entries somewhere so that these functions know what data they should operate on? (It seems to me that you are trying to do something like the latter with numberOfGrades but neither do you set the value of this variable correctly nor do you declare it in a scope that would make it available to your find functions)

EDIT: oops, forget the last one... just saw that you pass it as a parameter in you functions hahaha. Still you don't set its value correctly...

EDIT2: hmmm... you pass numberOfGrades in findAverage but not in the other two... Why is size necessary? And what value do you assign to it before you pass it in your functions?

1
2
3
avgOfGrades = findAverage(grades, numberOfGrades);
highestGrade = findHighest(grades, size);
lowestGrade = findLowest(grades, size);

Also you use the parameter correctly only in your first function (findAverage). My guess is that that's how all of your functions worked initially but because the results weren't what you expected to be (due to the bad initialization of the array) you modified the code in your functions trying to fix a problem located somewhere else... (that is in the initialization of the array)
Last edited on
Topic archived. No new replies allowed.