Function that finds the lowest value in an array returns 0

Hello,

I'm trying find the lowest value in an array, but it just keeps returning 0. The findHighest and findAverage functions return an acceptable value, except for the findLower function. I suspect there's an element in the array with the value of 0, but if that's so then I'm not quite sure how it gets there in the first place. Any insight is appreciated.

Thanks


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
 #include <iostream>
using namespace std;

typedef int GradeType[100];  // declares a new data type: 
                             // an integer array of 100 elements

void getData(GradeType array, int& sizeOfArray);
float findAverage (const GradeType array, int sizeOfArray);  // finds average of all grades
int   findHighest (const GradeType array, int sizeOfArray);  // finds highest of all grades
int   findLowest  (const GradeType array, int sizeOfArray);  // finds lowest of all grades

int main(){
    GradeType  grades;					   // the array holding the grades.
    int  numberOfGrades;				   // the number of grades read.					   

	float avgOfGrades;					   // contains the average of the grades.
	int highestGrade;					   // contains the highest grade.
	int lowestGrade;					   // contains the lowest grade.

	numberOfGrades = 0;  // Fill blank with appropriate identifier

	getData(grades, numberOfGrades);      // Read in the values into the array

	

	// call to the function to find average
	avgOfGrades = findAverage(grades, numberOfGrades);

	cout << endl << "The average of all the grades is " << avgOfGrades << endl;


	//  Fill in the call to the function that calculates highest grade
	highestGrade=findHighest(grades, numberOfGrades);

	cout << endl << "The highest grade is " << highestGrade << endl;

	// Fill in the call to the function that calculates lowest grade
	lowestGrade=findLowest(grades, numberOfGrades);

    // Fill in code to write the lowest to the screen
	cout<<endl<<"The lowest grade is "<<lowestGrade<<endl;

	return 0;
}

void getData(GradeType array, int& sizeOfArray){
	int pos = 0; // // index to the array which starts at 0.
	int grade; // holds each individual grade read in
	cout << "Please input a grade or type -99 to stop: " << endl;
	cin >> grade;

	while (grade != -99)
	{
		array[pos] = grade; // store grade read in to next array location 
		pos ++; // increment array index
		cout << "Please input a grade or type -99 to stop: " << endl;
		cin >> grade;
	}

	sizeOfArray = pos;  // upon exiting the loop, pos holds the
					    // number of grades read in, which is sent
					    // back to the calling function
}

float findAverage (const GradeType array, int sizeOfArray){  
	
	float avg = 0;   // holds the sum of all the numbers
	
    for (int pos = 0; pos < sizeOfArray; pos++){

	    avg+=(array[pos])/sizeOfArray;
	}
    return avg;  //returns the average        
}

int   findHighest (const GradeType array, int sizeOfArray){
	int highest = 0;

	for (int pos = 0; pos<sizeOfArray;pos++){
		if(highest<array[pos]){
			highest=array[pos];
		}
	}
	return highest;
}

int findLowest(const GradeType array, int sizeOfArray){
	int lowest = 0;

	for(int pos = 0; pos<sizeOfArray; pos++){
		if(lowest>array[pos]){
			lowest=array[pos];
		}
	}
	return lowest;
}
Last edited on
Lowest can't start out as zero because nothing in you array is going to be lower than that unless you put negative numbers in. In findLowest set lowest equal INT_MAX, you will need to include <climits>. This way any number you put into the array will be less than or equal to INT_MAX and you will pass the if check and set the lowest value.
Topic archived. No new replies allowed.