Hey i need help with my little homework my teacher gave me today


So i wrote this program you kinda see what i am trying to do, is there a better way to make this program? Im fairly new so i dont know all the functions I know till like For loops but the rest im not familiar.. Thanks guyz

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
  //Program for telling which grade the student got in their avarage note.
#include <iostream>
using namespace std;
int main()
{
    cout <<"Enter your midterm note  > " << flush;

    int vize; //midterm means Vize in my language...
    cin >> vize;

    cout <<"Enter your finals note > " << flush;
    int fin;// fin for finals.
    cin >> fin;

    int top = vize*0.4 + fin*0.6+0.5;

    cout << "Avarage > "<< top << endl;

    if (top > 90)
    {

        cout <<"Congratz you passed with AA" << endl;
    }

    else if (top > 80 && top < 90)
    {

        cout <<"Congratz you passed with AB" << endl;
    }

    else if (top > 70 && top < 80)
    {

        cout <<"Congratz you passed with BB" << endl;
    }

    else if (top > 60 && top < 70)
    {

        cout <<"Congratz you passed with BC" << endl;
    }

    else if (top > 50 && top < 60)
    {

        cout <<"Congratz you passed with CC" << endl;
    }

    else if (top > 40 && top < 50)
    {

        cout <<"Congratz you passed with DC" << endl;
    }
    else if (top > 30 && top < 40)
    {

        cout <<"Congratz you passed with DD" << endl;
    }
    if (top < 30)
    {

        cout <<"Sorry, you failed" << endl;
    }


    return 0;
}


edit: format
Last edited on
Well you need to be a little more careful with your logic, see this snippet:

1
2
3
4
5
6
7
    if (top > 90)
    {

        cout <<"Congratz you passed with AA" << endl;
    }

    else if (top > 80 && top < 90)


What happens if top is equal to 90?

By the way with the logic arranged as it is you really don't need the second comparison in each branch.
1
2
3
4
5
6
7
8
if(top > 90)
{
     // Do whatever for an 'A'.
}
else if(top > 80)
{
    // Do whatever for a 'B'.


Next look at this snippet:

1
2
3
4
5
6
7
    else if (top > 30 && top < 40)
    {

        cout <<"Congratz you passed with DD" << endl;
    }
    if (top < 30)
    {

That last if() should probably just be a simple else.


1
2
3
4
5
6
7
    else if (top > 30)
    {

        cout <<"Congratz you passed with DD" << endl;
    }
    else
    {


Oh thanks for the reply but this program is just a demo for what i am trying! What i am asking is can i make it shorter and get the same result.
Oh and i didnt understand why i dont need the second comparisons in the else ifs i didnt understand why it works :o

sorry if those are stupid questions :(
So you don't want it to work properly as well?



When i put second comparisons it still works fine. The code i wrote in the beginning works fine. I just didnt understand why we dont need the second comparisons.
There is certainly a way to make it compacter.
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
#include <iostream>
#include <string>
using namespace std;
int main()
{
    //midterms
    int vize;
    cout << "Enter your midterm note  > " << flush;
    cin >> vize;

    //finals
    int fin;
    cout << "Enter your finals note > " << flush;
    cin >> fin;

    //average
    int top = vize*0.4 + fin*0.6+0.5;
    cout << "Avarage > " << top << endl;

    //grade
    int ind = -3 + ((top - 1) / 10);
    string mark[] = {"DD", "DC", "CC", "BC", "BB", "AB", "AA"};
    if (ind >= 0) {
        cout << "Congratz you passed with " << mark[ind] << endl;;
    } else {
        cout << "Sorry, you failed" << endl;
    }
    
    return 0;
}
Last edited on
The code i wrote in the beginning works fine.

Really?

jlb wrote:
What happens if top is equal to 90?

Did you pay any attention to that? Did you test that? What is the answer?

I just didnt understand why we dont need the second comparisons.

At line 25, you don't don't need to make sure top is less than the minimum value for an AA grade. If execution reaches this line, we already know it must be lower than that value. There's no need to test for it.
Last edited on
The code i wrote in the beginning works fine. I just didnt understand why we dont need the second comparisons.

Look at this snippet:

1
2
3
4
5
6
7
8
    if (top > 90)
    {

        cout <<"Congratz you passed with AA" << endl;
    }

    else if (top > 80 && top < 90) 


In that else if() you already know that the value is less than 90 because of the preceding if() statement. You will only reach that else if() if the value is less than 90.

By the way the above code fails if the number is exactly equal to 90, because you never actually test if the number is exactly 90. But if you remove the second comparison from the else if() it will actually test 90.

The if() statement values of 91, 92, 93, ... would be valid (remember greater than).
The else if() (as written) tests for 81, 82, 83, ..., 88, and 89 ( greater than 80, and less than 90).

But when you only test with one comparison as suggested. Get the following:

The if() statement values of 91, 92, 93, ... would be valid (remember greater than).
The else if() (as suggested) tests for 81, 82, 83, ..., 88, 89 and 90 ( greater than 80).

Last edited on
Topic archived. No new replies allowed.