Using functions with arrays

Pages: 12
I took out the "double maximum" in the above code, and it's not compiling at all. I finish up the cin part and then it says "process finished."

I would like to understand why it would skip this entirely:

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

            cout << " The maximum grade is " <<grade[counter]<< endl;
        }
    }
}
Last edited on
did you see my last post? I think we posted at the same time. theres def a lot of logic errors here still. look at the issues i mentioned. lets address those real quick, and you almost have this done.
Last edited on
You are amazing! I think I've got it:

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

using namespace std;


const int NUM_GRADES = 10; // Array size

//Function Prototypes for reading grades and calculations
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);
    findMinGrade (grade, NUM_GRADES);
    findAverageGrade (grade, NUM_GRADES);
    return 0;
}
void findMaxGrade (double grade[], int NUM_GRADES)
{

    double maximum;
    maximum = grade[0];
    for (int counter = 0; counter < NUM_GRADES; counter++)
    {
        if (grade[counter] > maximum)
        {
          maximum = grade[counter];
        }
    }
    cout << "The maximum grade is " << maximum <<endl;
}

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

    double minimum;
    minimum = grade[0];
    for (int counter = 0; counter < NUM_GRADES; counter++)
    {
        if (grade[counter] < minimum)
        {
            minimum = grade[counter];
        }
    }
    cout << "The maximum grade is " << minimum <<endl;
}

void findAverageGrade (double grade[], int NUM_GRADES)
{
    double average;
    average = grade[0];
    for (int counter = 0; counter < NUM_GRADES; counter++)
    {
        if (grade[counter] += average/NUM_GRADES)
        {
            average = grade[counter];
        }
    }
    cout << "The maximum grade is " << average <<endl;
}
Last edited on
thank you :) yup you got it, think about the logic and exactly whats going on here by the way. once you fully understand it, the next two functions will be cake. you'll most likely see this again in a test
I posted what I have above. For some reason I don't think the average calculations is outputting correctly.
remember to change your cout statements for each function btw

also yeah its not calculating right because the logic is incorrect. what youre doing in the findaverage function is

declaring average as double
average = the first number in the array grade

for loop starting at 0 going til 9 at increments of one

then this is where im seeing the biggest error, your if statement. what you wrote was:

assign grade[counter] to grade[counter] + average/NUM_GRADES
if this all comes back true then iterate (execute) the statements
or if it comes back false then dont.
thats what if statements do.
if (true) then lets do it
if (false) then lets NOT do it
in this case and how you wrote your if statement, it'll always iterate


well thats not whatcha want right?

you just want to add up all the elements in the array and then divide them by NUM_GRADES. thats all

1
2
3
4
5
6
7
8
9
10
11
12

void findAverageGrade (double grade[], int NUM_GRADES)
{
    double average = 0;

    for (int counter = 0; counter < NUM_GRADES; counter++)
    {
       ____________________________;
    }
    _____________________________;
    cout << "The average grade is " << average <<endl;


here you go, you just need two lines of code
Last edited on
Ok, gotcha:

1
2
3
4
5
6
7
8
9
10
11
12
13
void findAverageGrade (double grade[], int NUM_GRADES)
{
    double average;
    int sum = 0;

    for (int counter = 0; counter < NUM_GRADES; counter++)
    {
        sum += grade[counter];

    }
    average = sum/NUM_GRADES;
    cout << "The average grade is " << average <<endl;
}


yup youre correct. you dont even really need the extra sum variable that you created. though the way you did it is fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

void findAverageGrade (double grade[], int NUM_GRADES)
{
    double average = 0;
    

    for (int counter = 0; counter < NUM_GRADES; counter++)
    {
        average += grade[counter];

    }
    average /= NUM_GRADES;
    cout << "The average grade is " << average <<endl;
}
Last edited on
Thank you very much for all of your help. You honestly helped me to understand things that I just wasn't "getting."

no problem, glad to hear that. if you ever have any questions feel free to post up again and message me the link to the thread.
Topic archived. No new replies allowed.
Pages: 12