Home > Net >  Why is the If statements inside the for loop not working except the first condition whichever it sat
Why is the If statements inside the for loop not working except the first condition whichever it sat

Time:10-23

I want the code to execute the average marks of all students simultaneously and also determine the grade of the student after printing each average marks.

#include <stdio.h>
        
int main()
{
    int stud_rlno[8], phy_marks[8], chem_marks[8], 
        bio_marks[8], CS_marks[8], average;
    int i;
    for(int i = 0; i < 1;   i)
    {
        printf("Enter the roll number of student %d: ", i);
        scanf("%d", &stud_rlno[i]);
        printf("Enter the physics marks of student %d: ", i);
        scanf("%d", &phy_marks[i]);
        printf("Enter the chemistry marks of student %d: ", i);
        scanf("%d", &chem_marks[i]);
        printf("Enter the biology marks of student %d: ", i);
        scanf("%d", &bio_marks[i]);
        printf("Enter the CS marks of student %d: ", i);
        scanf("%d", &CS_marks[I]);
    }
    for(int i = 0; i <= 1; i  )
    {  
        average = phy_marks[i]   chem_marks[i]   bio_marks[i]   CS_marks[i];
        printf("The average of student is %d \n", average / 4);
            if(average > 90)
        {
            printf("Grade A \n");
        }   
        else if(average <= 90 && average > 82)
        {
            printf("Grade B \n");
        }
        else if(average <= 82 && average > 75)
        {
            printf("Grade C \n");
        }
        else if(average <= 75 && average > 65)
        {
            printf("Grade D \n");
        }
        else if(average <= 65)
        {
            printf("Grade E \n");
        }
        else
        {
            printf("Fail");
        }
    }
}

The if statements inside the for loop don't work, Why are the If statements inside the for loop not working except for the first condition whichever it satisfies? I tried with many variations but it still doesn't work.

CodePudding user response:

Check your for loop. While executing, your program will iterate for only once as you have selected condition to be for (i=0; i<1;i ). I would advise you to take what will be the size of array (no.of students) and set the value of i.

secondly,

sum = phy_marks[i]   chem_marks[i]   bio_marks[i]   CS_marks[i];
average = sum/4;

CodePudding user response:

Your program calculates the average mark for a single student,(for i=0; i<1; i ). This means that it would still work without the loops. As shown below. To use use a loop in calculating the average mark for each student, consider the second part of this answer.

int stud_rlno;
float phy_marks,chem_marks,bio_marks,CS_marks,average;
printf("Enter the roll number of student: ");
scanf("%d", &stud_rlno);
printf("Enter the physics marks of student %d: ",stud_rlno);
scanf("%f", &phy_marks);
printf("Enter the chemistry marks of student %d: ",stud_rlno);
scanf("%f", &chem_marks);
printf("Enter the biology marks of student %d: ",stud_rlno);
scanf("%f", &bio_marks);
printf("Enter the CS marks of student %d: ",stud_rlno);
scanf("%f", &CS_marks);
average=(phy_marks chem_marks bio_marks CS_marks)/4;
printf("The average of student is %.2f.\n", average / 4);
if(average > 90){
    printf("Grade A \n");
}   
else if(average <= 90 && average > 82){
    printf("Grade B \n");
}
else if(average <= 82 && average > 75){
    printf("Grade C \n");
}
else if(average <= 75 && average > 65){
    printf("Grade D \n");
}
else if(average <= 65){
    printf("Grade E \n");
}
else{
    printf("Fail");
}

To calculate the average mark for a given number of students, consider the code below: The program prompts the user to enter the number of students. It then prompts the user to enter marks for all the four subjects, for each student. simultaneously the program prints out the marks, calculates the average and prints out the average and the grade.

#include<stdio.h>
#include<stdlib.h>

int main(){
    char *subject[]={"Physics","Chemistry","Biology","CS"};
    int no_of_students;
    printf("Enter the number of students: ");
    scanf("%d",&no_of_students);
    int i,j;
    float marks[100][4];
    //Marks input for all subjects ,for each students.
    for(i=0; i<no_of_students; i  ){
        printf("For student %d enter: \n",i 1);
        for(j=0; j<4; j  ){
            printf("%s marks: ",subject[j]);
            scanf("%f",&marks[i][j]);
        }
    }
    printf("Student\tPhysics\tChemistry\tBiology\tCS\n");
    float sum,average;
    for(i=0; i<no_of_students; i  ){
        printf("%d\t",i 1);//lists the students.
        sum=0;
        for(j=0; j<4; j  ){
            printf("%.2f\t",marks[i][j]);//prints the marks.
            sum =marks[i][j];//Sums marks for individual student.
        }
        average=sum/4;//calculates the average per student.
        printf("%.2f\t",average);//prints the average.
        //Grades the average mark.
        if(average > 90){
        printf("Grade A \n");
        }   
        else if(average <= 90 && average > 82){
            printf("Grade B \n");
        }
        else if(average <= 82 && average > 75){
            printf("Grade C \n");
        }
        else if(average <= 75 && average > 65){
            printf("Grade D \n");
        }
        else if(average <= 65){
            printf("Grade E \n");
        }
        else{
            printf("Fail");
        }
    }

}
  •  Tags:  
  • c
  • Related