-1

I wanted to make a program which obtains temperature of each day of 2 months
and prints the average for each day
The error code is at the line where the day_avg() is called.
(Cannot Cast float to float*)

#include <stdio.h>
void day_avg(float month[],float month2[]);

int main()
{
    float jul[31]={ 31,28,31,30,31,30,31,31,30,31,30,31 };
    float aug[31]={ 31,28,31,30,31,30,31,31,30,31,30,31 };
    day_avg(jul[31],aug[31]);
}
void day_avg(float month[],float month2[]){
    int i;
    float avg[31]={0};
    for(i=0;i<31;i++)
        avg[i]=(month[i]+month2[i])/2.0;
    for(i=0;i<31;i++)
        printf("\nAverage of temperature of 2 months for day %d :%.1f",i+1,avg[i]);
}
3
  • I could not write down the actual 31 values because of auto-fit issues with this site. Commented Dec 31, 2013 at 12:52
  • change to day_avg(jul,aug); Commented Dec 31, 2013 at 12:52
  • "I could not write down the actual 31 values because of auto-fit issues with this site." Sure you can. A horizontal scrollbar will appear if necessary. (There's no particular need to, the question is clear enough without, but the site isn't preventing you from doing it.) Commented Dec 31, 2013 at 12:55

4 Answers 4

2

Your call to day_avg(jul[31],aug[31]) should be day_avg(jul, aug)

jul and aug are references to arrays, they know how big they are so specifying it in function calls like that is wrong and doesn't do what you might think. What actually happens is that you send the value from array index 31 of jul and aug instead of the whole array.

Second problem is that array index 31 is out of bounds for your arrays, valid indexes are 0-30 in an array of size 31.

You want to send in the whole array, temperature data for all days, hence day_avg(jul, aug)

1
1

Your void day_avg(float month[],float month2[]) function is expecting pointer and you are passing value. You should pass like this day_avg(jul,aug);

1

You are calling your function wrong

day_avg(jul[31],aug[31]);  

day_avg expects arguments of type float *.
As array names are decays to pointer when passed to functions as arguments (not always), you can call it as

 day_avg(jul, aug); 

EDIT: As per the OP's comment: I defined the input as month[] not month* , i thought the array itself was the input

Compiler parsed

void day_avg(float month[], float month2[]);  

as

void day_avg(float *month, float *month2);  

You can't pass an entire array to a function but address to the first element.


SIDE NOTE:
Also if day_avg made to accept argument of type float then you can't pass jul[31],aug[31] as array indexing statrts from 0 in C.

0
0

Two problems:

Missing ; at the end

float jul[31]={ 31,28,31,30,31,30,31,31,30,31,30,31 };
float aug[31]={ 31,28,31,30,31,30,31,31,30,31,30,31 };

Second function should be like:

day_avg(jul,aug);

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.