Question:

What's wrong with the below C program??

by  |  earlier

0 LIKES UnLike

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:

   Report

3 ANSWERS


  1. Did you write this?  What is it supposed to do?  What is the problem you have?  Expecting someone to figure out what you&#039;re trying to do and figure out why it&#039;s not doing it is asking for psychic help.

    At a glance, the function name is calc_stats, not calc_stat.  Be consistent.

    main has to return an int

    Where does the data come in?  Don&#039;t see it anywhere.

    What is supposed to happen in function input_datar?   All your processing is in a while statement that says while indx &lt; maxdata so obviously the condition if (indx &gt;= maxdata) isn&#039;t going  to occur ever.  Do your error checking elsewhere.

    I gave up after that.  State your specs, state your problems, and post again.


  2. It would help if you described what the program is suppose to do, instead of having us guess as to what it is suppose to do.

  3. The function input_data() does not really accept any input. It declares a variable named &quot;buffer&quot; on stack (locally) and uses it without initializing or reading anything into it.

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions