Hi guys,
I would like to request for help on the below program.
I have spent about 2 weeks figuring out whats wrong with the program.
This is not a homework. it is a self-study material given by my instructor during summer holiday.
Hope you guys can help or provide some hints. Thanks!
#include<stdio.h>
#include<ctype.h>
#define MAX_LINE 256
#define MAX_DATA 500
double data[MAX_DATA];
int index = 0;
double sum_data;
double ave_data;
double max_data;
double min_data;
double mid_data;
void input_data(void);
void calc_stats(void);
void output_data(void);
int main(void){
input_data();
calc_stats();
output_data();}
void input_data(void){
char buffer[MAX_LINE];
while(index<MAX_DATA){
if(index>=MAX_DATA){
printf("Overlimit\n");
printf("Maximum no. of data is %d\n",MAX_DATA);
exit(-1);}
else if(isdigit(buffer[0])||buffer[0]=='.'){
data[index]=(double)atof(buffer);
index++;}
else{
printf("%s",buffer);}}}
void calc_stat(void){
int i,j;
double tmp;
sum_data = 0.0;
for(i=0;i<index;i++){
sum_data +=data[i];}
ave_data = sum_data/index;
if(index>0){
min_data = data[0];
max_data = data[0];}
for(i=0;i<index;i++){
if(min_data > data[i]){
min_data = data[i];}
if(max_data < data[i]){
max_data = data[i];}}
for(i=0;i<index-1;i++){
for(j=i+1;j < index;j++){
if(data[i] > data[j]){
tmp = data[i];
data[i] = data[j];
data[j] = tmp;}}}}
void output_data(void){
printf("No of data: %d\n",index);
printf("Total is %g\n",sum_data);
printf("Average is %g\n",ave_data);
printf("Max no is %g\n",max_data);
printf("Min no is %g\n",min_data);
printf("Middle no is %g\n",mid_data);}
Tags: