for and if

Hello! I'm practicing to code the finding the passed student with greater than or equals to 75. But it prints all the input grades even the other is less than 75.

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
#include <cstdlib>
#include <stdio.h>

int main()
{
   int scores [6]; 
   int i;
   int pg;
   pg = 75;
   
   printf ("Enter the prelim grades one at a time when prompted.\n");
   
        for (i=1; i<=5; i++)
        {
            printf ("Enter prelim grade of student number: ");
            scanf ("%d",&scores[i]);
            }
        for (i=1; i<=5; i++)
        {
            if (scores[i]> pg){
                            pg = scores[i];
                            }
                            printf ("\nThe total passed student is : ");
                            printf ("%d\n",scores[i]);
            }
    system("PAUSE");
    return EXIT_SUCCESS;
}
you want those printf statements at the bottom INSIDE your if statement i'm guessing?
the bad way you've layed out your braces and indentment it's hard to see that.

Also note that arrays are zero-based. So after you've populated the array the first element will always have an uninitialised value in it.
After reading other topics, I just edited the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
   int scores [6]; 
   int i;
   int j;
   int passed =0;
   int failed = 0;
   
   printf ("Enter the prelim grades one at a time when prompted.\n");
   
        for (i=1; i<=5; i++)
        {
            printf ("Enter prelim grade of student number %d: ",i);
            scanf ("%d",&scores[i]);
            if (scores[i]>= 75){
                                scores[i] = ++passed;
            }
            else if (scores[i]<75){
                                scores[i] = ++failed;
            }
                            printf ("The total passed student/s is: %d\n", passed);
                            printf ("The total failed student/s is: %d\n", failed);
            }


The output is right but i'm a little bit confused why after i input the first grade.

Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Enter prelim grade of student numer 1: 75
The total passed student/s is: 1
The total failed student/s is: 0

Enter prelim grade of student numer 1: 70
The total passed student/s is: 1
The total failed student/s is: 1

Enter prelim grade of student numer 1: 70
The total passed student/s is: 1
The total failed student/s is: 2

Enter prelim grade of student numer 1: 70
The total passed student/s is: 1
The total failed student/s is: 3

Enter prelim grade of student numer 1: 70
The total passed student/s is: 1
The total failed student/s is: 4


I want is after i input the grades and then the PRINTF will functioning.
After reading other topics


..just read my post.
I'ts working now! Thank you mutexe!

Just edit this part

1
2
3
4
5
6
if (scores[i]>= 75){
                                scores[i] = ++passed;
            }
            else if (scores[i]<75){
                                scores[i] = ++failed;
            }


to

1
2
3
if (scores[i]>= 75) passed++;
                             else failed++;
            }

that still looks a bit weird.

I meant something like this:

1
2
3
4
5
6
7
8
9

if (scores[i]>= 75)
{
    passed++;
}
else
{
    failed++;
}


Now you can see EXACTLY which lines get called whether your 'if' condition evaluates to true or false.
Last edited on
Topic archived. No new replies allowed.