Pass or Fail

Good day I've been tasked with an assignment where I am meant to write a program that indicates whether a student has passed or failed after they have made an input of their grades. The student has to input 6 grades and in order to obtain a pass they must have attained 50 percent or more for English and 3 other subjects and if not then they have failed. my first thought at solving this was to create nested if statements for all possible varieties in which the marks could come like below:

1
2
3
4
5
if(a>50)
else if (b>50)
else if (c>50)
else if (d>50)
result=pass


keeping "if(a>50)" as the common statement among the various possible nested if statements. But this seemed a bit tedious and lacking in elegance so if there are any easier solutions to this problems they'd be much appreciated.



Check to see if the user failed English class. If yes, the user failed overall.

Count the number of other classes the user passed.
If it is 3 or more, the user's passed overall.
If it is 2 or less, the user's failed overall.
The simplest way would be to check this while you read the values in:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::cout << "Enter 6 grades, starting with English:\n";
int check;
int temp[6];
for(int i = 0; i < 6; i++)
{
     std::cout << "Enter grade: ";
     std::cin >> temp[i];
     if(temp > 50)
     {
         check++;
     }
}

if(temp[0] > 50 && check >= 4)
{
     std::cout << "You passed!";
}


If you can't use arrays yet.. Assuming "a" is their English class and there are 5 other grades to check:

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
if(a > 50)
{
     int count = 0;
     if (b > 50)
         count++;

     if (c > 50)
         count++;

     if (d > 50)
         count++;

     if (e > 50)
         count++;

     if (f > 50)
         count++;

     if (count >= 3)
          std::cout << "You passed!";

     else
          std::cout << "You Failed...";
}
else
     std::cout << "You Failed...";
1
2
3
4
5
bool hasPassed( int English, int Maths, int Physics, int Geography, int UltimateFrisbee, int ScubaDiving )
{
   if ( English < 50 ) return false;
   return ( Maths >= 50 ) + ( Physics >= 50 ) + ( Geography >= 50 ) + ( UltimateFrisbee >= 50 ) + ( ScubaDiving >= 50 ) >= 3;
}
Topic archived. No new replies allowed.