Problem with code output

Hello,

I am trying to write a code that will calculate a person's average grade by first asking the grade received and then asking the max grade for the assignment. It works okay for about 5 or 6 grades, but it gives weird answers for 10 or more total grades. Answers like "1.363573e-09".

Can anyone tell me what's wrong with 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
  #include<iostream>
using namespace std;

int main()
{
    int numGrades;
    double grade, sumGrades, sumAllGrades, gradeAverage, assignMax;
    float grades[numGrades];
    char answer;

    cout << "This program will calculate your class average." << endl;

    cout << "Is there a point total under 'My Grades' on Blackboard? ('y' for yes, 'n' for no)" << endl;
    cin >> answer;

    if (answer == 'N' || answer == 'n')
    {
        cout << "How many grades are there?" << endl;
        cin >> numGrades;

        for (int i = 0; i < numGrades; i++)
        {
            cout << "Enter a grade: ";
            cin >> grade;

            grades[i] = grade;

            cout << "What is the maximum grade for this assignment?" << endl;
            cin >> assignMax;

            sumAllGrades += assignMax;
        }

        for (int i = 0; i < numGrades; i++)
        {
            sumGrades += grades[i];
        }

        gradeAverage = (sumGrades/sumAllGrades)*100;
        cout << "The average of your grades is " << gradeAverage << ". \n";

    }

    else
    {
        cout << "What is the first number? (bold number, upper half of the fraction) " << endl;
        cin >> sumGrades;

        cout << "What is the second number? (number in the lower half of the fraction) " << endl;
        cin >> sumAllGrades;

        gradeAverage = sumGrades / sumAllGrades;

        cout << "The average of your grades is " << gradeAverage << ". \n";
    }
    return 0;
}

Last edited on
change grades to a double instead of a float.
I did that and this keeps happening.

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
This program will calculate your class average.
Is there a point total under 'My Grades' on Blackboard? ('y' for yes, 'n' for no)
n
How many grades are there?
10
Enter a grade: 100
What is the maximum grade for this assignment?
100
Enter a grade: 18
What is the maximum grade for this assignment?
20
Enter a grade: 77
What is the maximum grade for this assignment?
100
Enter a grade: 0
What is the maximum grade for this assignment?
100
Enter a grade: 100
What is the maximum grade for this assignment?
100
Enter a grade: 0
What is the maximum grade for this assignment?
100
Enter a grade: 100
What is the maximum grade for this assignment?
100
Enter a grade: 100
What is the maximum grade for this assignment?
100
Enter a grade: 100
What is the maximum grade for this assignment?
100
Enter a grade: 100
What is the maximum grade for this assignment?
100
Enter a grade: 100
What is the maximum grade for this assignment?
100
Enter a grade: 15
What is the maximum grade for this assignment?
20
Enter a grade: 100
Segmentation fault (core dumped)
Last edited on
hmm sorry working off my phone here so cant try anything. You can always change it back to a float and if you use:

float grades[x] = roundf(grades[x])

I believe that line should bring you to two decimal places and fix your error, when I get home ill try this on my pc and see what I come up with.
Please, have a look at the following code (I don't know your data, so it's hard to check if it's correct or not):
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
#include <iostream>

int main()
{
    std::cout << "This program will calculate your class average.\n"
              << "Is there a point total under 'My Grades' on Blackboard?\n"
                 "('y' for yes, 'n' for no) ";
    char answer = '\0';
    std::cin >> answer;

    double grades[1000] = {};
    if (answer == 'N' || answer == 'n')
    {
        std::cout << "How many grades are there? ";
        int numGrades = 0;
        std::cin >> numGrades;

        double sumGrades = 0.0, sumAllGrades = 0.0;
        for (int i = 0; i < numGrades; i++)
        {
            std::cout << "Enter a grade: ";
            std::cin >> grades[i];
            sumGrades += grades[i];

            std::cout << "What is the maximum grade for this assignment? ";
            double assignMax = 0.0;
            std::cin >> assignMax;

            sumAllGrades += assignMax;
        }

        double gradeAverage = (sumGrades / sumAllGrades) * 100;
        std::cout << "The average of your grades is " << gradeAverage << ". \n";

    }

    else
    {
        std::cout << "What is the first number? (bold number, upper half of "
                     "the fraction) ";
        double sumGrades = 0.0;
        std::cin >> sumGrades;

        std::cout << "What is the second number? (number in the lower half "
                     "of the fraction) ";
        double sumAllGrades = 0.0;
        std::cin >> sumAllGrades;

        double gradeAverage = sumGrades / sumAllGrades;  // * 100 ?

        std::cout << "The average of your grades is " << gradeAverage << ". \n";
    }
    return 0;
}

Output:
This program will calculate your class average.
Is there a point total under 'My Grades' on Blackboard?
('y' for yes, 'n' for no) n
How many grades are there? 3
Enter a grade: 13.666
What is the maximum grade for this assignment? 14
Enter a grade: 51.51
What is the maximum grade for this assignment? 100
Enter a grade: 7
What is the maximum grade for this assignment? 77
The average of your grades is 37.7885.

This program will calculate your class average.
Is there a point total under 'My Grades' on Blackboard?
('y' for yes, 'n' for no) y
What is the first number? (bold number, upper half of the fraction) 55
What is the second number? (number in the lower half of the fraction) 444
The average of your grades is 0.123874.

If you don't initialize a local variable, there could be any value inside it.
Topic archived. No new replies allowed.