Question
i need help with my c code to get the right output, my code isnt running, need help fixing it. record_llist.h #ifndef RECORD_LLIST #define RECORD_LLIST
i need help with my c code to get the right output, my code isnt running, need help fixing it.
record_llist.h
#ifndef RECORD_LLIST
#define RECORD_LLIST
#include
#include
#include
#include
/**
* RECORD structure
* name - char array for person's name
* score - float score of record
*/
typedef struct record{
char name[40];
float score;
} RECORD;
/**
* linked list node structure
* data - RECORD data
* next - pointer pointing to next node of linked list
*/
typedef struct node {
int data;
struct node *next;
} NODE;
/**
* Display linked list.
* @param start Pointer to the first node of linked list.
*/
void display(NODE *start);
/**
* Search linked list for name key.
* @param start Pointer to the first node of linked list.
* @param name Key to search
* @return Pointer to found node if found; otherwise NULL
*/
NODE *search(NODE *start, char *name);
/**
* Insert new record to linked list at the sorted position.
* @param startp Pointer pointing to the start pointer of linked list, used to update the start node address in case of change.
* @param name The name data of new record.
* @param score The score data of new record
*/
void insert(NODE **startp, char *name, float score);
/**
* Deletes a node of the name from the linked list and returns 1 if successful or 0 otherwise.
* @param startp pointer pointing to the first node
* @param name The name of the node being deleted
*/
int delete(NODE **startp, char *name);
/**
* This cleans the linked list, namely free all nodes.
* @param startp pointer to the node of linked list.
*/
void clean(NODE **startp);
// The following functions are adapted and/or modified from previous assignment
void import_data(NODE **startp, char *filename);
void report_data(NODE *start, char *filename);
char letter_grade(float score);
void swap(float *first, float *second );
void quick_sort(float *a, int left, int right);
float median(NODE *start);
#endif
record_llist.c
#include "record_llist.h"
#include
#include
#include
#include
NODE *new_node(int val) {
NODE *p = (NODE*)malloc(sizeof(NODE));
if(p != NULL) {
p->data = val;
p->next = NULL;
return p;
}
}
void display(NODE *start) {
if (start == NULL) return;
NODE *ptr = start;
while (ptr != NULL) {
printf("%d", ptr->data);
ptr = ptr->next;
}
}
NODE* search(NODE *start, char *name) {
NODE *ptr = start;
while (ptr != start) {
if (strcmp(ptr->data.name, name) == 0)
return ptr;
ptr = ptr->next;
}
return NULL;
}
void insert(NODE **startp, char *name, float score) {
NODE *p = (NODE *) molloc(sizeof(NODE));
strcpy(p->data.name, name);
p->data.score = score;
p->next = NULL;
NODE *prev = NULL;
NODE *ptr = *startp;
NODE *np = *startp;
while (np != NULL) {
if(strcmp(ptr->data.name, name) >= 0)
break;
prev = ptr;
ptr = ptr->next;
}
if(prev == NULL) {
*startp = p;
p->next = ptr;
} else {
prev->next = p;
p->next = ptr;
}
}
int delete(NODE **startp, char *name) {
NODE *ptr = *startp;
if (ptr != NULL) {
*startp = ptr->next;
free(ptr);
}
}
void clean(NODE **startp) {
struct node *temp, *np = *startp;
while (np != NULL) {
temp = np;
np = np -> next;
free(temp);
}
*startp = NULL;
}
// the following functions are adapted and modified from previous assignments. You make use of them.
void report_data(NODE *start, char *filename) {
NODE *np = start;
int count = 0;
float mean = 0;
float stddev = 0;
int histogram[6] = {0};
FILE *fp = fopen(filename, "w");
fprintf(fp, "grade report ");
while (np != NULL) {
count++;
mean += np->data.score;
stddev += np->data.score * np->data.score ;
//histogram data
if (np->data.score >=100) histogram[0]++;
else if (np->data.score
else histogram[(unsigned int) (99.99-np->data.score)/10]++;
fprintf(fp, "%-15s%3.1f%4c ", np->data.name, np->data.score, letter_grade(np->data.score));
np = np->next;
}
mean /= count;
stddev = sqrt(stddev/count - mean*mean);
fprintf(fp, " statistics summary ");
fprintf(fp, "%-15s%d ", "count", count);
fprintf(fp, "%-15s%3.1f ", "mean", mean);
fprintf(fp, "%-15s%3.1f ", "stddev", stddev);
fprintf(fp, "%-15s%3.1f ", "median", median(start));
fprintf(fp, "histogram ");
int i;
fprintf(fp, "%-15s%d ", "[90,100]", histogram[0]);
for (i=1; i
fprintf(fp, "[%d,%d) %d ", 90-i*10, 100-i*10, histogram[i]);
}
fprintf(fp, "%-15s%d ", "[0,50)", histogram[5]);
fclose(fp);
}
void import_data(NODE **startp, char *filename) {
char line[40], name[20];
FILE *fp = fopen(filename, "r");
char *result = NULL;
char delimiters[] = ",";
float score = 0;
if (fp == NULL) {
perror("Error while opening the file. ");
exit(EXIT_FAILURE);
}
while (fgets(line, sizeof(line), fp) != NULL) {
result = strtok(line, delimiters);
strcpy(name, result);
result = strtok(NULL, delimiters);
score = atof(result);
insert(startp, name, score);
}
fclose(fp);
}
char letter_grade(float s){
char r = 'F';
if (s >= 85) r = 'A';
else if (s >= 70) r = 'B';
else if (s >= 60) r = 'C';
else if (s >= 50) r = 'D';
else r = 'F';
return r;
}
void swap(float *first, float *second )
{
float temp = *first;
*first = *second;
*second = temp;
}
void quick_sort(float *a, int left, int right)
{
float key;
int i,j;
if( left
key = *(a+left);
i = left+1;
j = right;
while(i
while( i
while( j >= left && *(a+j) > key ) j--;
if( i
}
swap(a+left, a+j);
quick_sort(a, left, j-1);
quick_sort(a, j+1, right);
}
}
float median(NODE *start)
{
if (start == NULL) return 0;
int len = 0, i=0;
NODE *np = start;
while (np) { len++; np = np->next; }
float a[len];
for (i=0, np=start; inext) {
a[i] = np->data.score;
}
quick_sort(a, 0, len-1);
return (len % 2 == 1)? a[len/2] : (a[len/2-1] + a[len/2])/2 ;
}
output:
Ali 88.0 Allison 67.7 Bodnar 93.6 Chabot 80.4 Costa 45.1 Dabu 74.4 Eccles 77.8 Giblett 59.1 Hatch 66.5 He 85.7 Koreck 77.4 Lamont 98.1 Myrie 76.7 Parr 92.5 Pereira 80.3 Peters 82.3 Smith 60.1 Suglio 85.7 Sun 67.7 Wang 98.1 Found:Moore, 92.00
marks.txt
Myrie,76.7 Hatch,66.5 Costa,45.1 Dabu,74.4 Sun,67.7 Chabot,80.4 Giblett,59.1 Suglio,85.7 Smith,60.1 Bodnar,93.6 Allison,67.7 Koreck,77.4 Parr,92.5 Lamont,98.1 Peters,82.3 Wang,98.1 Eccles,77.8 Pereira,80.3 He,85.7 Ali,88.0
report.txt
grade report Ali 88.0 A Allison 67.7 C Bodnar 93.6 A Chabot 80.4 B Costa 45.1 F Dabu 74.4 B Eccles 77.8 B Giblett 59.1 D Hatch 66.5 C He 85.7 A Koreck 77.4 B Lamont 98.1 A Moore 92.0 A Myrie 76.7 B Parr 92.5 A Pereira 80.3 B Peters 82.3 B Smith 60.1 C Suglio 85.7 A Sun 67.7 C
statistics summary count 20 mean 77.6 stddev 13.1 median 79.1 histogram [90,100] 4 [80,90) 6 [70,80) 4 [60,70) 4 [50,60) 1 [0,50) 1
v C record_llist.c q1 14 expression must have struct or union type [39, 16] expression must have struct or union type [49, 3] expression must have struct or union type [50, 3] expression must have struct or union type [56, 15] expression must have struct or union type [102, 13] expression must have struct or union type [103, 15] expression must have struct or union type [103, 32] expression must have struct or union type [105, 9] expression must have struct or union type [106, 14] expression must have struct or union type [107, 42] expression must have struct or union type [109, 36] expression must have struct or union type [109, 51] expression must have struct or union type [109, 80] expression must have struct or union type [199, 12] v C record_llist.c q1 14 expression must have struct or union type [39, 16] expression must have struct or union type [49, 3] expression must have struct or union type [50, 3] expression must have struct or union type [56, 15] expression must have struct or union type [102, 13] expression must have struct or union type [103, 15] expression must have struct or union type [103, 32] expression must have struct or union type [105, 9] expression must have struct or union type [106, 14] expression must have struct or union type [107, 42] expression must have struct or union type [109, 36] expression must have struct or union type [109, 51] expression must have struct or union type [109, 80] expression must have struct or union type [199, 12]Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started