help me to complete the source
the txt file is
15
KimSD 38
ParkES 91
LeeYS 87
LeeYK 25
KimWY 14
MinHB 77
MoonYS 85
ChoiKI 73
KimIS 68
KimHI 35
ParkJK 66
LeeDR 89
ChoiKI 81
KimHL 98
KimBY 62
You must have the following output in this program.
*
* average = 65.93
* std. dev. = 6.51
* Grade '66' of 'ParkJK' is nearest to average.
* KimIS
* ParkJK
* KimBY
/*
* The following functions are given in this file.
*/
static void *malloc(int);
static void memory_free(void *);
static void check_memory(void);
/*
* Given a table of grades, compute average and standard deviation.
* Standard deviation is given via the last argument, and
* this function returns the average value.
*
* NOTE : You may use the math function "double sqrt(double)".
* average = A = (f0 + f1 + ... + fN)/N
* std_dev = sqrt((f0-A)**2 + (f1-A)**2 + ... + (fN-A)**2)/N
*/
static double get_average(int *grades, int num_persons, double *std_dev)
{
double sqrt(double);
int average;
average=*grades/num_persons;
*std_dev=sqrt((*grades-average)**2)/num_persons;
return *std_dev;
}
/*
* Given a table of names/grades and average value of the grades,
* get the person whose grade is nearest to the average.
* Name of the person is given via the last argument, and
* this function returns grade of the person.
*/
static int find_nearest(char **names, int *grades, int num_persons, double average, char **who)
{
}
/*
* Give memories back to operating system
*/
static void dispose_memories(int *grades, char **names, int num_persons)
{
free(pt);
}
/*
* Given names and grades, this function gives a table of
* names which are in the given range.
* This function returns the number of names in the range.
*/
static int get_in_range(char **names, int *grades, int num_persons, double average, double std_dev, char **names_in_range)
{
int k, count;
double mini = average - std_dev;
double maxi = average + std_dev;
double d_grade;
/*
* Main program of this programming lab.
* This main() function has program errors, modify this program
* so that this program works.
*/
void main(void)
{
FILE *fp;
double average, std_dev;
int k, grade_of_who, num_in_range, len;
char *who;
char the_name[512]; /* temporary area for name */
int num_persons; /* number of persons at 1st line of data file */
char **names; /* table of names in data file */
int *grades; /* grades of in data file */
char **names_in_range; /* table of names who are in range */
/*
* Read data file
*/
/* open data file */
fp = fopen("main2.txt", "r");
/* read the number of persons in 1st line */
fscanf(fp, "%d", &num_persons);
/*
* Compute average and standard deviation of the grades
*/
average = get_average(grades, num_persons, &std_dev);
printf("average = %3.2f\n", average);
printf("std. dev. = %3.2f\n", std_dev);
/*
* Get the person who are nearest to average.
*/
grade_of_who = find_nearest(names, grades, num_persons, average, &who);
printf("Grade '%d' of '%s' is nearest to average.\n", grade_of_who,who);
/*
* Get the persons who are in the range (min, max), where
* min = average - std_dev, max = average + std_dev.
*/
names_in_range = (char **)malloc(num_persons*sizeof(char *));
assert(names_in_range);
num_in_range = get_in_range(names, grades, num_persons, average, std_dev, names_in_range);
for(k = 0 ; k < num_in_range ; k++)
printf("%s\n", names_in_range[k]);
/*
* Epilog of this program : check whether all the memories are given
* back to operating system.
*/
dispose_memories(grades, names, num_persons);
check_memory();
}
/*
* The following functions are written to check dynamic memory
* allocation easily.
* DO NOT MODIFY THE FOLLOWING PROGRAM CODES
*/
typedef struct mem_chunk {
int code;
int size;
void *ptr;
int *pcode;
} MEM_CHUNK;
static int size_mems = 0;
static int num_mems = 0;
static MEM_CHUNK **mems = 0;
static void *malloc(int byte_count)
{
MEM_CHUNK *the_mem;
int for_code;
static void check_memory(void)
{
if(num_mems > 0) {
fprintf(stderr, "%d chunks of memories allocated by dynamic memory allocation are not given back to operating system.\n", num_mems);
exit(1);
}
}