Thread: averaging program

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    15

    averaging program

    How do would I incorporate a function that prints averages for each student in a class. I want to use A=1, B=2, C=3, D=4, and F=5. What would be the best way to go about doing this?

    Thanks

    Code:
    #include <stdio.h>
    
    #define CLASS_SIZE  100
    
    struct student {
        char    *last_name;
        int       student_id;
        char    grade;
    }
    
    
    #include "cl_info.h"
    
    int main(void)
    {
        struct student      temp,  class[CLASS_SIZE];
    
        temp.grade = 'A';
        temp.last_name = "Smith";
        temp.student_id = 39498;

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    add all the grades up, divide by the number of grades, and that will give you a numerical average. If the average is 1.5, then it is somewhere between 'A' and 'B'. So you will need a series of if statements
    Code:
    char Grade;
    if( average >= 0 && average < 2)
       Grade = 'A';
    else if (... // blabla

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    LOL I got the numbers mixed up, but you've helped me anyway.
    I meant to have A=4 and so on. Minor. Thanks for the help.

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Do you want to just have each letter correspond to the number and print that? Should there be multiple grades that we're averaging (right now there's only that one char grade)? Did you want the class average instead? I dunno. I just couldn't see where averaging is coming into this problem

  5. #5
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by Ancient Dragon
    add all the grades up, divide by the number of grades, and that will give you a numerical average. If the average is 1.5, then it is somewhere between 'A' and 'B'. So you will need a series of if statements
    Code:
    char Grade;
    if( average >= 0 && average < 2)
       Grade = 'A';
    else if (... // blabla
    or you could just use integer arithmetic which truncates to a whole number, or you could use floating point and rounding.....no need to use so many ifs.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    Quote Originally Posted by Tonto
    Do you want to just have each letter correspond to the number and print that? Should there be multiple grades that we're averaging (right now there's only that one char grade)? Did you want the class average instead? I dunno. I just couldn't see where averaging is coming into this problem
    I want it to be a letter grade for the average of each individual student. I haven't quite got the code working yet.

  7. #7
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
    const char letter_grades[] = { 'F', 'D', 'C', 'B', 'A' };
    int index = (grade1 + grade2 + grade3 + grade4 + grade5) / 5;
    char avg_grade = letter_grades[index];
    would be an idea.

    *note this code will not compile, duh.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    15
    Not quite following your.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. averaging program
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 10-04-2001, 11:14 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM