Problem with array program that displays average, highest and lowest.

hello. I seem to be having a problem with my assignment. I am writing a program in which you enter scores for 20 different students and then the program displays the the highest, the lowest, and the average. I seem to be having a problem in my get highest that tells me that SIZE is undefined. I also have a problem in my get lowest loop telling me that my function is incompatible. my code is as follows. If anyone can help me, thank you!

#include <iostream>
using namespace std;

int getHighest(int [], int);
int getLowest(int [], int);
double getAverage(int [], int);
int count;
int numbers;

int main()
{
const int SIZE = 20;
int scores[SIZE];
double average;
int biggest, smallest;

for(int i=0; i<SIZE; i++)
{
cout << "Enter score for student: " << i+1 << ": " ;
cin >> scores[i];
while(scores[i] > 100 || scores[i] < 0)
{
cout << "Invalid score. Enter value between 0 and 100" << endl;
cin >> scores[i];
}

}

average = getAverage(scores, SIZE);
cout << average << endl;
biggest = getHighest(scores, SIZE);
cout << biggest << endl;
lowest = getLowest(scores, SIZE);
cout << lowest << endl;


return 0;
}


//get maximum value

int getHighest( int total[], int i)

{

int biggest;
for(i = 0; i < SIZE; i++)
{
if (total[i] >biggest)
biggest = total[i];
}
return biggest;
}
my get highest that tells me that SIZE is undefined


That's because SIZE is undefined in getHighest. It is passed as a parameter, but you named the parameter 'i' in the function, not SIZE.

I also have a problem in my get lowest loop telling me that my function is incompatible.


Huh? Incompatible with what? Can you post the actual error message?

Also I don't see your getLowest function anywhere. I only see the prototype for it.
Sorry, seems like I have forgotten to include a good portion of my code. I have tweaked it and got rid of the SIZE.

#include <iostream>
using namespace std;

int getHighest(int [], int);
int getLowest(int [], int);
double getAverage(int [], int);
int count;
int numbers;

int main()
{
const int SIZE = 20;
int scores[SIZE];
double average;
int biggest, smallest;

for(int i=0; i<SIZE; i++)
{
cout << "Enter score for student: " << i+1 << ": " ;
cin >> scores[i];
while(scores[i] > 100 || scores[i] < 0)
{
cout << "Invalid score. Enter value between 0 and 100" << endl;
cin >> scores[i];
}

}

average = getAverage(scores, SIZE);
cout << average << endl;
biggest = getHighest(scores, SIZE);
cout << biggest << endl;
lowest = getLowest(scores, SIZE);
cout << lowest << endl;


return 0;
}


//get maximum value

int getHighest( int total[], int s)

{

int biggest;

for(int i = 0; i < s; i++)
{
if (total[i] >biggest)
biggest = total[i];
}
return biggest;
}

//get min value

int getLowest = (int total[], int s)

{
int lowest
int lowest = 0;
for (int i = 0; i < s; i++)
{
if (total[i] < lowest)
lowest = total[i];
}
return lowest;
}


double getAverage(int ar[], int size)
{
double total= 0, avg;
for(int i=0; i<size; i++)
total += ar[i];

avg = total/size;
return avg;

}
Last edited on
you initialized lowest twice and didn't close the first with a semi-colon.
thank you for your help rjcup3. I now have

#include <iostream>
using namespace std;

int getHighest(int [], int);
int getSmallest(int [], int);
double getAverage(int [], int);
int count;
int numbers;

int main()
{
const int SIZE = 20;
int scores[SIZE];
double average;
int biggest, smallest;

for(int i=0; i<SIZE; i++)
{
cout << "Enter score for student: " << i+1 << ": " ;
cin >> scores[i];
while(scores[i] > 100 || scores[i] < 0)
{
cout << "Invalid score. Enter value between 0 and 100" << endl;
cin >> scores[i];
}

}

average = getAverage(scores, SIZE);
cout << average << endl;
biggest = getHighest(scores, SIZE);
cout << biggest << endl;
smallest = getSmallest(scores, SIZE);
cout << smallest << endl;


return 0;
}


//get maximum value

int getHighest( int total[], int s)

{

int biggest;

for(int i = 0; i < s; i++)
{
if (total[i] >biggest)
biggest = total[i];
}
return biggest;
}

//get min value

int getSmallest (int total[], int s)

{
int smallest;

for(int i = 0; i < s; i++)
{
if (total[i] < smallest)
smallest = total[i];
}
return smallest;
}


double getAverage(int ar[], int size)
{
double total= 0, avg;
for(int i=0; i<size; i++)
total += ar[i];

avg = total/size;
return avg;

}




My program compiles, but I have uninitialized local variables, "biggest" and "smallest" I am working towards fixing those, but if anyone can provide any input that would be very helpful
Please use code tags. Your code is hard to read without them.

[code]put your code here[/code]

Anyway this is the source of your problem:

1
2
3
4
5
6
7
8
9
10
11
int getHighest( int total[], int s)
{
  int biggest;   // you never give 'biggest' a value

  for(int i = 0; i < s; i++)
  {
    if (total[i] >biggest)  // so how can you compare against it here?  What are you comparing to?
      biggest = total[i];
  }
  return biggest;
}


You have the same problem with smallest in your other function.
Last edited on
thank you Disch for letting me now about the code tags

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
 

//get maximum value

int getHighest( int total[], int s)

{
	
	int biggest = 0;
	
	for(int i = 0; i < s; i++)
	{
		if (total[i] >biggest)
			biggest = total[i];
	}
	return biggest;
}

//get min value

int getSmallest (int total[], int s)

{
	int smallest = 0;

    for(int i = 0; i < s; i++) 
{ 
   if (total[i] < smallest) 
      smallest = total[i]; 
}
   return smallest;
}




However I am now return 0 as my smallest value. Seems like a have an error in logic, since every score is being compared to 0, perhaps i should initialize 0 to a higher number?
Last edited on
However I am now return 0 as my smallest value.


Well why do you think that is? ;P
Last edited on
yes thank You Disch lol. I changed the zero to a higher number and now have my code working as stated in the assignment

Thank you all for your help. this is my first experience with cplusplus.com and it was a good one. I look forward to learning more from you all in the future.
Happy to help =)
Topic archived. No new replies allowed.