Thread: C program using structs to calculate grades

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    9

    C program using structs to calculate grades

    Code:
    Here is the program description:
    
    1. Write a grading program for a class with the following grading policies.
    
    a. There are two quizzes, each graded on the basis of 10 points.
    b. There is one midterm exam and one final exam, each graded on the basis of 100 points. 
    c. The final exam counts for 50% of the grade, the midterm counts for 25%, and the two quizzes together count for a total of 25% (Do not forget to normalize the quiz scores).
    
    The program will read in the students’ scores and output the student’s numeric score for the entire course. The program will display the average of the quizzes, tests, and the total score before it terminates.
    
    A session with the program might look like this:
    
    Enter the student's name: Mike Fisher
    Enter the student's grade for quiz #1: 8
    Enter the student's grade for quiz #2: 9
    Enter the student's grade for midterm: 89
    Enter the student's grade for final: 98
    
    Mike Fisher's numeric score for the entire course is 92.5
    
    Would you like to enter another student record? y(yes) or n(no)?y
    
    Enter the student's name: Jon Stewart
    Enter the student's grade for quiz #1: 7
    Enter the student's grade for quiz #2: 8
    Enter the student's grade for midterm: 78
    Enter the student's grade for final: 84
    
    Jon Stewart's numeric score for the entire course is 80.25
    
    
    Would you like to enter another student record? y(yes) or n(no)?n
    
    The average score on quiz1 is 7.5
    The average score on quiz2 is 8.5
    The average score on midterm is 83.5
    The average score on final is 91
    The average score for the entire course is 86.375 
    
    
    Specifications: 
    
    1. The program should be built around an array of structures, with each structure containing information of a student’s record, including name (a string), quiz 1 score, quiz 2 score, midterm score, and final score (all scores are of type int)
    
    2. To read in a student’s name, use the version of read_line function that skips white-space characters before it begins storing characters.
    
    3. The program should have a loop that continues as long as the user wants to enter another student’s record. 
    
    4. Assume that there are no more than 100 students.
    
    5. Assume that each student’s name is no more than 50 characters long. 
    
    
    
    I would like to figure out how to get the following output:
    
    
    
    The average score on quiz1 is 7.5
    The average score on quiz2 is 8.5
    The average score on midterm is 83.5
    The average score on final is 91
    The average score for the entire course is 86.375 
    
    
    
    I have been able to get the average numerical scores, but I want to be able to get the average quiz1 score, quiz2 score, midterm, final, and entire course course once I type n (for no). How could I possibly do it? 
    
    #include <stdio.h>
    #include <ctype.h>
    #define NAME_LEN 50
    #define STUD_LEN 100
    
    int read_line(char str[], int n);
    int find_students(int students);
    
    struct test_result {
    char name[NAME_LEN+1];
    int number;
    int grade1;
    int grade2;
    int midterm;
    int final;
    float numeric;
    }studen[STUD_LEN];
    
    void insert(void);
    void print(void);
    
    int num_students = 0;
    
    int main(void)
    {
    struct test_result test;
    printf("Enter the student's name: ");
    read_line(test.name, NAME_LEN);
    printf("Enter the student's grade for quiz #1: ");
    scanf("%d", &test.grade1);
    printf("Enter the student's grade for quiz #2: ");
    scanf("%d", &test.grade2); 
    printf("Enter the student's grade for midterm: ");
    scanf("%d", &test.midterm);
    printf("Enter the student's grade for final: ");
    scanf("%d", &test.final);
    test.numeric = (((test.grade1 + test.grade2) * 1.25) + (test.midterm * 0.25) + (test.final * 0.50)); 
    printf("%s's numeric score for the entire course is %.1f\n", test.name, test.numeric);
    
    char code;
    for (;;) {
    printf("\n");
    printf("Would you like to enter another student record? y(yes) or n(no)?");
    scanf(" %c", &code);
    while (getchar() != '\n') /* skips to end of line */
    ;
    
    switch (code) {
    case 'y': insert();
    break;
    case 'n': print();
    return 0;
    default: printf("Invalid entry. Try again.\n");
    return 0;
    } 
    }
    }
    
    int find_students(int students)
    {
    int i;
    
    for (i = 0; i < num_students; i++)
    if (studen[i].number == students)
    return i;
    return -1;
    }
    
    
    void insert(void)
    { 
    int part_number;
    if (num_students == STUD_LEN) {
    printf("Sorry, cannot enter any more students.\n");
    return;
    }
    
    studen[num_students].number = part_number;
    printf("Enter the student name: ");
    read_line(studen[num_students].name, NAME_LEN);
    printf("Enter the student's grade for quiz #1: ");
    scanf("%d", &studen[num_students].grade1);
    printf("Enter the student's grade for quiz #2: ");
    scanf("%d", &studen[num_students].grade2);
    printf("Enter the student's grade for midterm: ");
    scanf("%d", &studen[num_students].midterm);
    printf("Enter the student's grade for final: ");
    scanf("%d", &studen[num_students].final);
    
    studen[num_students].numeric = 
    (((studen[num_students].grade1 + studen[num_students].grade2) * 1.25) + (studen[num_students].midterm * 0.25) + (studen[num_students].final * 0.50));
    printf("%s's numeric score for the entire course is %.1f\n", studen[num_students].name, studen[num_students].numeric);
    num_students++;
    }
    
    
    void print(void)
    {
    printf("The average score on quiz1 is\n");
    printf("The average score on quiz2 is\n"); 
    printf("The average score on midterm is\n"); 
    printf("The average score on the final is\n");
    printf("The average score for the entire course is\n");
    
    }
    
    
    int read_line(char str[], int n)
    {
    int ch, i = 0;
    
    while(isspace(ch = getchar()))
    ;
    str[i++] = ch;
    while ((ch = getchar()) != '\n') {
    if (i < n)
    str[i++] = ch;
    }
    str[i] = '\0';
    return i;
    }

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Code:
    Here is the program description:
    
    1. Write a grading program for a class with the following grading policies.
    
    a. There are two quizzes, each graded on the basis of 10 points.
    b. There is one midterm exam and one final exam, each graded on the basis of 100 points. 
    c. The final exam counts for 50% of the grade, the midterm counts for 25%, and the two quizzes together count for a total of 25% (Do not forget to normalize the quiz scores).
    
    The program will read in the students’ scores and output the student’s numeric score for the entire course. The program will display the average of the quizzes, tests, and the total score before it terminates.
    
    A session with the program might look like this:
    
    Enter the student's name: Mike Fisher
    Enter the student's grade for quiz #1: 8
    Enter the student's grade for quiz #2: 9
    Enter the student's grade for midterm: 89
    Enter the student's grade for final: 98
    
    Mike Fisher's numeric score for the entire course is 92.5
    
    Would you like to enter another student record? y(yes) or n(no)?y
    
    Enter the student's name: Jon Stewart
    Enter the student's grade for quiz #1: 7
    Enter the student's grade for quiz #2: 8
    Enter the student's grade for midterm: 78
    Enter the student's grade for final: 84
    
    Jon Stewart's numeric score for the entire course is 80.25
    
    
    Would you like to enter another student record? y(yes) or n(no)?n
    
    The average score on quiz1 is 7.5
    The average score on quiz2 is 8.5
    The average score on midterm is 83.5
    The average score on final is 91
    The average score for the entire course is 86.375 
    
    
    Specifications: 
    
    1. The program should be built around an array of structures, with each structure containing information of a student’s record, including name (a string), quiz 1 score, quiz 2 score, midterm score, and final score (all scores are of type int)
    
    2. To read in a student’s name, use the version of read_line function that skips white-space characters before it begins storing characters.
    
    3. The program should have a loop that continues as long as the user wants to enter another student’s record. 
    
    4. Assume that there are no more than 100 students.
    
    5. Assume that each student’s name is no more than 50 characters long. 
    
    
    
    I would like to figure out how to get the following output:
    
    
    
    The average score on quiz1 is 7.5
    The average score on quiz2 is 8.5
    The average score on midterm is 83.5
    The average score on final is 91
    The average score for the entire course is 86.375 
    
    
    
    I have been able to get the average numerical scores, but I want to be able to get the average quiz1 score, quiz2 score, midterm, final, and entire course course once I type n (for no). How could I possibly do it?
    That's not C
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to either make num_students a global variable, or you need to pass it around to insert(). Otherwise, the num_students variable that you're incrementing in insert(), will not be the same num_students that you need in other functions.

    After inserting new data, you should check that it's all being put into your data array, correctly. I'd suggest printing that array out and checking it for accuracy, before proceeding.

    Finding an average will work as always: (add up scores) and divide by num_students, once you get the num_students variable working.

    This snippet of code might help:

    Code:
    insert (&num_students);            //the call to insert, passes the address of num_students
    void insert (int *num_students);  //the function prototype for insert, receives that address
    Now changes to num_students, will be made to the calling function's variable, num_students.

    It's very helpful and customary, to put your code inside the code tags, and put your other commentary or questions, outside the code tags.

    Some good indentation style in your code, would be a godsend. Nobody wants to work with code like that.
    Last edited by Adak; 07-04-2009 at 11:46 PM.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    48
    First your assignment says
    >>The program should be built around an array of structures,
    makes it clear that you don't need the input in the main()
    function twice , maybe somthing like this
    Code:
    /*Includes here*/
    
    int read_line(char str[], int n);
    void insert (int student_number);
    void print (int num_students);
    void average_finder(int num_students);
    
    /*Note that every function gets the number of students
      as a parameter(originally you had it global)*/
    
    struct test_result {
        char name[NAME_LEN+1];
        int number;
        int grade1;
        int grade2;
        int midterm;
        int final;
        float numeric;
    }studen[STUD_LEN];
    
    int main(void)
    {
    
        char code;
        int num_students = 0;
        for (num_students = 0; num_students < STUD_LEN; num_students++) {
    
            printf("\nWould you like to enter another record? y(yes) or n(no)?");
            scanf(" %c", &code);
            while (getchar() != '\n') /* skips to end of line */
             ;
    
            switch (code) {
            case 'y': insert(num_students);
                      print(num_students);
             break;
            case 'n': average_finder(num_students);
             return 0;
    
            default: printf("Invalid entry. Try again.\n");
             return 0;
    
         }
        }
    }
    I also changed your insert() function definition to get rid of the checking of the num_students.
    Code:
    void insert(int num_students)
    {
       studen[num_students].number = part_number;
    
        printf("Enter the student name: ");
        read_line(studen[num_students].name, NAME_LEN);
    
        printf("Enter the student's grade for quiz #1: ");
        scanf("%d", &studen[num_students].grade1);
    
        printf("Enter the student's grade for quiz #2: ");
        scanf("%d", &studen[num_students].grade2);
    
        printf("Enter the student's grade for midterm: ");
        scanf("%d", &studen[num_students].midterm);
    
        printf("Enter the student's grade for final: ");
        scanf("%d", &studen[num_students].final);
    
        studen[num_students].numeric =
        (((studen[num_students].grade1 + studen[num_students].grade2) * 1.25) + (studen[num_students].midterm * 0.25) + (studen[num_students].final * 0.50));
    
    }
    While your average finder could look something like this:

    Code:
      /*For displaying average of quiz1 scores ,you can surely  do for the remaining  ones*/  
      int i ;
        int  total_quiz1 = 0;
        float average_quiz1 = 0;
    
        for (i = 0; i < num_students ;i++)  {
            total_quiz1 += studen[i].grade1;
        }
    
        average_quiz1 = (double)total_quiz1 /(num_students);
       
        printf ("\nAverage quiz 1 score = %.2lf," , average_quiz1);
    }
    Your print function
    Code:
    void print(int num_students)
    {
        printf ("\nName:%25s\n" , studen[num_students].name);
        printf("The average score on quiz1 is%10d\n",studen[num_students].grade1);
        printf("The average score on quiz2 is%10d\n",studen[num_students].grade2);
        printf("The average score on midterm is%8d\n",studen[num_students].midterm);
        printf("The average score on the final is %5d\n",studen[num_students].final);
        printf("The average score for the entire course is %10.3f\n",studen[num_students].numeric );
    
    }
    Your read line function remains the same.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    You will not get the correct answer for an average until you make :

    int grade1;
    int grade2;

    as

    double grade1;
    double grade2;

    Otherwise they will just be shortened to be integers. The 7.5 will be 7, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program to calculate change return.
    By OrAnGeWorX in forum C Programming
    Replies: 15
    Last Post: 11-17-2008, 09:49 AM
  2. Small program that has to calculate miles per gallon
    By Guti14 in forum C++ Programming
    Replies: 6
    Last Post: 01-06-2004, 02:47 PM
  3. Replies: 2
    Last Post: 03-13-2003, 09:40 AM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM