#include <stdio.h>
#include <stdlib.h>
double average(int arrx[6], int *ptr);
int main(void){
int arry[6]= {2,5,4,5,6,7};
int count=0;
double avg;

avg=average(arry,&count);
printf("The average is %.2f\n",avg);
printf("Number bigger than average is %i", count); 
return 0;
}

double average(int arrx[6], int *ptr){
int c=0;
int sum=0;
double a=0;
for(c=0;c<6;c++){
sum=(double)sum+arrx[c];
}
a=sum/6;
for(c=0;c<6;c++){
if(arrx[c]>a){
*ptr = *ptr+1;
}
}
return a,*ptr;
}

The the program is working but shows wrong answer in average. it should be 4.83 but the program shows 4.00. there is no problem on number bigger than average, it is showing the correct answer. i think i done something wrong on my function and return value or the function. anyone can help me with this?

sum/6 is an int divided by an int. An int divided by an int will give you back an int, always. What's 3/2? 1. What's 29/6? 4.

To get a double answer, you need one of the two values to be a double. An easy way to do this is to make sum a double.

Edited 3 Years Ago by Moschops

ok, i change the int sum into double during the division but i still wont get the correct answer.

a=(double)sum/6;

can you help me check am i correct about the function on line 3,9,15,28. because even i change my code int sum to double sum, it still show the same result.

line 17: Should be double sum = 0;
line 28: You can't return 2 parameters

#include <stdio.h>
#include <stdlib.h>

double average(int arrx[6], int *ptr);

int main(void){
    int arry[6]= {2, 5, 4, 5, 6, 7};
    int count = 0;
    double avg;

    avg = average(arry, &count);
    printf("The average is %.2f\n", avg);
    printf("The first number bigger than the average is %i", count);

    printf("\n\npress Enter to exit...");
    getchar();

    return 0;
}

double average(int arrx[6], int *ptr){
    int c = 0;
    double sum = 0;
    double a = 0;

    for(c = 0; c < 6 ; c++){
        sum = (double)sum + arrx[c];
    }
    a = sum/6;

    /* Set count to the first number greater than the average */
    for (c = 0; c < 6; c++){
        if(arrx[c] > a){
            *ptr = arrx[c];
            break;
        }
    }
    return a;
}
Comments
thx, you solve my problem by remind me can't return 2 parameters.
This article has been dead for over six months. Start a new discussion instead.