Math

Hello . I'm in school right now but i'm trying to learn c++ , and i really want to make something for myself and maybe my friends . What i want it to do is basically arithmetic mean but with a twist .
Example .
Grade 1 = 5
Grade 2 = 5
Grade 3 = 5
Grade 4 = 5
Grade 5 = 5
Exam = 6

5+5+5+5+5=25 /// 25/5=5 /// 5*3=15 /// 15+6=21 /// 21/4=5.25 . Final answer 5.25
What i want it to do is grade 1 + grade 2 + ... + grade 5 = x
x=x/(The number of grades)
x*3+exam/4=r
cout r . What i can't do is have different amouts of grades for example . i have 5 grades but he has 7 and she has 4 . But i can only make for a certain amount . This is not homework is just something i want to do to and i have no idea how . I'm really a beginner so if you don't want to tell me the exact thing help me understand how to make it . Thank you so much <3 .
Last edited on
Something like this?
Basically, instead of looping 5 times, you can get input from the user and then loop N times.

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
// Example program
#include <iostream>
#include <string>

int main()
{
    int num_grades = 0;
    std::cout <<  "Enter number of grades: ";
    std::cin >> num_grades; // get number from user input
    
    double grades_sum = 0;
    for (int i = 0; i < num_grades; i++) // loop [num_grades] times, i goes from 0 to num_grades-1
    {
        double grade;
        std::cout << "Grade " << i+1 << " = ";
        std::cin >> grade; // get user input for each individual grade
        
        grades_sum += grade;
    }
    
    std::cout << "Exam = ";
    double exam_grade;
    std::cin >> exam_grade;
    
    double final_grade = grades_sum / num_grades; // x=x/(The number of grades)
    final_grade = final_grade*3 + exam_grade / 4; // x*3+exam/4=r
    std::cout << final_grade << std::endl;

}



Enter number of grades: 5
Grade 1 = 5
Grade 2 = 5
Grade 3 = 5
Grade 4 = 5
Grade 5 = 5
Exam = 6
16.5
Last edited on
that's good but there's a problem . The answer should've been 5.25 . 5+5+5+5+5=25 ,
25/5=5 , 5*3=15 , 15+6=21 , 21/4=5.25
That's not how order of operations works (I was just going by your initial comment).
You would want:

(x*3 + exam)/4, not x*3+exam/4.
Add a pair of parenthesis to encapsulate the operation before the division.
i'm a bit stupid lol . Sorry for that . I Was sure i made a mistake somewhere i just didn't notice . i thought it was obvious but it isnt . yeah what i want is (x*3+exam)/4
Well i fixed it . Thank you so much <3
Topic archived. No new replies allowed.