Function for finding mean average of an array

Hello everyone. I need a little bit of help here. I am writing a slightly long assignment for my intro level c++ course, which involves manipulating arrays in different ways using functions. So far I'm on a good track on all of them but I got stuck on finding the average of my array. I will post the entire code so you guys can see it but I am particularly having issue with the function FindMean().


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

int main()
{
static const int SIZE = 10;
static int testScores[SIZE] = { 80,90,85,65,55,70,75,50,80,95 };
int count;
int highest, lowest;
static int average;

// Function prototypes
void SortTest(int[], int);
int FindMean(int[], int);

// The code below will display the unsorted list of test scores.
cout << "The unsorted scores for each test are: " << endl;
cout << testScores[0];
cout << " " << testScores[1];
cout << " " << testScores[2];
cout << " " << testScores[3];
cout << " " << testScores[4];
cout << " " << testScores[5];
cout << " " << testScores[6];
cout << " " << testScores[7];
cout << " " << testScores[8];
cout << " " << testScores[9] << endl;

// Now we will call a function to sort our array in ascending order.
SortTest(testScores, SIZE);

// Now we will find the highest and lowest numbers.

highest = testScores[0];
for (count = 1; count < SIZE; count++)
{
if (testScores[count] > highest)
highest = testScores[count];
}
cout << "\n"; // Used solely for spacing.
cout << "\nThe highest test score is: " << highest << endl; // When the above code is done processing this line will display the highest number.

lowest = testScores[0];
for (count = 1; count < SIZE; count++)
{
if (testScores[count] < lowest)
lowest = testScores[count];
}
cout << "\nThe lowest test score is: " << lowest << endl; // When the above code is done processing this line will display the lowest number.

// Now we will call a function to find the mean average of all the test scores.
FindMean(testScores, SIZE);

return 0;
}

// This function will use a technique called a bubble sort to sort our array.
void SortTest(int testScores[], int SIZE)
{
int size = 10;
int i, j, temp;



for (i = 0; i < size; i++);

for (i = 1; i < size; i++)
{
for (j = 0; j < (size - i); j++)
if (testScores[j] > testScores[j + 1])
{
temp = testScores[j];
testScores[j] = testScores[j + 1];
testScores[j + 1] = temp;
}
}

cout << "\nThe scores for each exam after sorting in ascending order are:" << endl;
for (i = 0; i < size; i++)
cout << " " << testScores[i];
return;
}

// This function will calculate the mean average of all test scores for us.
int FindMean(int testScores[], int SIZE)
{
double average;
double sum = 0.0;
for (int x = 0; x < SIZE; x++)
{
sum += testScores[x];
average = sum / SIZE;
}
return(average);
}
closed account (48T7M4Gy)
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
98
99
100
101
102
103
104
105
106
107
#include<iostream>
#include<string>
using namespace std;

int main()
{
    static const int SIZE = 10;
    static int testScores[SIZE] = { 80,90,85,65,55,70,75,50,80,95 };
    int count;
    int highest, lowest;
    //static int average; // <-- delete not used
    
    // Function prototypes
    void SortTest(int[], int);
    double FindMean(int[], int); // <-- double see below
    
    // The code below will display the unsorted list of test scores.
    cout << "The unsorted scores for each test are: " << endl;
    cout << testScores[0];
    cout << " " << testScores[1];
    cout << " " << testScores[2];
    cout << " " << testScores[3];
    cout << " " << testScores[4];
    cout << " " << testScores[5];
    cout << " " << testScores[6];
    cout << " " << testScores[7];
    cout << " " << testScores[8];
    cout << " " << testScores[9] << endl;
    
    // Now we will call a function to sort our array in ascending order.
    SortTest(testScores, SIZE);
    
    // Now we will find the highest and lowest numbers.
    
    highest = testScores[0];
    for (count = 1; count < SIZE; count++)
    {
        if (testScores[count] > highest)
            highest = testScores[count];
    }
    cout << "\n"; // Used solely for spacing.
    cout << "\nThe highest test score is: " << highest << endl;
    // When the above code is done processing this line will
    // display the highest number.
    
    lowest = testScores[0];
    for (count = 1; count < SIZE; count++)
    {
        if (testScores[count] < lowest)
            lowest = testScores[count];
    }
    cout << "\nThe lowest test score is: " << lowest << endl;
    // When the above code is done processing this line will
    // display the lowest number.
    
    // Now we will call a function to find the mean average of all
    // the test scores.
    
    cout << "Average = " << FindMean(testScores, SIZE) << endl; // <-- one way
    
    double average = FindMean(testScores, SIZE); // <-- another way
    cout << "Alternative average = " << average << endl;
    
    return 0;
}

// This function will use a technique called a bubble sort to sort our array.
void SortTest(int testScores[], int SIZE)
{
    int size = 10;
    int i, j, temp;
    
    
    
    for (i = 0; i < size; i++);
    
    for (i = 1; i < size; i++)
    {
        for (j = 0; j < (size - i); j++)
            if (testScores[j] > testScores[j + 1])
            {
                temp = testScores[j];
                testScores[j] = testScores[j + 1];
                testScores[j + 1] = temp;
            }
    }
    
    cout << "\nThe scores for each exam after sorting in ascending order are:"
    << endl;
    for (i = 0; i < size; i++)
        cout << " " << testScores[i];
    return;
}

// This function will calculate the mean average of all test scores for us.
double FindMean(int testScores[], int SIZE) // <--- should be double
                                            //if you return a double
{
    double average = 0.0; // <-- should initialize variables
    double sum = 0.0;
    for (int x = 0; x < SIZE; x++)
    {
        sum += testScores[x];
        // average = sum / SIZE; // <-- delete this line
    }
    return average; // <-- no brackets
}
The unsorted scores for each test are: 
80 90 85 65 55 70 75 50 80 95

The scores for each exam after sorting in ascending order are:
 50 55 65 70 75 80 80 85 90 95

The highest test score is: 95

The lowest test score is: 50
Average = 74.5
Alternative average = 74.5
Program ended with exit code: 0
Last edited on
You calculate an average by adding all the elements and then dividing by the number of elements.
Learn C++ here: https://hackr.io/tutorials/learn-c-c-plus-plus
var total = 0;
for(var i = 0; i < grades.length; i++) {
total += grades[i];
}
var avg = total / grades.length;
The reason you got 68 as your result is because in your loop, you keep overwriting your average, so the final value will be the result of your last calculation. And your division and multiplication by grades.length cancel each other out.

Update: added ; in last line of code example to avoid error.
closed account (48T7M4Gy)
Line 104 above should be deleted - luckily it didn't matter for the outcome :)
Topic archived. No new replies allowed.