Thread: Structing with grades

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    28

    Structing with grades

    Im working on a program that is supposed to return a class average using a struct. I've found some examples that get me in the right direction, but at the moment, Im not getting very far in understanding how to format data/read data from a struct into my program and produce the result. I've got one example of temp data but no earthly idea or reference in how to make this work. At this point I would rather just have the information(data) in the program itself and not worry about reading from a text file or input from the screen. My code is still not where it needs to be, but I dont think I can go further until I know how to put the data into my program and understand the struct concept better. The rough code I'm working with so far is:

    Code:
    //MAIN.C
    #include "cl_info.h"
    
    int main(void)
    {
    	struct student temp, class[CLASS_SIZE]; // ????
    	char grade;
    	// Database for grades goes here?
    	temp.grade = 'B';
    	temp.last_name = "Jones";
    	temp.student_id = 31223;
    
    	//prints out each student#, student last name and letter grade
    	printf("GRADE REPORT FOR STUDENT #: %d LAST NAME:
     %c FINAL GRADE: %
    c\n",temp.student_id,temp.last_name,temp.grade);
    	avg = average(struct student class[CLASS_SIZE]);
    	//Assigns a letter grade for GPA
    	if(avg > 4.0)
    		printf("ERROR, TOO HIGH.  Check your logic!");
    	if(avg == 4.0)
    		grade = 'A';
    	if(avg >= 3.0 && avg <= 3.99)
    		grade = 'B';
    	if(avg >= 2.0 && avg <= 2.99)
    		grade = 'C';
    	if(avg >= 1.0 && avg <= 1.99)
    		grade = 'D';
    	if(avg < .099 && avg >= 0)
    		grade = 'F';
    	if(avg <= -.001)
    		printf("ERROR  TOO LOW.  Check your logic!");
    			
    
    	printf("The class grade average is a %c, or %f.3, 
    based on a GPA scale of 4.0",grade,avg);
     //prints out the class gpa average and letter grade
    
    	
    
    return 0;
    
    }
    
    //CL_INFO.H
    
    #include <stdio.h>
    
    #define CLASS_SIZE 100
    
    int average(struct student class[]);
    
    struct student
    {
    	char	*last_name;
    	int		student_id;
    	char	grade;
    };
    float avg;
    
    //AVERAGE.C
    
    // Average's Class Grades
    
    // 4 is for A, 3 for B...etc
    
    #include "cl_info.h"
    
    int average(struct student class[])
    {
    int i, a, result, tmp = 0, cnt = 0;
    
    for(i = 0; i < CLASS_SIZE; ++i)
    	{
    	++cnt; //Brings count to 1.  This counter will be used to find the average
    	a = class[i].grade; //Adds the first array to result
    	result = tmp + a; // temp Sum of 2 students
    	tmp = result;	// transfers the sum to temp for summing in the next iteration
    	}
    	
    	avg = tmp / cnt; // class average (GPA)
    	return avg; // returns class average to main
    }

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    First off, you have alot of if statements in there that should be else ifs in the same statement. It's not possible for a value to have two different letter grades, is it? It's a logical error to use minimum in maximum in an if statement.

    Instead of this:
    Code:
    	if(avg > 4.0)
    		printf("ERROR, TOO HIGH.  Check your logic!");
    	if(avg == 4.0)
    		grade = 'A';
    	if(avg >= 3.0 && avg <= 3.99)
    		grade = 'B';
    	if(avg >= 2.0 && avg <= 2.99)
    		grade = 'C';
    	if(avg >= 1.0 && avg <= 1.99)
    		grade = 'D';
    	if(avg < .099 && avg >= 0)
    		grade = 'F';
    	if(avg <= -.001)
    		printf("ERROR  TOO LOW.  Check your logic!");
    Consider doing this:
    Code:
    	
        if (avg > 4.0)
           printf("Check your logic.");
        else if (avg == 4.0)
           grade = 'A';
        else if (avg >= 3.0)
           grade = 'B';
        else if (avg >= 2.0)
           grade = 'C';
        else if (avg >= 1.0)
           grade = 'D';
        else if (avg >= 0.0)
           grade = 'F';
        else
           printf("Check your logic.");
    It gives you the same results and it's much more quicker and logical.

    Secondly, you should be reading in a outputting a string for the last name, not a character. Change "%c" to "%s".

    Next, you have a syntax error on how you're setting float precision. It should be "%.2f" not "%f.2"
    Last edited by SlyMaelstrom; 10-21-2005 at 12:13 PM.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a lot C programming help PLEASE!
    By copelli39 in forum C Programming
    Replies: 11
    Last Post: 04-21-2009, 09:37 PM
  2. Replies: 16
    Last Post: 10-27-2007, 12:42 PM
  3. bare bones grades program
    By stormy in forum C Programming
    Replies: 6
    Last Post: 09-13-2005, 04:06 PM
  4. Grades program has me lost
    By adrea in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2002, 08:35 PM
  5. Replies: 13
    Last Post: 08-15-2002, 09:20 AM