Question
Write a C program, named bst.h, bst.c, markstats.h, markstats.c, a7q2.c, to hold mark records by binary search tree and to process data statistics. The mark
Write a C program, named bst.h, bst.c, markstats.h, markstats.c, a7q2.c, to hold mark records by binary search tree and to process data statistics. The mark records are in file marks.txt. Compute average, standard deviation and letter grades, output results to a file named report.txt sorted by name like report.txt. More requirements are as following.
-
Refer to the class code example simple binary search tree for the implementation of some functions.
bst.h
#ifndef BST_H #define BST_H #include
#include #include #include typedef struct rec { char name[40]; float score; } RECORD; typedef struct node { RECORD data; struct node *left; struct node *right; } TNODE; /* search bst by data.name */ TNODE *search(TNODE *root, char *name); /* insert record (name, score) into bst using data.name as key */ void insert(TNODE **rootp, char *name, float score); /* delete node of data.name from bst */ void delete(TNODE **rootp, char *name); /* get and return and delete the node of the smallest data.name node from the bst */ TNODE *extract_smallest_node(TNODE **rootp); /* display bst tree data by in-order */ void display_inorder(TNODE *root); /* clean the tree */ void clean_tree(TNODE **rootp); #endif Refer to lecture 14 for the implementation of insert and delete functions.
bst.c
#include "bst.h" TNODE* search(TNODE *root, char *name) { // your implementation } void insert(TNODE **rootp, char *name, float score) { // your implementation } void delete(TNODE **rootp, char *name) { // your implementation } TNODE *extract_smallest_node(TNODE **rootp) { // your implementation } void display_inorder(TNODE *root) { // similar to that of tree.c } void clean_tree(TNODE **rootp) { // similar to that of tree.c }
markstats.h
#ifndef MARKSTATS_H #define MARKSTATS_H #include
#include #include #include "bst.h" typedef struct mark_stats { TNODE *bst; int count; float mean; float stddev; } MARKS; void add_data(MARKS *dataset, char *name, float score); void remove_data(MARKS *dataset, char *name); // following functions are from a5q2 void display_stats(MARKS *dataset); void import_data(MARKS *dataset, char *filename); void report_data(MARKS *dataset, char *filename); void print_to_file(TNODE *data, FILE *filename); char letter_grade(float score); #endif Refer to lecture 14 for the implementation of insert and delete functions.
markstats.c
#include "markstats.h" void add_data(MARKS *sd, char *name, float score) { if (search(sd->bst, name) == NULL) { insert(&sd->bst, name, score); //your code to compute statistics summary after adding new data element } else printf("record exit"); } void remove_data(MARKS *sd, char *name) { TNODE *np = NULL; if ( (np = search(sd->bst, name)) != NULL) { float score = np->data.score; delete(&sd->bst, name); //your code to compute statistics summary after removing a data element } else printf("record not exit"); } void display_stats(MARKS *sd) { printf(" statistics summary "); printf("%-15s%d ", "count", sd->count); printf("%-15s%3.1f ", "mean", sd->mean); printf("%-15s%3.1f ", "stddev", sd->stddev); } void import_data(MARKS *ds, char *filename) { char line[40], name[20]; FILE *fp = fopen(filename, "r"); char *result = NULL; char delimiters[] = ", "; float score = 0; int count = 0; float mean =0, stddev = 0; if (fp == NULL) { perror("Error while opening the file. "); exit(EXIT_FAILURE); } while (fgets(line, sizeof(line), fp) != NULL) { result = strtok(line, delimiters); if (result) { strcpy(name, result); result = strtok(NULL, delimiters); score = atof(result); count++; mean += score; stddev += score * score; insert(&ds->bst, name, score); } } ds->count = count; mean /= count; ds->mean = mean; ds->stddev = sqrt(stddev/count - mean*mean); fclose(fp); } void report_data(MARKS *sd, char *filename) { FILE *fp = fopen(filename, "w"); fprintf(fp, "grade report "); print_to_file(sd->bst, fp); fprintf(fp, " simple statistics summary "); fprintf(fp, "%-15s%d ", "count", sd->count); fprintf(fp, "%-15s%3.1f ", "mean", sd->mean); fprintf(fp, "%-15s%3.1f ", "stddev", sd->stddev); fclose(fp); } void print_to_file(TNODE *root, FILE *fp) { if (root) { if (root->left) print_to_file(root->left, fp); fprintf(fp, "%-15s%3.1f%4c ", root->data.name, root->data.score, letter_grade(root->data.score)); if (root->right) print_to_file(root->right, 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; }
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