validating marks

Were am i going wrong with the if else statement.It is not validating the marks wether they are above 60% and if the candidaate qualifies for the bsc comp science degree
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
# include <iostream>
# include <cassert>
using namespace std;

int main()


{ const int exammarks =3;
   int mark[exammarks];
    int i;

    for (int i=0;i<=2;i++)
    {
        cout <<"enter your marks for physical science, English and mathematics:";
        cin>>mark[i];
        assert(mark [i]<100);
    }

if (mark[i]>=60)
    cout<<"you have qualified for the BSc in computer science!"<<endl;

else
    cout <<"you donot qualify for the BSC in computer science";


return 0;
}
You have two variables called i. The one that you use on line 19 hasn't been set.

Surely you would require all marks above the boundary to get that degree?

BTW, this is a terribly ironic question!
so you are saying for line 19 i should type

i>=60

sorry for the ironic question i am new to programming and i am trying to learn it didnt mean to mock you
or should i say

mark[exammarks]>=60
mark[exammarks] translates to mark[3] which is out of bounds. So don't do that.
Your array consists of 0, 1, 2 indices, no 3rd index.

Also you want to find the average to decide whether a person qualifies. What's the average? The sum of all elements in the array, that is mark[0]+mark[1]+mark[2] divided by the number of data, which is 3.

mark[0]+mark[1]+mark[2] / exammarks is the average (you can do it more fancily with a loop but they serve the same purpose). If that is above 60, the student qualifies.

Also, assert(mark [i]<100); should be assert(mark [i]<=100); otherwise nobody would be allowed to get a 100! :0
RahRah wrote:
or should i say mark[exammarks]>=60

Let's be a little harsher. Any decent university would impose a minimum mark in all the "previous requirements":
1
2
3
4
5
   if ( *min_element( mark, mark + exammarks ) >= 60 )
      cout<<"You have qualified to start a BSc in computer science!\n";

   else
      cout <<"You don't qualify to start a BSc in computer science\n";

You will need a
#include <algorithm>



You should move your prompt
cout <<"enter your marks for physical science, English and mathematics:";
to BEFORE the main loop, or you will be asked three times.


Last edited on
Thank you so much for the help it is greatly appreciated
i dont want the program to calculate anything i just want it to read the marks if all three are greater than 60% then it should display you have qualfied for the bsc in comp science...

please explain This part if ( *min_element( mark, mark + exammarks ) >= 60 )


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
# include <iostream>
# include <cassert>
using namespace std;

int main()


{ const int exammarks =2;
   int mark[exammarks];
    int i;

 {

   for (int i=0;i<=2;i++)
    {
        cout <<"enter your marks for physical science, English and mathematics:";
        cin>>mark[i];
        assert(mark[i]<=100);
    }

if (mark[i]>=60&&mark[i]<=100)
    cout<<"you have qualified for the BSc in computer science!"<<endl;

else
    cout <<"you donot qualify for the BSC in computer science";

 }
return 0;
}
RahRah wrote:
explain This part if ( *min_element( mark, mark + exammarks ) >= 60 )

I'm guessing that you want to require that ALL marks are greater than or equal to 60.
Logically, this is equivalent to the MINIMUM mark being greater than or equal to 60.
*min_element( mark, mark + exammarks ) produces the minimum of mark[0], mark[1], ... , mark[exammarks-1]. See
http://www.cplusplus.com/reference/algorithm/min_element/
min_element can be used for any iterable array or container.


What you have just written:
1
2
3
4
if (mark[i]>=60&&mark[i]<=100)
    cout<<"you have qualified for the BSc in computer science!"<<endl;
else
    cout <<"you donot qualify for the BSC in computer science";

makes no sense because:
(a) You are at most considering one mark, not all 3.
(b) Variable i is not defined at this point, so the index is meaningless.

You can either:
(1) find the minimum mark by some method similar to that suggested; or
(2) determine whether any of the marks is below 60 in the same loop in which you read them in.


You haven't addressed the other points in my previous post. You have also added some completely unnecessary braces. Your indentation is all over the place.
Last edited on
Topic archived. No new replies allowed.