-1

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

}
1
  • 5
    intemp=age[i];=> age[i]=intemp;. You're not storing the ages in the array. Commented Dec 30, 2013 at 20:30

2 Answers 2

4

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.

4
  • I'm not the downvoter, but I will say that you might explain the reasoning behind what you said instead of just saying "do it this way". Commented Dec 30, 2013 at 21:24
  • 1
    @ChronoKitsune; I do not think that was the issue although I fixed that. Commented Dec 30, 2013 at 21:32
  • Quibble: the number of entries in the array should be passed as an argument to avg_age, rather than having the function rely on the presence of a global symbol like N. Loose coupling is a good habit to get into as early as possible. Commented Dec 30, 2013 at 22:29
  • @JohnBode; Passing array size is a good practice. But I do not think N is causing any problem here. Commented Dec 30, 2013 at 22:33
0

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)

4
  • Both of them are outdated. Turbo C is obsolete now. Developers stopped giving support to Dev-C++ 4.9.9.4. Commented Dec 30, 2013 at 20:50
  • 1
    @niCk: Consider switching to Linux... It is free, and you will learn a lot! So install it on your computer! Commented Dec 30, 2013 at 20:53
  • Which scripts? You have only a C source file! Your instructor cannot see if you are using Linux, Windows or MacOSX (or Hurd or BeOS) on your own computer ... Commented Dec 30, 2013 at 20:56
  • If forced to use Windows, look at MinGW as a nice platform to have a bash shell, the gcc toolchain and other tools you're familiar with from Ubuntu. Simpler than full-blown Cygwin. Commented Dec 30, 2013 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.