Robert Furger
Robert Furger

Reputation: 53

Passing array by reference C++

I am having trouble getting the getAverage function to get the correct average. What am I doing wrong? I cannot use pointers. Here is the original question:

A program that will prompt for grades and calculate the average. The grades will be stored in an array called GradesInput that is defined in main. The maximum number of grades that can be stored in the array is 100. The variable holding the actual number of grades the user entered should be defined in main and should be called GradeCount. The program will have two functions in addition to main. The 1st function should be called from main and should keep prompting the user for grades till a sentinel is entered, capture those grades and store them in GradesInput. This function should also update the variable GradeCount in main, GradeCount should have been passed by reference. The 2nd function should be called from main and should find the average of the grades in GradesInput. The average should be returned to and printed out from main.

//Lab7D This program will get the grades of students and average
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

//The 1st function should be called from main and should keep prompting the user for grades till a sentinel is entered,
//capture those grades and store them in GradesInput.
//This function should also update the variable GradeCount in main, GradeCount should have been passed by reference.
void store(int arr[], int &GradeCount) //for taking input by user pass by reference
{
    int i = 0;

    while (arr[i] != -1)
    {
        cout << "Enter grade : ";
        cin >> arr[i];
        GradeCount++;
    }
}

//The 2nd function should be called from main and should find the average of the grades in GradesInput.
//he average should be returned to and printed out from main.
double getAverage(int arr[], int size)
{
    int i;
    double avg, sum = 0;

    for (i = 0; i < size; i++)
    {
        sum += arr[i];
    }
    avg = sum / size;

    return avg;
}

//The variable holding the actual number of grades the user entered should be defined in main and should be called GradeCount
int main()
{
    int GradeCount = 0;
    int grades[100];
    store(grades, GradeCount);

    cout << "Average : " << getAverage(grades, GradeCount) << endl;
}

Upvotes: 0

Views: 210

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

The function store has undefined behavior because the array was not yet initialized. So the condition in the loop

while (arr[i]!=-1)

is invalid. Moreover the variable i is not being changed in the loop.

The function can be defined the following way/

void store( int arr[],int &GradeCount ) //for taking input by user pass by reference
{
    const int Sentinel = -1;

    GradeCount = 0;

    bool success = true;

    do
    {
        cout << "Enter grade : ";

        int value = Sentinel;

        if ( ( success = ( cin >> value && value != Sentinel ) ) )
        {
            arr[GradeCount++] = value;
        } 
    } while ( success ); 
}

But it would be much better to declare and define the function the following way

size_t store( int arr[], size_t n, int sentinel = -1 )
{
    size_t GradeCount = 0;

    if ( n != 0 )
    {    
        bool success = true;;

        do
        {
            cout << "Enter grade : ";

            int value = sentinel;

            if ( ( success = ( cin >> value && value != sentinel ) ) )
            {
                arr[GradeCount++] = value;
            }
        } while ( success && GradeCount < n ); 
    }

    return GradeCount;
}

And call it like

int grades[100];

size_t GradeCount = store( grades, 100 );

The function getAverage should be declared and defined the following way

double getAverage( const int arr[], size_t n )
{
    double sum = 0.0;

    for ( size_t i = 0; i < n; i++ )
    {
        sum += arr[i];
    }

    return n == 0 ? 0.0 : sum / n;
}

Upvotes: 1

user13297721
user13297721

Reputation:

Your problem resides in the store() function. You're comparing the value held in a[i] to -1 before even initializing it. An Alternative is to do the following:

int store( int arr[] )
{
    int i = 0;
    int grade = 0 ;
    while ( true )
    {
        std::cout<< "Enter grade: " ;
        std::cin >> grade ;
        if ( grade == -1 )
            break ;

        arr[ i ] = grade ;
        ++i ;
    }
    return i ;
}

The value returned by store() will be the GradeCount you can use to average your grades later.

Upvotes: 0

Related Questions