passing arrays

Hello, I am just learning arrays, and I am having a hard time trying to pass the information between functions. What I am trying to do is have the user put it in 10 grades, then it will take the average of those 10 grades and display the result. I am getting 1 % for some reason, but I feel like I haven't really passed enough information to the averageGrades function. Can someone help?

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
  #include <iostream>
using namespace std;

/*************************************************************
 * a function that prompts the user for ten grades  
**************************************************************/
void getGrades(int listGrades[], int numGrades)
{
    numGrades == 10;
    int total = 0;
    cout << "Enter Grades\n";
    for (int iGrades = 0; iGrades < numGrades; iGrades++)
    {
        cout << "Grade" << iGrades + 1 << ":" << endl;
        cin >> listGrades[iGrades];
        total = total + listGrades[iGrades];
    }
}

/*************************************************************
 * a function that finds the average of the grades and returns
 * the answer
**************************************************************/
void averageGrades(int listGrades[], int numGrades)
{
 int total;
 int averageofGrades;
 averageofGrades = total / numGrades;
}

/**************************************************************
 * main will call the functions above and display the result
***************************************************************/
int main() 
{
    cout << "Average Grade: " << averageGrades << "%" << endl; 

	return 0;
}
1
2
3
4
5
6
7
8
9
10
 In function 'void getGrades(int*, int)': 
9:15: warning: statement has no effect [-Wunused-value] 
 In function 'void averageGrades(int*, int)': 
27:6: warning: variable 'averageofGrades' set but not used [-Wunused-but-set-variable] 
 At global scope: 
24:35: warning: unused parameter 'listGrades' [-Wunused-parameter]
 In function 'int main()': 
36:34: warning: the address of 'void averageGrades(int*, int)' will always evaluate as 'true' [-Waddress]
 In function 'void averageGrades(int*, int)': 
28:37: warning: 'total' is used uninitialized in this function [-Wuninitialized]


I am getting 1 % for some reason

On line 36, the code is trying to print the memory address of averageGrades (which relates to the warning abve about the average always evaluating to 'true''). The function would be called with averageGrades(listGrades, numGrades).

I don't see that the getGrades function is called anywhere. From the comments it looks like you should be calling this in main, before trying to calculate the average.

The total that's calculated at line 16 doesn't go anywhere. And total at line 28 hasn't been initialized before it's used.
so just a question I want to pass the total to the averageGrades function, how would I do that? and are my parameters correct for my functions above main? I want to make sure it is being passed correctly, like I said I'm new to arrays, and they have a bit of a different syntax then just normal variables.
numGrades == 10;
This is a statement and does nothing.

1
2
3
4
5
6
void averageGrades(int listGrades[], int numGrades)
{
 int total;
 int averageofGrades;
 averageofGrades = total / numGrades;
}

numGrades is passed by value, so whatever you do to it in this function won't affect the original. I'd recommend returning the average here. Beware of integer division.

1
2
3
4
5
6
int main() 
{
    cout << "Average Grade: " << averageGrades << "%" << endl; 

	return 0;
}

Including all the points noted above, you need to make an array here and pass it to getGrades(). You also need to call averageGrades().

http://www.cplusplus.com/doc/tutorial/functions/
Last edited on
I think I'd calculate the total in the averageGrades function instead of in the getGrades function.
I think I was able to calculate it, but I can't seem to figure out how to break into 3 functions, computeAverage, getGrades, and main. I feel like it's just easier to leave it main, but I just want to see what I need to do to pass it through.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
    int n = 10;
    int i;
    int grades[10], sum=0, average;

    for(i = 0; i < n; ++i)
    {
        cout << i + 1 << ". Enter number: ";
        cin >> grades[i];
        sum += grades[i];
    }

    average = sum / n;
    cout << "Average = " << average;

    return 0;
}
Different ways this could be done, this is just an example. getGrades could handle the input. averageGrades could handle the whole calculation - adding up each grade in the array and dividing by the number of grades, returning the average to main.

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
#include <iostream>

void getGrades(int grades[], int num){
    
    for(int i = 0; i < num; ++i){
        std::cout << i + 1 << ". Enter grade: ";
        std::cin >> grades[i];
    }    
}

double averageGrade(int grades[], int num){
    
    double sum = 0.0;
    
    for(int i = 0; i < num; ++i){
        sum += grades[i];
    }
    
    return sum/num;    
}

int main(){
    
    const int numGrades = 10;
    int grades[numGrades];

    getGrades(grades, numGrades);

    std::cout << "\nThe average grade was " << averageGrade(grades, numGrades);

    return 0;
}
Oh wow thanks, just have to call it correctly, I feel like a noob.
Topic archived. No new replies allowed.