Question:
C program to print grades using linked list?
anonymous
2009-07-11 23:27:20 UTC
Here is the description:

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;
}
Three answers:
sney e
2009-07-12 00:15:44 UTC
You have the logic right, you're just missing the loop. See this fixed main function:



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;

}



This could be done better but I just made a quick change to your existing code. Mess with it if you'd like.
anonymous
2016-10-03 14:42:50 UTC
that is no longer an hassle-free undertaking. in case you have in no way applied parenthesis matching, heavy parsing in any code to your assignments so a techniques, or have not obtained a smart information of c you're in excess of your head. This undertaking merely is composed of assorted legwork in valuable aspects which you will ought to modularize. i individually could start up off with a function that implements a stack to work out in case you have the terrific suited quantity of parenthesis. Then, write a header document complete of purposes that examine syntax. each and every function could take a char pointer as its parameter(char*) and return an integer comparable to no count number if the syntax is genuine for that rule. Then after that factor you in basic terms: a. study a line of the document, b. examine parenthesis, c. examine syntax. If that line has blunders say so, if no longer bypass to the subsequent one until eventually the top of the document is reached. of course you may choose to discover the least perplexing thank you to try this on your undertaking to respond to the questions.
D
2015-02-07 15:42:22 UTC
your fix for the code has many bugs and doesn't run with your suggestions


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...