In my program the user allocates a number of random float values to a pointer array. After that I wish to use the values generated in the array in a function to calculate the mean-value, but I dont get the correct values
This is what I use to allocate random float numbers to the pointer *arr
for (int i = 0; i < antalVarde; i++)
{
if (i % 10 == 0)
printf("\n");
*arr = RandomReal(low, high);
printf("%.1f ", *arr);
}
And this is my function to calculate the mean-value.
float mean(int antalVarde, float *arr)
{
float medel = 0.0;
for (int i = 0; i < antalVarde; i++)
{
medel = medel + *(arr + i);
}
medel = medel / antalVarde;
return medel;
}
When I debug while stepping-into the code it gets messed up when I get to
medel = medel + *(arr + i);
Can this not be used to access the values in a pointer array?
Forgive me if it's a poorly asked question. grateful for answers.
You don't increment pointer arr
in the initialisation loop. Only the first float
at the addresse pointed by arr
is written.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.