anonymous
2009-07-11 23:27:20 UTC
1) The program uses the same grading policy. The program will read in the students’ scores.
2) The program should be built around a linked list of structures instead of an array of structures. The structure will contain an additional member (a pointer to the next node). Store a student’s information in a dynamically allocated structure.
3) There should not be a limit on the size of the database. Use malloc to allocate memory for a new structure.
4) Before the program terminates, print a listing of all students in the database, showing their names, scores on quizzes and tests, and their numeric score for the entire course.
Name Quiz1 Quiz2 midterm Final Total
Mike Fisher 8 9 89 98 92.5
Jon Stewart 7 8 78 84 80.5
…
5) Before the program terminates, display the average of the quizzes, tests, and the total score.
Quiz1 Quiz2 midterm Final Total
Average 7.5 8.5 83.5 91 86.375
The program stops after I enter y (for yes). Also, I would like to be able to properly print the averages. I am not sure how to do it properly. Please help.
#include
#include
#include
#define NAME_LEN 50
int read_line(char str[], int n);
void insert (int num_students);
void average_finder(int num_students);
void print (int num_students);
struct test_result {
char name[NAME_LEN+1];
int number;
int grade1;
int grade2;
int midterm;
int final;
float numeric;
};
struct test_result *studen = NULL;
int main(void)
{
struct test_result *p;
char code = 'y';
int num_students = 0;
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;
}
printf("\nWould you like to enter another record? y(yes) or n(no)?");
scanf(" %c", &code);
while (getchar() != '\n') /* skips to end of line */
;
return 0;
}
void insert(int num_students)
{
//int part_number = 0;
struct test_result *p;
p = malloc(sizeof(struct test_result));
printf("Enter the student name: ");
read_line(p->name, NAME_LEN);
printf("Enter the student's grade for quiz #1: ");
scanf("%d", &p->grade1);
printf("Enter the student's grade for quiz #2: ");
scanf("%d", &p->grade2);
printf("Enter the student's grade for midterm: ");
scanf("%d", &p->midterm);
printf("Enter the student's grade for final: ");
scanf("%d", &p->final);
p->numeric = (((p->grade1 + p->grade2) * 1.25) + (p->midterm * 0.25) + (p->final * 0.50));
}
void average_finder(int num_students)
{
num_students = 0;
struct test_result *p;
int total_quiz1 = 0;
int total_quiz2 = 0;
int total_midterm = 0;
int total_final = 0;
int total_numeric = 0;
float average_quiz1 = 0;
float average_quiz2 = 0;
float average_midterm = 0;
float average_final = 0;
float average_numeric = 0;
total_quiz1 += p->grade1;
total_quiz2 += p->grade2; // += num_students;
total_midterm += p->midterm; // += num_students;
total_final += p->final; // += num_students;
total_numeric += p->numeric; // += num_students;
average_quiz1 = (double)total_quiz1 / num_students;
average_quiz2 = (double)total_quiz2 / num_students;
average_midterm = (double)total_midterm / num_students;
average_final = (double)total_final / num_students;
average_numeric = (double)total_numeric / (num_students);
printf ("The average score on quiz 1 is %.1f\n", average_quiz1);
printf ("The average score on quiz 2 is %.1f\n", average_quiz2);
printf ("The average score on midterm is %.1f\n", average_midterm);
printf ("The average score on final is %.1f\n", average_final);
printf ("The average score for the entire course is %.1f\n", average_numeric);
}
void print(int num_students)
{
struct test_result *p;
printf("\n");
printf("%s's score for the entire course is %.1f\n", p->name, p->numeric );
}
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;
}