pointer problem

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

*****************************************
#include <stdio.h>
#include <math.h>
#include <assert.h>

#define cfree(x) free(x)

/*
* 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;

count = 0;
for(k = 0 ; k < num_persons ; k++) {
d_grade = (double)(grades[k]);
if(d_grade <= maxi && d_grade >= mini) {
names_in_range[count] = names[k];
count++;
}
}
return(count);
}


/*
* 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);

/* read name grade pair */
for(k = 0 ; k < num_persons ; k++) {
fscanf(fp, "%s", the_name);
len = strlen(the_name);
names[k] = (char *)malloc((len+1)*sizeof(char));
strcpy(names[k], the_name);
fscanf(fp, "%d", &(grades[k]));
}
fclose(fp);

/*
* 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;

if(byte_count <= 0)
return((void *)0);

the_mem = (MEM_CHUNK *)calloc(1, sizeof(MEM_CHUNK));
if(!the_mem)
return((void *)0);
the_mem->ptr = (void *)calloc(1, byte_count);
if(!(the_mem->ptr)) {
cfree(the_mem);
return((void *)0);
}
the_mem->size = byte_count;
for_code = (int)(the_mem->ptr);
the_mem->code = byte_count ^ for_code;
the_mem->pcode = (int *)calloc(1, sizeof(int));
if(!(the_mem->pcode)) {
cfree(the_mem->ptr);
cfree(the_mem);
return((void *)0);
}
*(the_mem->pcode) = the_mem->code;
if(!mems) {
size_mems = 128;
mems = (MEM_CHUNK **)calloc(2*size_mems, sizeof(MEM_CHUNK *));
assert(mems);

}
else if(num_mems >= size_mems) {
int k;
MEM_CHUNK **temp_mems = mems;
mems = (MEM_CHUNK **)calloc(2*size_mems, sizeof(MEM_CHUNK *));
assert(mems);
for(k = 0 ; k < size_mems ; k++)
mems[k] = temp_mems[k];
if(temp_mems)
cfree(temp_mems);
size_mems += size_mems;
}
mems[num_mems] = the_mem;
num_mems++;
return(the_mem->ptr);
}


static void memory_free(void *ptr)
{
int k, for_code, size;

assert(mems);
assert(size_mems > 0);
assert(num_mems > 0);
for(k = 0 ; k < num_mems ; k++) {
if(mems[k] && (mems[k]->ptr == ptr)) {
if(mems[k]->code != *(mems[k]->pcode)) {
fprintf(stderr, "\nMemory corrupted.\n");
exit(1);
}
for_code = (int)ptr;
size = mems[k]->code ^ for_code;
if(mems[k]->size != size) {
fprintf(stderr, "\nMemory corrupted.\n");
exit(1);
}
cfree(mems[k]->pcode);
cfree(mems[k]->ptr);
cfree(mems[k]);
mems[k] = mems[num_mems-1];
num_mems--;
if(!num_mems) {
cfree(mems);
mems = (MEM_CHUNK **)0;
size_mems = 0;
}
return;
}
}
fprintf(stderr, "\nInvalid argument at function call \"memory_free()\".\n");
exit(1);
}


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);
}
}


Last edited on
Did you write that yourself? If you wrote all that clever memory stuff, you should have no problem calculating an average.
Topic archived. No new replies allowed.