I'm working on a school project. Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
float addNumbers(float *array){
float t;
int len = sizeof(array) / sizeof(array[0]);
for(int i = 0; i < len;i++){
t += array[i];
}
return t;
};
int main(){
float n;
float* v = (float*)malloc(10 * sizeof(float));
int counter = 1;
for(int i = 0;i < 4;i++){
printf("Insert N%d: ", counter);
counter++;
scanf("%f", &n);
v[i] = n;
printf("%f\n", n);
}
int len = sizeof(v) / sizeof(v[0]);
for(int i = 0;i < len;i++){
printf("%f", v[i]);
}
free(v);
float x = addNumbers(v);
printf("%f", x);
}
The output of this code is:
Insert N1: 5
5.000000
Insert N2: 7
7.000000
Insert N3: 8
8.000000
Insert N4: 3
3.000000
5.000000-1.#QNAN0
I discovered that the problem is how the array stores the values. The values below the Insert N$, are the values of n. 5.000000-1.#QNAN0
, however,
is the value stored inside the array, but it's only one. I decided to give the array more memory, but that doesn't work. That's all i know