test scores(avg, highest, lowest...)

This is a discussion on test scores(avg, highest, lowest...) within the C++ Programming forums, part of the General Programming Boards category; this prog keeps saying grades needs to be initialized, but it shouldnt....it deals with arrays Code: // This program will ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    6

    uninitialized array?

    this prog keeps saying grades needs to be initialized, but it shouldnt....it deals with arrays

    Code:
    // This program will read in a group of test scores( positive integers from 1 to 100)
    // from the keyboard and then calculates and outputs  the average score
    // as well as the highest and lowest score. There will be a maximum of 100 scores.
    
    
    #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.
    
    	// Read the values into the array
    	pos = 0;
    	cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;
    	cin  >> grades[pos];
    	
    	//findAverage ([], size);
    
    	while (grades[pos] != -99)
    	{
    
            // Fill in the code to read the grades: 4 scores
    		cout << "enter test score number " << (pos + 1) << ":";
    		cin >> grades[pos];
    	}
    
    	numberOfGrades = grades[pos];  // Fill blank with appropriate identifier
    
    	// 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, size);
    	cout << endl << "The highest grade is " << highestGrade << endl;
    
    	// Fill in the call to the function that calculates lowest grade
    
    	lowestGrade = findLowest(grades, size);
        // Fill in code to write the lowest to the screen
    	cout << endl << "The highest grade is " << lowestGrade << endl;
    
    	return 0;
    }
    
    //****************************************************************************
    //                                 findAverage
    //
    // task:          This function receives an array of integers and its size.
    //                It finds and returns the average of the numbers in the array
    // data in:       array of floating point numbers
    // data returned: avarage of the numbers in the array
    //
    //****************************************************************************
    
    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
              
    }
    
    //****************************************************************************
    //                                 findHighest
    //
    // task:          This function receives an array of integers and its size.
    //                It finds and returns the highest value of the numbers in
    //                the array
    // data in:       array of floating point numbers
    // data returned: highest value of the numbers in the array
    //
    //****************************************************************************
    
    int   findHighest (int array[], int& size)
    {
       // Fill in the code for this function
    	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;
    }
    
    
    //****************************************************************************
    //                                 findLowest
    //
    // task:          This function receives an array of integers and its size.
    //                It finds and returns the lowest value of the numbers in 
    //                the array
    // data in:       array of floating point numbers
    // data returned: lowest value of the numbers in the array
    //
    //****************************************************************************
    
    int   findLowest(int array[], int& size)
    {
       // Fill in the code for this function
    	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;
    
    }
    Last edited by taj777; 04-08-2010 at 09:21 PM.

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    38
    Watch what your code is doing and you can see why it's tweaking out. You should always try and trace what's happening, as each line of code runs, to spot where you've gone wrong.

    Code:
    // This program will read in a group of test scores( positive integers from 1 to 100)
    // from the keyboard and then calculates and outputs  the average score
    // as well as the highest and lowest score. There will be a maximum of 100 scores.
    
    
    #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];		// == ???
        int  numberOfGrades; 	// == ???
        int pos; 				// == ???
    	int size;				// == ???
    	float avgOfGrades;		// == ???
    	int highestGrade;		// == ???
    	int lowestGrade;		// == ???
    
    	// Read the values into the array
    	// pos == 0, grades[0] == ???
    	pos = 0; 
    	cout << "Please input a grade from 1 to 100, (or -99 to stop)" << endl;
    	// Fake input: 1
    	cin  >> grades[pos];
    	// pos == 0, grades[0] == 1
    
    	// 1: pos == 0, grades[0] == 1, grades[0] != -99
    	// 2: pos == 0, grades[0] == 75, grades[0] != -99
    	// 3: pos == 0, grades[0] == 84, grades[0] != -99
    	// 4: pos == 0, grades[0] == 99, grades[0] != -99
    	// 5: pos == 0, grades[0] == 100, grades[0] != -99
    	// 6: pos == 0, grades[0] == -99, grades[0] == -99 --->
    	while (grades[pos] != -99)
    	{
    		// 1: (pos + 1) == 1, grades[1] == ???
    		// 2: (pos + 1) == 1, grades[1] == ???
    		// 3: (pos + 1) == 1, grades[1] == ???
    		// 4: (pos + 1) == 1, grades[1] == ???
    		// 5: (pos + 1) == 1, grades[1] == ???
    		cout << "enter test score number " << (pos + 1) << ":";
    		// 1: Fake input 75
    		// 2: Fake input 84
    		// 3: Fake input 99
    		// 4: Fake input 100
    		// 5: Fake input -99
    		cin >> grades[pos];
    		// 1: pos == 0, grades[0] == 75
    		// 2: pos == 0, grades[0] == 84
    		// 3: pos == 0, grades[0] == 99
    		// 4: pos == 0, grades[0] == 100
    		// 5: pos == 0, grades[0] == -99
    	}
    
    	// --->
    	// pos == 0, grades[0] == -99, numberOfGrades == -99
    	numberOfGrades = grades[pos];  // Fill blank with appropriate identifier
    
    	// call to the function to find average
    	// avgOfGrades == ???
    	// avgOfGrades == findAverage(grades, -99) --->
    	avgOfGrades = findAverage(grades, numberOfGrades);
    	// --->
    	// avgOfGrades == 0
    	cout << endl << "The average of all the grades is " << avgOfGrades << endl;
    
    	//  Fill in the call to the function that calculates highest grade
    	// highestGrade == ???
    	// highestGrade == findHighest(grades, -99) --->
    	highestGrade = findHighest(grades, size);
    	// --->
    	// highestGrade == ???
    	cout << endl << "The highest grade is " << highestGrade << endl;
    
    	// Fill in the call to the function that calculates lowest grade
    	// lowestGrade == ???
    	// lowestGrade == findLowest(grades, -99) --->
    	lowestGrade = findLowest(grades, size);
        // Fill in code to write the lowest to the screen
        // --->
        // lowestGrade == ???
    	cout << endl << "The highest grade is " << lowestGrade << endl;
    
    	return 0; // ---> (0)
    }
    
    //****************************************************************************
    //                                 findAverage
    //
    // task:          This function receives an array of integers and its size.
    //                It finds and returns the average of the numbers in the array
    // data in:       array of floating point numbers
    // data returned: avarage of the numbers in the array
    //
    //****************************************************************************
    
    float findAverage (int array[], int size)
    {  
    	// --->
    	float sum = 0;   // holds the sum of all the numbers
    
    	// for (pos = 0, pos < -99; pos++) --->
        for (int pos = 0; pos < size; pos++)
    		sum = sum + array[pos];
    
    	// --->
    	// sum == 0, size == -99
        return (sum / size);  // --->
              
    }
    
    //****************************************************************************
    //                                 findHighest
    //
    // task:          This function receives an array of integers and its size.
    //                It finds and returns the highest value of the numbers in
    //                the array
    // data in:       array of floating point numbers
    // data returned: highest value of the numbers in the array
    //
    //****************************************************************************
    
    int   findHighest (int array[], int& size)
    {
       // Fill in the code for this function
       // --->
       // array == grades, size == -99, SIZE == 100
    	const int SIZE=100;
    	int highest;			// == ???
    	int grades[SIZE];		// == ???
    	// grades[0] == ???, highest == ???
    	highest = grades[0];	
    	
    	// for (pos == 1, pos < 100; pos++)
    	for (int pos = 1; pos < SIZE; pos++)
    	{
    		// grades[pos] == ???, highest == ???
    	   if (grades[pos] > highest)
    		// highest == ???
    		  highest = grades[pos];
    		  // etc...
    	}
    	
    	// highest == ???
    	return highest;	// --->
    }
    
    
    //****************************************************************************
    //                                 findLowest
    //
    // task:          This function receives an array of integers and its size.
    //                It finds and returns the lowest value of the numbers in 
    //                the array
    // data in:       array of floating point numbers
    // data returned: lowest value of the numbers in the array
    //
    //****************************************************************************
    
    int   findLowest(int array[], int& size)
    {
    	// --->
       // Fill in the code for this function
    	const int SIZE=100;
    	int lowest;			// == ???
    	int grades[SIZE];	// == ???	
    	// grades[0] == ???, lowest == ???
    	lowest = grades[0];
    	
    	// for (pos == 1; pos < 100; pos++)
    	for (int pos = 1; pos < SIZE; pos++)
    	{
    		// grades[pos] == ???, lowest == ???
    	   if (grades[pos] < lowest)
    			// grades[pos] == ???, lowest == ???
    		  lowest = grades[pos];
    		  // etc...
    	}
    	
    	// lowest == ???
    	return lowest; // --->
    
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting lowest and highest score from array
    By sjalesho in forum C Programming
    Replies: 6
    Last Post: 03-01-2011, 06:24 PM
  2. HELP: Exclude the highest and lowest number...
    By Ocin101 in forum C Programming
    Replies: 12
    Last Post: 07-21-2009, 02:51 AM
  3. Replies: 3
    Last Post: 02-22-2009, 02:54 PM
  4. Trying to sort numbers from lowest to highest
    By jw232 in forum C++ Programming
    Replies: 21
    Last Post: 01-21-2008, 04:03 PM
  5. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 01:55 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21