Using the same machine and IDE as reffered in my other question (third paragraph at Problems in code or my IDE/comp is bugged?)
I try to run this code:
#include <stdio.h>
#define n 3
int main()
{
int i;
float values[n],sumval,svmean,tmp;
for(i=0;i<n;++i)
{
scanf("%f",&tmp);
values[i]=tmp;
sumval = sumval + values[i];
}
svmean = sumval/n;
printf("%f \n",svmean);
return(0);
}
The above code is supposed to run this formula
That means that it has to add some values and divide the result by their total number.
As you see above I made an array with random n
positions and I ask the user to fill in a value for each position then add them all up and divide them.
The problem is that it doesnt work. It outputs only the result 7 no matter what the iput is.
BUT if I include stdlib.h to the code it works fine.
so
- Question A: why the code does not work properly using only the stdio.h library? which element of the code does require the stdlib.h library?
As you see the array values[n]
seems to have an random n
number of cells but actually I have already set this numer to be equal to 3 (using #define
)
- Question B: Is there a way to run a code with the same porpuse but letting the user to define the size of the array
values[n]
or in other words let the user input an integer that sets the value ofn
invalues[n]
?