Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I made a program which accepts input for 30 users (About their age) and then the array was supposed to be an input in a custom function made by me (avg_age) However the average printed on screen is 0.0000 (for 30 non-zero values) That is why I think it does not return anything.

#include <stdio.h>

float avg_age(int age[]);

main()

{
    int i=0,age[30]={0},intemp;

do{
    printf("Input age for 30 users: ");
    scanf("%d",&intemp);
    if(intemp>0 && intemp<100)
        intemp=age[i];
    else i--;
    i++;
}while(i<30 || intemp<0 || intemp>100);

printf("\nAverage age of 30 users: %f\n",avg_age(age));




float avg_age(int age[]){
    int i,avg=0;
    for(i=0;i<30;i++)
        avg+=age[i];
    avg=(float)avg/30;
    return avg;
    }

}
share|improve this question
5  
intemp=age[i];=> age[i]=intemp;. You're not storing the ages in the array. –  cactus1 Dec 30 '13 at 20:30

2 Answers 2

up vote 4 down vote accepted

Take out the function definition of avg_age out of the main. You declared avg as int but it should be declared float to store float values.

float avg = 0.0;  

In main intemp=age[i]; is not storing the inputs to the array age instead assigning 0 each time to intemp. Change it to

age[i] = intemp;  

Your modified code: (for 5 users)

#include <stdio.h>
#define N 5

float avg_age(int age[]);

int main(void)
{
    int i=0,age[N]={0},intemp;

    do{
       printf("Input age for %d users: ", N);
       scanf("%d",&intemp);
       if(intemp>0 && intemp<100)
           age[i] = intemp;
       else i--;
       i++;
    }while(i<N || intemp<0 || intemp>100);

    printf("\nAverage age of %d users: %f\n",N, avg_age(age));
    return 0;
}

float avg_age(int age[]){
    int i;
    float avg=0.0;
    for(i=0;i<N;i++)
        avg+=age[i];
    avg=(float)avg/N;
    return avg;
 }  

Here is the tested code.

share|improve this answer
    
tcc:error:(line) 46:incompatible types for redefinition of avg –  niCk Dec 30 '13 at 20:51
2  
Working fine : ideone.com/bQ5RoZ –  haccks Dec 30 '13 at 20:56
    
You are right; I am terribly sorry; thanks for the assistance!!! –  niCk Dec 30 '13 at 21:03
1  
Down voter care to explain! –  haccks Dec 30 '13 at 21:08
1  
@ChronoKitsune; I do not think that was the issue although I fixed that. –  haccks Dec 30 '13 at 21:32

Your avg inside avg_age should be a float

float avg_age(int age[]){
  int i; float sum=0.0;
  for(i=0;i<30;i++)
     sum += age[i];
  return sum/30;
}

and of course the above function should be outside of main (preferably before it).

BTW, you should have compiled with all warnings and debugging info (e.g. with gcc -Wall -g). The compiler certainly would have warned you! Also, learn how to use the debugger (e.g. gdb)

share|improve this answer
    
It is outside main() but I did a little mistake when I was uploading –  niCk Dec 30 '13 at 20:43
    
I use Dev-C++ 4.9.9.4 and Turbo C compiler. –  niCk Dec 30 '13 at 20:44
    
Both of them are outdated. Turbo C is obsolete now. Developers stopped giving support to Dev-C++ 4.9.9.4. –  haccks Dec 30 '13 at 20:50
1  
@niCk: Consider switching to Linux... It is free, and you will learn a lot! So install it on your computer! –  Basile Starynkevitch Dec 30 '13 at 20:53
    
Τhanks for the advice but I have been instructed to run some things in Windows and then upload the scripts to the UNIX server of my university –  niCk Dec 30 '13 at 20:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.