Home > Software engineering >  My computeAverage function is not returning value c
My computeAverage function is not returning value c

Time:09-25

The problem I am facing is my computeAverage() function is not calculating the average of marks.

// computeAverage receives the array with the test scores, and its size,
// as paramaters, computes the average and returns the average value.
// Uses the local variable average to store temporary and final values.
// Uses a for iteration control structure to add all values in the array,
// and store the sum in average. ONLY 1 variable declared in this method.

int computeAverage(const int anArray[], const int arraySize)
{
    // declare the local integer variable average and initialize to 0
    int average = 0;
    // create a for iteration control structure to add all values in the array
    for (int i = 0; i < arraySize; i  )
    {
        average  = anArray[i];
    }
    // the intermediate results are stored in average
    average = average / arraySize;

    // return the average value
    return average;
}


I have used this computeAverage(&theScores[arraySize], arraySize) to call the function

CodePudding user response:

I have used this computeAverage(&theScores[arraySize], arraySize) to call the function

Because you need to simply invoke it as

result = computeAverage(&theScores[0], arraySize);

Or more generally:

result = computeAverage(theScores, arraySize);

As you have it now, you are passing the set of elements past the end of your array, not the actual array start.

  • Related