This supplemental answer is in response to the OP's comment on my earlier answer. Defender asks:
"I have some questions left. If it's expecting an array of type int and i sent double why this would change the result in 360 degrees. I still didn't get an answer to that."
It'll be easier to illustrate this with a type clash between short int
and int
, but the same idea applies to int
vs. double
.
Take a look at this code:
#include <stdlib.h>
#include <stdio.h>
void functionA(short int[], int size);
int main(void) {
printf("sizeof short int : %lu\n", sizeof(short int));
printf("sizeof int : %lu\n", sizeof(int));
printf("\n\n");
printf("==== sending short int to functionA() ====\n");
short int shortdata[4] = {10, 20, 50, 100};
functionA(shortdata, 4);
printf("==== sending int to functionA() ====\n");
int intdata[4] = {10, 20, 50, 100};
functionA(intdata, 4);
}
void functionA(short int arr[], int size) {
int i;
char* ptr;
for (i = 0; i < size; ++i) {
ptr = &arr[i];
printf("bytes of 'arr[%d]' : %x %x\n", i, *ptr, *(ptr+1));
printf("value of 'arr[%d]' : %d\n", i, arr[i]);
printf("\n");
}
}
which produces this output:
sizeof short int : 2
sizeof int : 4
==== sending short int to functionA() ====
bytes of 'arr[0]' : a 0
value of 'arr[0]' : 10
bytes of 'arr[1]' : 14 0
value of 'arr[1]' : 20
bytes of 'arr[2]' : 32 0
value of 'arr[2]' : 50
bytes of 'arr[3]' : 64 0
value of 'arr[3]' : 100
==== sending int to functionA() ====
bytes of 'arr[0]' : a 0
value of 'arr[0]' : 10
bytes of 'arr[1]' : 0 0
value of 'arr[1]' : 0
bytes of 'arr[2]' : 14 0
value of 'arr[2]' : 20
bytes of 'arr[3]' : 0 0
value of 'arr[3]' : 0
The first two lines of output show that on my machine, short int
takes 2 bytes of memory and int
takes 4 bytes.
functionA()
is expecting a short int
array, and when I send it a short int[]
, we see the expected output. The bytes that make up the first element of the array are 0x0a 0x00
, which in decimal is "10"; the bytes that make up the second element of the array are 0x14 0x00
, which in decimal is "20"; and so on.
But when I send functionA()
an int[]
, I'm sending 4 bytes per element, so when it iterates through the array, it doesn't see the elements correctly. It does okay with the first element, but only because it's a small number; when it looks for the second element, it is actually looking at the last two bytes of the first element, so it sees 0x00 0x00
, or "0"; when it looks for the third element it is looking at the first two bytes of the second element, and sees 0x14 0x00
, or "20"; and so on.
Another way to show it is that the bytes of shortdata
are this:
0a 00 14 00 32 00 64 00
and the bytes of intdata
are this:
0a 00 00 00 14 00 00 00 32 00 00 00 64 00 00 00
Because functionA()
is expecting a short int[]
, it treats intdata
that way -- two bytes per element -- and sees as its elements:
arr[0] : 0a 00
arr[1] : 00 00
arr[2] : 14 00
arr[3] : 00 00
It's a similar story with your code. Your getAverage()
is expecting a int[]
, so when you send it a double[]
, it is not seeing the bytes the way you intend.
(The clash between int
and double
is even more drastic than between short int
and int
; this is because in addition to being different sizes (on most modern machines, int
is 4 bytes and double
is 8 bytes), floating-point numbers are stored as an exponent and a mantissa -- so the byte values have an entirely different meaning.)
I hope this explanation helps, and I encourage you to experiment with the code above, and read about the internals of C memory as it pertains to allocating and handling variables. As a new C programmer, you will gain insight in to fundamentals that will help you for as long as you continue programming in C.