C programming

Last Edited By Krjb Donovan
Last Updated: Mar 11, 2014 07:50 PM GMT

Question

QUESTION: Student grading system

i've done program for students grading. it require user to input no of student, no of subject to produce grade for each marks.at the end average marks will determine if the student qualify to next semester. It appears to be a lot of error..need your sincere help. I M A Beginner.....

  1. include <stdio.h>

void main(){

    float average,total_mark;
    int noStud, StudID ;
    int mark[2]n, i,x;
    
    printf("\t\t\tGrading System\n\n");
    printf("Enter no. of student: ");
    scanf("%d",&noStud);
    for(noStud=0;noStud< x;noStud++)

printf("Enter number Of Subject\t: "); scanf("%d",&n); for(i=0;i< n;i++) {

printf ("\n\n\t>>Enter students ID:");

       scanf ("%d" , &StudID );
       printf ("\n\t>>Enter marks for %d:\n" , StudID );

printf("\nMark Subject %d\t: ",i+1); scanf("%d",&mark[i]); }


 {


    if ((mark >= 80 && mark <= 100))
    printf ("\tGrade: A\n");
    else if ((mark >= 70 && mark <=79))
    printf ("\tGrade: B\n");
    else if ((mark >=60 && mark <=69))
    printf ("\tGrade: C\n");
    else if ((mark >= 50 && mark <=59))
    printf ("\tGrade: D\n");
    else if ((mark >= 40 && mark <=49))
    printf ("\tGrade: E\n");
    else if ((mark >=0 && mark <=39))
    printf ("\tGrade: F\n");
    
    
    total_mark=mark[i];
    printf ("\nTotal Marks = %.2f\n", total_mark);
    average=total_mark/n;
    printf ("Average Marks = %.2f\n" ,total_mark/noSub);
    if ( average  >= 40)
    printf ("PROCEED TO THE NEXT SEMESTER....\n");
    else if (average <=39)
    printf ("FAIL TO THE NEXT SEMESTER....\n");


    system("pause");
    return 0;


}



ANSWER: Hello Vanan

Yes you have many compiler errors, and some logic errors. When you get a compiler error, you should look at the first error, read it carefully, try to fix it, and then recompile. Often fixing one error will get rid of many compiler messages. The majority of your errors came from comparing mark against a number when mark was an array. You cannot compare an array against a number, you must compare an array element against a number. Anyway, there is no need to make mark an array because you need mark only while deciding the letter grade, and adding it to total_mark. After that, you no longer need the mark, and you can reuse the mark variable for the next subject.

I fixed your program according to what I thought it should do. I have put in comments to explain what I did. Actually, I did too much fixing for you. Much learning comes from fixing a program, so read the program carefully and try to understand why the changes were made.


  1. include <stdio.h>

/* the standard is that main returns int, not void */ int main() {

   float average,total_mark;
   int noStud, StudID;
   int mark; /* you don't need an array of marks because
             the mark will be added to the total, and then will not be needed again
             so you don't need to save all the marks.
             You just need the current mark being worked on.
             */
   int noSubject, i, s;
   printf("\t\t\tGrading System\n\n");
   printf("Enter no. of student: ");
   scanf("%d",&noStud);
   /* this is incorrect -> for(noStud=0;noStud< x;noStud++) it should be as below */
   for(s = 0; s < noStud; s++) /* this is the student loop */
   {
       printf ("\n\n\t>>Enter students ID:");
       scanf ("%d" , &StudID );
       /* For each student you want to collect all the marks, so
       the whole reading of subject marks
       has to be inside the student loop */
       printf("Enter  number Of Subject\t: ");
       scanf("%d",&noSubject);
       total_mark = 0; /* it is important to initialize this to 0 otherwise, it will start at a random value */
       for(i=0; i<noSubject;i++) /* this is the subject loop */
       {
           /*printf ("\n\n\t>>Enter students ID:");
           scanf ("%d" , &StudID );
           You don't want a student ID for each subject, you want it for each student, so I moved it
           up out of the subject loop
           */
           printf ("\n\t>>Enter marks for %d:\n" , StudID );
           printf("\nMark Subject %d\t: ",i+1);
           scanf("%d",&mark);
           /* The majority of your errors came from trying to compare
           mark against a number when mark was declared as an array
           Now that mark is no longer an array, you can do the comparison
           */
           if ((mark >= 80 && mark <= 100))
               printf ("\tGrade: A\n");
           else if ((mark >= 70 && mark <=79))
               printf ("\tGrade: B\n");
           else if ((mark >=60 && mark <=69))
               printf ("\tGrade: C\n");
           else if ((mark >= 50 && mark <=59))
               printf ("\tGrade: D\n");
           else if ((mark >= 40 && mark <=49))
               printf ("\tGrade: E\n");
           else if ((mark >=0 && mark <=39))
               printf ("\tGrade: F\n");
           total_mark += mark; /* add mark to total_mark */
       }
       printf ("\nTotal Marks = %.2f\n", total_mark);
       average=total_mark/noSubject;
       printf ("Average Marks = %.2f\n" ,total_mark/noSubject);
       if ( average  >= 40)
           printf ("PROCEED TO THE NEXT SEMESTER....\n");
       else if (average <=39)
           printf ("FAIL TO THE NEXT SEMESTER....\n");
       //system("pause");
   }
   return 0;

}

---------- FOLLOW-UP ----------

QUESTION: thank you very much for your help.i m gratefull to u.now i understand not only the mistakes in the program but also the use of loop and array function very well.your guidedance are aprreciated. now i need to make sure the input marks is valid.. can i use statement like if / while as below to correct logical error..

int value_input;

valid_input=0 while (valid_input==0){ printf("\nMark Subject %d\t: ",i+1); scanf("%d",&mark); if (mark <0 && mark >100) printf("Invalid mark,pls enter 1-100"); valid_input=1 i tried this and some other way but it doest work..where should this statement should be placed...


ANSWER: Hello Vanan

Your code

int value_input; valid_input=0 while (valid_input==0){ printf("\nMark Subject %d\t: ",i+1); scanf("%d",&mark); if (mark <0 && mark >100) printf("Invalid mark,pls enter 1-100"); valid_input=1

is almost correct. A mark is invalid if it is <0 OR > 100. The code you have considers a mark invalid if it is <0 AND >100. Of course a number cannot be both < 0 AND > 100. The code should look like this:

int valid_input; valid_input=0 while (valid_input==0){

  printf("\nMark Subject %d\t: ",i+1);
  scanf("%d",&mark);
  if (mark <0 || mark >100)
     printf("Invalid mark,pls enter 1-100");
  else
     valid_input=1;

}

I have not run this through a compiler, so there may be syntax errors, but I just want to show you what you need to do.

If you are programming in C, then you will need to put your int valid_input declaration with the other declarations. In C++ you can put it right before it's needed.

I suggest you put the entire thing into a separate function so that you do not clutter up your main program with little loops. Do something like this int GetValidMark(int subjectNumber) {

  int value_input;
  int mark;
  valid_input=0
  while (valid_input==0){
     printf("\nMark Subject %d\t: ", subjectNumber);
     scanf("%d",&mark);
     if (mark <0 || mark >100)
        printf("Invalid mark,pls enter 1-100");
     else
        valid_input=1;
  return mark;

}

Again, this is just to illustrate the idea. I have not checked the code with a compiler.

You will find that if you give scanf bad input, you will run into trouble. For example, if scanf wants a number, but you give a letter, you will get an infinite loop. I will explain how to handle scanf errors tomorrow.


---------- FOLLOW-UP ----------

QUESTION: thank you sir,

below are the program as advised by u..anyway i m confused with while and if statement and it seems does not working. so i tried if statement, it works with error where grade repeated in loop.

another problem is..student ID is repeated for each subject. i try to work on the loop lines but it ended up with infinite loop. it should looks like this: enter mark for id :111 mark 1: mark 2: mark 3:

but it happend like this enter mark for id :111 mark 1: enter mark for id :111 mark 2: enter mark for id :111 mark 3: 'ENTER MARK FOR ID' REPEATED IN LOOP FOR EACH SUBJECT!

I NEED TOO HAVE TO MAKE SURE THERE IS NO SCANF BAD INPUT..WHERE WHEN LETTER GIVEN TO INT INFINITE LOOP HAPPEN..THANK U..

  1. include <stdio.h>


int main() {

  float average,total_mark;
  int noStud, StudID;
  int mark; 
  int noSubject, i, s;
  printf("			Grading System

");

  printf("Enter no. of student: ");
  scanf("%d",&noStud);
  
  for(s = 0; s < noStud; s++) /* this is the student loop */
  {
      printf ("

>>Enter students ID:");

      scanf ("%d" , &StudID );
      
      printf("Enter  number Of Subject	: ");
      scanf("%d",&noSubject);
      total_mark = 0;
      for(i=0; i<noSubject;i++) /* this is the subject loop */
      {
          
          printf ("

>>Enter marks for %d: " , StudID );

          printf("

Mark Subject %d : ",i+1);

          scanf("%d",&mark);

if(mark <0 || mark > 100) {

               printf("You entered wrong mark , enter mark between 1-100.");
               continue;
   } 
            
      for(k=0; k<mark;k++) /* THIS IS WHERE I TRY TO REPEAT THE MARKS FOR SUBJECT IF INVALID MARKS ENTERED */
      {    
          if ((mark >= 80 && mark <= 100))
              printf ("	Grade: A

");

          else if ((mark >= 70 && mark <=79))
              printf ("	Grade: B

");

          else if ((mark >=60 && mark <=69))
              printf ("	Grade: C

");

          else if ((mark >= 50 && mark <=59))
              printf ("	Grade: D

");

          else if ((mark >= 40 && mark <=49))
              printf ("	Grade: E

");

          else if ((mark >=0 && mark <=39))
              printf ("	Grade: F

");

          total_mark += mark; /* add mark to total_mark */
      }
      printf ("

Total Marks = %.2f ", total_mark);

      average=total_mark/noSubject;
      printf ("Average Marks = %.2f

" ,total_mark/noSubject);

      if ( average  >= 40)
          printf ("PROCEED TO THE NEXT SEMESTER....

");

      else if (average <=39)
          printf ("FAIL TO THE NEXT SEMESTER....

");

      system("pause");
  }
  return 0;

}


Answer

Hello Vanan

You can read my tutorial about console input in C here https://sites.google.com/site/zlatkoscodingtopics/console-input-with-c

That tutorial has a working program which shows a correct way to read numbers with scanf. It will prevent the infinite loop that I warned you about in my earlier answer.

If you do not want the student ID printed for each mark request, then move the line printf (">>Enter marks for %d:" , StudID ); out of the subject loop. Put it just before the subject loop, like this:

printf (">>Enter marks for %d:" , StudID ); for(i=0; i<noSubject;i++) /* this is the subject loop */

Keep working at it. You're doing great.

Advertisement

©2021 eLuminary LLC. All rights reserved.