954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

getting my code for highest and lowest value in a numeric array to work

I am having problems figuring out what I did wrong to my highest, lowest and average for grades entered by a user. I searched for help but have not found any. Any hint in what I did wrong would be greatly appreciated.

#include <iostream>
#include <iomanip>
using namespace std;

typedef int GradeType[100]; // declares a new data type:
							// an integer array of 100 elements
float findAverage (const GradeType, int); // finds average of all grades
int findHighest (const GradeType, int); // finds highest of all grades
int findLowest (const GradeType, int); // finds lowest of all grades


int main()
{

	GradeType grades; // the array holding the grades.
	int numberOfGrades; // the number of grades read.
	int pos; // index to the array.
	float avgOfGrades; // contains the average of the grades.
	int highestGrade; // contains the highest grade.
	int lowestGrade; // contains the lowest grade.

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

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

		   numberOfGrades = pos;

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

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

		   highestGrade = findHighest(grades, numberOfGrades);

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

		   lowestGrade = findLowest(grades, numberOfGrades);


		   cout<< endl << "The Lowest grade is " << lowestGrade << endl;


		   system ("pause");
           
           return 0;
}

float findAverage (const GradeType 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 (const GradeType array, int size)
{
	float highest = 0;    // holds the highest number
		for (int pos = 0; pos < size; pos++)
		{
			if (array[pos] > highest)
				highest = array[pos];
		}
		 return highest;


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

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


}
blsnls
Newbie Poster
4 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

Garbage in, garbage out. Before you spend a lot of time analyzing your functions to see where the logic error is in calculating the lowest, highest, and average, loop through your array and make sure it's storing what you think it is.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 
Garbage in, garbage out. Before you spend a lot of time analyzing your functions to see where the logic error is in calculating the lowest, highest, and average, loop through your array and make sure it's storing what you think it is.

i.e the problem first occurs somewhere in here :

while (grades[pos] != -99)
			{
				grades[pos] = numberOfGrades; // store grade read in to next array location
				pos ++; // increment array index
				cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;
				cin >> grades[pos];
			}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

Ok, I figured out my first problem, Thanks for sending me in the right direction, now I am working on the other problems. I know they have to be simple just like the last one.

blsnls
Newbie Poster
4 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

I figured it out, now my program works like it should. Thanks for pushing me in the right direction.

blsnls
Newbie Poster
4 posts since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You