Thread: Help with Simple Grading Program

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    1

    Help with Simple Grading Program

    Ok I made a simple program (2 days of learning C btw)
    It accepts the names and grades of the number of students entered and then finds their percentage based on the total marks of the exam. Problem:It only prints the percentage grade of one student. Please help.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <ctype.h>
    
    
    int main()
    {
     char fName[50][256];
     char lName [50] [256];
     int grades [50][256];
     int num,i,b,c;
     int total;
     float average;
    
    
    printf("Enter total marks for the examination:");
    scanf("%d",&total);
     printf("Enter number of students:");
     scanf("%d",&num);
     for (i=0;i<num;i++){
        printf("Enter their first names: ");
        scanf("%s",fName[i]);
        printf("Enter their last names: ");
        scanf("%s",lName[i]);
     }
     printf("Enter student's grade beside their name\n");
     for (b=0;b<num;b++){
        printf("%s ",fName[b]);
         printf("%s ",lName[b]);
         scanf("%d",grades[b]);
    
    
    printf("%s Grade:",fName[b]);
    average=(grades[b][b]/total)*100;
    printf("%.2f \n",average);
    
    
    
    
     }
     
    
    
    
    
       return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The first step is to work on your indentation skills, so it's clear what the overall flow of your program is.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <math.h>
    #include <ctype.h>
    
    
    int main()
    {
        char fName[50][256];
        char lName[50][256];
        int grades[50][256];
        int num, i, b, c;
        int total;
        float average;
    
        printf("Enter total marks for the examination:");
        scanf("%d", &total);
        printf("Enter number of students:");
        scanf("%d", &num);
    
        for (i = 0; i < num; i++) {
            printf("Enter their first names: ");
            scanf("%s", fName[i]);
            printf("Enter their last names: ");
            scanf("%s", lName[i]);
        }
    
        printf("Enter student's grade beside their name\n");
        for (b = 0; b < num; b++) {
            printf("%s ", fName[b]);
            printf("%s ", lName[b]);
            scanf("%d", grades[b]);
            printf("%s Grade:", fName[b]);
            average = (grades[b][b] / total) * 100;
            printf("%.2f \n", average);
        }
    
        return 0;
    }
    > scanf("%d", grades[b]);
    So why is grades a 2D array with 256 elements for each student?

    > average = (grades[b][b] / total) * 100;
    Why are you taking a diagonal through that array?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Here's a rough diagram of what your loop is doing - "the diagonal" Salem was talking about.

    Code:
    for (b = 0; b < MAX; b++) {
        printf("%2d %*.01f\n", b, 5*b+5, grades[b][b]);
    }
    
         0    1    2    3    4    5    6    7    8    9
     0  75.8
     1       21.8
     2            49.8
     3                  8.5
     4                      89.5
     5                           36.4
     6                                 0.6
     7                                     74.5
     8                                          73.3
     9                                               19.5
    Positions with numbers were processed. You need an outer loop, and another variable besides b, to keep the leftmost index constant while you process each grade.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a grading program in C
    By welchbs in forum C Programming
    Replies: 2
    Last Post: 02-26-2017, 11:37 AM
  2. a grading program
    By compugeek915 in forum C Programming
    Replies: 7
    Last Post: 10-19-2007, 12:38 AM
  3. Grading Program pt.II (classes) help...
    By eun-jin in forum C++ Programming
    Replies: 7
    Last Post: 05-10-2006, 04:50 PM
  4. Program for grading
    By MasterLika in forum C Programming
    Replies: 3
    Last Post: 04-04-2005, 01:33 AM
  5. Grading Program
    By hockeydrummer88 in forum C++ Programming
    Replies: 6
    Last Post: 03-09-2005, 12:05 PM

Tags for this Thread