Really need help with C program to calculate grades using linked lists! Please help?
1 comment so farI am trying to calculate the average quizzes, tests and total score. I am getting a segmentation fault once I press n (for no). When I press n, it is supposed to show the averages for all of the scores I entered up to that point, like so:
Quiz1 Quiz2 midterm Final Total
Average 7.5 8.5 83.5 91 86.3
If anyone can help me ASAP, it would be greatly appreciated. Thank you.
#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 *next;
};
struct test_result *studen = NULL;
int main(void)
{
struct test_result *p;
char code = ‘y’;
int num_students = 0;
int done = 0;
while(!done) {
switch (code) {
case ‘y’: insert(num_students);
print(num_students);
break;
case ‘n’: average_finder(num_students); done++;
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)
{
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;
total_midterm += p->midterm;
total_final += p->final;
total_numeric += p->numeric;
average_quiz1 = total_quiz1 / num_students;
average_quiz2 = total_quiz2 / num_students;
average_midterm = total_midterm / num_students;
average_final = total_final / num_students;
average_numeric = total_numeric / num_students;
printf(” Quiz1 Quiz2 Midterm Final Total\n”);
printf(”Average %9.1f %9.1f %9.1f %9.1f %9.3f\n”, average_quiz1, average_quiz2, average_midterm, average_final, average_numeric);
}
void print(int num_students)
{
struct test_result *p;
printf(”\n”);
printf(”Name Quiz1 Quiz2 Midterm Final Total\n”);
printf(”%10s %9d %9d %9d %9d %9.1f\n”, p->name, p->grade1, p->grade2, p->midterm, p->final, 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;
}
Friday, March 19th, 2010 at 6:36 pm and is filed under Programming & Design. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.







Dave - I think you’re really missing the whole point about how a linked list works. You really need to go back and read that section of the textbook again.
You need at least 2 things to make a linked list work:
1) A pointer to the head of the list.
2) A link in each node of the list to the next node (where NULL means end of list).
You have neither. You declared a whole bunch of test_result structs or pointers (5 if I counted right), but none of them are acting as the Head pointer, and none are linked using the next pointer.
You need a head node:
struct test_result * Head = NULL;
When you allocate a node, you need to add to list (add to start, it’s easier):
struct test_result *node = malloc(sizeof(struct test_result));
node->next = Head; // add to front of list
Head = node; // Make node the start of list.
average_finder needs a loop. You need to loop over all the nodes:
struct test_result *node = Head;
while(node != NULL)
{
// do stuff with node
node = node->next;
}