Using functions with arrays

Pages: 12
This topic is pretty much my sore spot. I can figure out how to create the function, but then calling it trips me up as well as any calculations needed inside the function definition. I am writing a program that has an array of 10 double values. That array then has to call functions for the maximum, minimum and average of those numbers. I have this so far, though I'm far from finished. I've tried doing the calculations outside of main (), but I keep getting 0 or 1 as my output. Can anyone help with giving me a nudge in the right direction? Thank you and excuse my awful spacing.

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

const int NUM_GRADES = 10; // Array size

//Function Prototypes for reading grades and calculations
void findMaxGrade(double[]);
void findMinGrade(double[]);
void findAverageGrade(double[]);

int main()
{

    double grade [NUM_GRADES]; //The Array has 10 elements

    //Get grade input from user. NOTE: Given permission to do it this way
    for (int counter = 0; counter < NUM_GRADES; counter++)
    {
        cout << " Enter grades for 10 students: " << endl;
        cin >> grade[NUM_GRADES];

    }


    //Store values in the Array
    for (int counter = 0; counter < NUM_GRADES ; counter++)
    {
        grade[counter] = counter;
    }

    //Call our functions to find Max, Min and Average
    if (findMaxGrade, NUM_GRADES)

        cout << " The maximum grade is: " << findMaxGrade << endl;


    if (findMinGrade, NUM_GRADES)
    {
        cout << " The minimum grade is: " << findMinGrade << endl;
    }

    if (findAverageGrade, NUM_GRADES)
    {
        cout << " The average grade was: " << findAverageGrade << endl;
    }


    return 0;
}

//Function definitions
void findMaxGrade (double grade[])
{
    
}
Last edited on
Have you looked at http://www.cplusplus.com/doc/tutorial/arrays/
The latter half of it has section "Arrays as parameters".

You could check http://www.cplusplus.com/doc/tutorial/functions/ too, for "how to call a function".


PS. Please use code tags when posting code. See http://www.cplusplus.com/articles/jEywvCM9/
Thank you. I'm still having issues, but hopefully I can get it to work.
what do your functions/calculations look like ?
first of all line 24. why do you have NUM_GRADES there? The for loop needs to iterate. NUM_GRADES does not change right? the value of counter changes right (becaose of the for loop)? So you need to swap out NUM_GRADES for counter right there so the user can store up to 10 values.

also lines 29-33 are useless. you dont need that there. the previous for loop on 24 stores the values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 double grade [NUM_GRADES]; //The Array has 10 elements

    //Get grade input from user. NOTE: Given permission to do it this way

    cout << " Enter grades for 10 students: " << endl;


    for (int counter = 0; counter < NUM_GRADES; counter++)
    {

        cin >> grade[counter];


    }


    //Store values in the Array
    // deleted no use of that 
Last edited on
Thanks!

