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.
#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.
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;
}
elseif (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.