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
}