As for my calculations, I'm trying to figure them out as we speak. They are terrible, but this is the "template" I have so far:

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
void findMaxGrade (double[], int NUM_GRADES)
{
    double maximum = 0;
    for (int counter = 0; counter < NUM_GRADES-1; counter++)
    {
        if (maximum > counter)
        {
            cout << " The maximum grade is " << findMaxGrade << endl;
        }
    }
}
void findMinGrade (double[], int NUM_GRADES)
{
    double minimum = 0;
    for (int counter = 0; counter < NUM_GRADES-1; counter++)
    {
        if (minimum < minimum)
        {
            cout << " The minimum grade is " << findMinGrade << endl;
        }

    }
}
void findAverageGrade (double[], int NUM_GRADES)
{
    double average = 0;
    for (int counter = 0; counter < NUM_GRADES-1; counter++)
    {
        if (average += counter/ NUM_GRADES)
        {
            cout << "The average grade is " << findAverageGrade << endl;
        }


    }
Last edited on
okay lets focus on findMaxGrade first.

you have a couple issues in line 11. that was done incorrectly. fix that asap.

now lets look at lines 36-46. Why did you use those if statements? Im not sure what you are trying to do with those. The proper way to call your maxgrade func is

findMaxGrade(grade, NUM_GRADES);

what does calling a function do? It initiates/activates the function right? its going to take those two array/value with it to go start working on that function. you gotta bring some numbers/values with ya if you are going to that function . so thats why you have those parameters. youre taking that array grade and the value of NUM_GRADES with you into the findMax function so you can do your calculations.

lets work on fixing this first, then we will have to fix the logic errors you have in your function as well. there is a lot that is wrong in this code. lets go step by step.
Last edited on
Yes, I had already changed those things but didn't edit my post. I'm sorry about that. I now have:

1
2
3
4
5
6
//Call our functions to find Max, Min and Average
    findMaxGrade (grade, NUM_GRADES);

    findMinGrade (grade, NUM_GRADES);

    findAverageGrade (grade, NUM_GRADES);
okay great job. now lets focus on the next issue. calling the function. is there any reasons to have those if statements there?

on line 38 you are using findMaxValue as a variable. and you know its not lol because its a function that you made. you have that function typed as a void function.

void functions dont return values. they simply just execute and thats it. they finish. so in this case lets just say the computer is reading this code. it goes line by line. you got all your values enter by the user and you got an array that is now full of grades filled up by the user.

so now at this point you want the program to give you and display the maxGrade right? so we gotta call our maxgrade function. how do we do that?

findMaxGrade(grade, NUM_GRADES);

lets get that all fixed up and then show me what you got. then we have to fix your function

thats all, no dataTypes of the function or values/arrays needed. now the program is going to run the findMaxGrade function. so it hops down over to that func.



Last edited on
Ok, so far:
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

const int NUM_GRADES = 10; // Array size


//Function Prototypes for reading grades and calculations
void findMaxGrade (double[], int);
void findMinGrade (double[], int);
void findAverageGrade (double[], int);

int main()
{
    double grade [NUM_GRADES]; //The Array has 10 elements

    cout << " Enter grades for 10 students: " << endl;

    //Get grade input from user. NOTE: Given permission to do it this way
    for (int counter = 0; counter < NUM_GRADES; counter++)
    {
        cin >> grade[counter];
    }


   //Call our functions to find Max, Min and Average
    findMaxGrade (grade, NUM_GRADES);
    findMinGrade (grade, NUM_GRADES);
    findAverageGrade (grade, NUM_GRADES);



    return 0;
}
Last edited on
Line 12 13 14 you already corrected earlier

Now let's create the func definition, also excuse my abbreviations im on my phone

DataTypeOfFunc ( DataType Var/ArrName) {

}

While you're making the func def title you need two parameters in this case, which ones and Why ? Do you understand how parameters work?

findMaxGrade (grade, NUM_GRADES);

This is what you called out earlier in your main func. So you sent two values with this func along with the program while activating that function? Make sense ? While you're making your func title you always need to have the same amount of parameters. For simplicity lets use the same names of the arr and that other var
Last edited on
The parameter is telling us that we need an array of doubles with an integer as the argument.

1
2
3
4
void findMaxGrade (double num[], int)
{

}
close, the parameter is saying we are using an array of doubles and integer. that is correct.

1
2
3
4
void findMaxGrade (double num[], int)
{

}


basically whats going on here is you state the function type is void, so it does not return any value or anything. Of course it still executes/runs but it wont send back any value or value changes back to the int main.

findMaxGrade this is just the name

(double num[], int NUM_GRADES)

this is declaring your array/variables that you will now use in your function alright but youre going to need the function to grab the values of that array and variable from the main func. you need to assign your variables and arrays to play/use with them right? So just to let you know these guys have already been assigned values. this was done in the main function earlier by you. because when we look at where you called the function. in the original function call out the array num[] is being copied over to your function definition's parameters (double num[], int NUM_GRADES). and so is the variable NUM_GRADES that is being copied over into that function parameter int NUM_GRADES



1
2
3
4
void findMaxGrade (double num[], int NUM_GRADES)
{

}


does the function prototype ( the one before the main func), calling of the function, how the parameters/arguments work and the function definition all make sense? This is super important because you will use functions a lot more later in your class and in other programs you make in the future. a lot. if everything makes sense we can tackle the logic in your function now or you can ask questions
Last edited on
That makes complete sense as to why my calculations were not printing. For some reason, though, I'm getting an error on the name of the function when I write it like this:

1
2
3
4
findMaxGrade (double num[], int NUM_GRADES)
{
    
}


Is it because I previously used "void?"
you need to have the data type there, we can use void thats fine. so keep void there.

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

const int NUM_GRADES = 10; // Array size


//Function Prototypes for reading grades and calculations
//Call our functions to find Max, Min and Average
void findMaxGrade (double grade[],  int NUM_GRADES);

void findMinGrade (double grade[],  int NUM_GRADES);

void findAverageGrade (double grade[], int NUM_GRADES);

int main()
{
    double grade [NUM_GRADES]; //The Array has 10 elements

    cout << " Enter grades for 10 students: " << endl;

    //Get grade input from user. NOTE: Given permission to do it this way
    for (int counter = 0; counter < NUM_GRADES; counter++)
    {
        cin >> grade[counter];
    }


   //Call our functions to find Max, Min and Average
    findMaxGrade (grade, NUM_GRADES);




    return 0;
}

void findMaxGrade (double grade[],  int NUM_GRADES)
{
    
 cout << "my func works";
}
Last edited on
Look at the first example in http://www.cplusplus.com/doc/tutorial/functions/
* What does the function 'addition' do?
* How does main() use the 'addition'?
Yes, all of it makes sense. Basically we set up a prototype that calls to the definition of the function. The definitions basically perform the actions, but only after the function is called in main. The prototype and definitions pass information to one another, and main calls what they're trying to pass through. The parameter declares the function and the argument is getting passed to the function.
so now we just need to fix your logic in there right?

1
2
3
4
5
6
7
8
9
10
11
void findMaxGrade (double grade[], int NUM_GRADES)
{
    double maximum = 0;
    for (int counter = 0; counter < NUM_GRADES-1; counter++)
    {
        if (maximum > counter)
        {
            cout << " The maximum grade is " << findMaxGrade << endl;
        }
    }
}


line 4 that -1 does not need to be there because you have < which it'll stop at 8 with the way you have it. you want it to stop at 9. so remove that -1.
line 6 yes you need a if statement, though why are you comparing maximum (which you have set to 0) to check if its larger than counter (which is also 0.. then it'll be 1, 2, 3, 4...9,10 throughout the for loop)
you want the if statement to compare the grade array and each element dont you? because you want the highest grade. make some improvements on that and i'll help you get to the answer.

you want the if statement to compare the each element in the array.
Last edited on
Yes, the one thing I can't get right. I either get a bunch of 1s, zeroes, or all of the numbers as maxGrade.

I tried the one above as well as this one:

1
2
3
4
5
6
7
8
9
10
11
void findMaxGrade (double grade[], int NUM_GRADES)
{
    double maximum = 0;
    for (int counter = 0; counter < NUM_GRADES-1; counter++)
    {
        if (grade[counter] > maximum)
        {
            cout << " The maximum grade is " << grade[counter]<< endl;
        }
    }
}


Last edited on
oh yeah the one above this one isnt correct, i just copy and pasted what you had. here is your code but its a step towards what you need it to be. tho it is NOT correct yet

1
2
3
4
5
6
7
8
9
10
11
12
void findMaxGrade (double grade[], int NUM_GRADES)
{
    double maximum = 0;
    for (int counter = 0; counter < NUM_GRADES-1; counter++)
    {
        if (grade[counter] > grade[counter])
        {

            cout << " The maximum grade is " << maximum << endl;
        }
    }
}


line 6 you need something else here, its comparing the same element in the grade array. well we want it to compare but not the same one. how can we make it so it compares the next element instead?

line 8, well you did make the maximum variable earlier didnt you a couple lines ago? so lets use it. if grades[counter] is the bigger value thru that iteration then why not assign maximum that value of grades[counter]

line 9 well this is just going to print out a cout statement every single time the if statement goes thru. we dont want that. just just want it once, so lets take it out the if statement and for loop. but we still need that in the function for sure.
Last edited on
Pages: 12