This program should enter student records into a linked listof structures and pr
ID: 3612896 • Letter: T
Question
This program should enter student records into a linked listof structures and print out the entire class record at theend. I have to use malloc to allocate memory space for eachstructure -I think I need a for loop at the end that contains theprintf( ) lines, but what arguments do I use in the forloop?? -I am getting an error in line 100: error: expected identifier before struct I really would appreciate some help with this, I think I amclose to getting it right, but I could be wrong. code so far: #include <stdio.h>#include <ctype.h>
#include <stdlib.h>
#define MAX_NAME_LEN 50 struct record{
char name[MAX_NAME_LEN+1];
int number;
int quiz1;
int quiz2;
int midterm;
int final;
float numeric;
struct record *next;
}; struct record *first = NULL; // points to first studentrecord
struct record *new_record; // points to the next node forinsert void insert_rec(struct record record);
int read_line(char str[], int n); //function declaration, will readin names of students int main(void)
{
int count=0;
float sum_q1=0, sum_q2=0, sum_mid=0, sum_fin=0,sum_total=0;
float average_q1, average_q2, average_mid,average_fin, average_total;
char choice;
struct record record;
do{
printf("Enter thestudent's name: ");
scanf("%s",record.name);
read_line(record.name,MAX_NAME_LEN);
printf("Enter thestudent's grade for quiz #1: ");
scanf("%d",&record.quiz1);
printf("Enter thestudent's grade for quiz #2: ");
scanf("%d",&record.quiz2);
printf("Enter thestudent's grade for midterm: ");
scanf("%d",&record.midterm);
printf("Enter thestudent's grade for final: ");
scanf("%d",&record.final);
record.numeric =(((record.quiz1+record.quiz2) * 1.25)+(record.midterm *0.25)+(record.final * 0.50));
printf("%s's numericscore for the entire course is %.2f ", record.name,record.numeric);
//student[num_students++] = record;
insert_rec(record);
printf(" ");
printf("Would you liketo enter another student record? y(yes) or n(no)?");
scanf(" %c",&choice);
count++;
//for(i = 0; i <MAX_STUDENT; i++)
//{
sum_q1 += record.quiz1;
sum_q2 += record.quiz2;
sum_mid += record.midterm;
sum_fin += record.final;
sum_total += record.numeric;
//}
average_q1 = (sum_q1 /count) / 100;
average_q2 = (sum_q2 /count) / 100;
average_mid = (sum_mid /count) / 100;
average_fin = (sum_fin /count) / 100;
average_total =(sum_total / count) / 100;
}while(choice != 'n');
printf(" Name Quiz1 Quiz2 Midterm Final Total ");
printf("----------------------------------------------------------------------- ");
printf(" Quiz1 Quiz2 Midterm Final Total ");
printf("----------------------------------------------------------------------- ");
printf("Average %9.1f%9.1f%9.1f%9.0f%9.3f ",average_q1, average_q2, average_mid, average_fin,average_total);
printf(" ");
getchar();
return 0;
}
int read_line(char str[], int n) //reads string into an array
{
int ch, i = 0;
while (isspace(ch = getchar()))
;
str[i++] = ch;
while ((ch = getchar()) != ' ') {
if (i < n)
str[i++] = ch;
}
str[i] = '';
return i;
}
void insert_rec(struct record record)
{
new_record->next = first;
first = new_record;
first = NULL;
new_record = malloc(sizeof(struct record));
new_record->struct record;
}
This program should enter student records into a linked listof structures and print out the entire class record at theend. I have to use malloc to allocate memory space for eachstructure -I think I need a for loop at the end that contains theprintf( ) lines, but what arguments do I use in the forloop?? -I am getting an error in line 100: error: expected identifier before struct I really would appreciate some help with this, I think I amclose to getting it right, but I could be wrong. code so far: #include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define MAX_NAME_LEN 50 struct record{
char name[MAX_NAME_LEN+1];
int number;
int quiz1;
int quiz2;
int midterm;
int final;
float numeric;
struct record *next;
}; struct record *first = NULL; // points to first studentrecord
struct record *new_record; // points to the next node forinsert void insert_rec(struct record record);
int read_line(char str[], int n); //function declaration, will readin names of students int main(void)
{
int count=0;
float sum_q1=0, sum_q2=0, sum_mid=0, sum_fin=0,sum_total=0;
float average_q1, average_q2, average_mid,average_fin, average_total;
char choice;
struct record record;
do{
printf("Enter thestudent's name: ");
scanf("%s",record.name);
read_line(record.name,MAX_NAME_LEN);
printf("Enter thestudent's grade for quiz #1: ");
scanf("%d",&record.quiz1);
printf("Enter thestudent's grade for quiz #2: ");
scanf("%d",&record.quiz2);
printf("Enter thestudent's grade for midterm: ");
scanf("%d",&record.midterm);
printf("Enter thestudent's grade for final: ");
scanf("%d",&record.final);
record.numeric =(((record.quiz1+record.quiz2) * 1.25)+(record.midterm *0.25)+(record.final * 0.50));
printf("%s's numericscore for the entire course is %.2f ", record.name,record.numeric);
//student[num_students++] = record;
insert_rec(record);
printf(" ");
printf("Would you liketo enter another student record? y(yes) or n(no)?");
scanf(" %c",&choice);
count++;
//for(i = 0; i <MAX_STUDENT; i++)
//{
sum_q1 += record.quiz1;
sum_q2 += record.quiz2;
sum_mid += record.midterm;
sum_fin += record.final;
sum_total += record.numeric;
//}
average_q1 = (sum_q1 /count) / 100;
average_q2 = (sum_q2 /count) / 100;
average_mid = (sum_mid /count) / 100;
average_fin = (sum_fin /count) / 100;
average_total =(sum_total / count) / 100;
}while(choice != 'n');
printf(" Name Quiz1 Quiz2 Midterm Final Total ");
printf("----------------------------------------------------------------------- ");
printf(" Quiz1 Quiz2 Midterm Final Total ");
printf("----------------------------------------------------------------------- ");
printf("Average %9.1f%9.1f%9.1f%9.0f%9.3f ",average_q1, average_q2, average_mid, average_fin,average_total);
printf(" ");
getchar();
return 0;
}
int read_line(char str[], int n) //reads string into an array
{
int ch, i = 0;
while (isspace(ch = getchar()))
;
str[i++] = ch;
while ((ch = getchar()) != ' ') {
if (i < n)
str[i++] = ch;
}
str[i] = '';
return i;
}
void insert_rec(struct record record)
{
new_record->next = first;
first = new_record;
first = NULL;
new_record = malloc(sizeof(struct record));
new_record->struct record;
}
void insert_rec(struct record record)
{
new_record->next = first;
first = new_record;
first = NULL;
new_record = malloc(sizeof(struct record));
new_record->struct record;
}
Explanation / Answer
do{
printf("Enter thestudent's name: ");
scanf("%s",record.name);
read_line(record.name,MAX_NAME_LEN);
printf("Enter thestudent's grade for quiz #1: ");
scanf("%d",&record.quiz1);
printf("Enter thestudent's grade for quiz #2: ");
scanf("%d",&record.quiz2);
printf("Enter thestudent's grade for midterm: ");
scanf("%d",&record.midterm);
printf("Enter thestudent's grade for final: ");
scanf("%d",&record.final);
record.numeric =(((record.quiz1+record.quiz2) * 1.25)+(record.midterm *0.25)+(record.final * 0.50));
printf("%s's numericscore for the entire course is %.2f ", record.name,record.numeric);
//student[num_students++] = record;
insert_rec(record);
printf(" ");
printf("Would you liketo enter another student record? y(yes) or n(no)?");
scanf(" %c",&choice);
count++;
}while(choice != 'n');
//for(i = 0; i < MAX_STUDENT; i++)
//{
ptr= first;
while(ptr !=NULL)
{
record = *ptr;
sum_q1 += record.quiz1;
sum_q2 += record.quiz2;
sum_mid += record.midterm;
sum_fin += record.final;
sum_total += record.numeric;
ptr=ptr->next;
}
average_q1= (sum_q1 / count) / 100;
average_q2 = (sum_q2 /count) / 100;
average_mid = (sum_mid /count) / 100;
average_fin = (sum_fin /count) / 100;
average_total =(sum_total / count) / 100;
printf(" Name Quiz1 Quiz2 Midterm Final Total ");
printf("----------------------------------------------------------------------- ");
printf(" Quiz1 Quiz2 Midterm Final Total ");
printf("----------------------------------------------------------------------- ");
printf("Average %9.1f%9.1f%9.1f%9.0f%9.3f ",average_q1, average_q2, average_mid, average_fin,average_total);
printf(" ");
getchar();
return 0;
}
int read_line(char str[], int n) //reads string into an array
{
int ch, i = 0;
while (isspace(ch = getchar()))
;
str[i++] = ch;
while ((ch = getchar()) != ' ') {
if (i < n)
str[i++] = ch;
}
str[i] = '';
return i;
}
voidinsert_rec(struct record rec)
{
if(first ==NULL)
{
first=new_record =(struct record *)malloc(sizeof(struct record));
*first = rec;
first->next = NULL;
}else
{
new_record =(struct record *)malloc(sizeof(struct record));
*new_record = rec;
new_record->next = first;
first = new_record;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.