-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]);
}
solid.py
  • 2,782
  • 5
  • 23
  • 30
  • I could not write down the actual 31 values because of auto-fit issues with this site. – solid.py Dec 31 '13 at 12:52
  • change to `day_avg(jul,aug);` – BLUEPIXY Dec 31 '13 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.) – T.J. Crowder Dec 31 '13 at 12:55

4 Answers4

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)

Jite
  • 4,250
  • 1
  • 17
  • 18
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.

haccks
  • 104,019
  • 25
  • 176
  • 264
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);

Chinna
  • 3,930
  • 4
  • 25
  • 55
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);

Digital_Reality
  • 4,488
  • 1
  • 29
  • 31