'I'm not sure why my variance function returns 0.0000 0.0000
My program runs, but my output should print the variance and the average. I was told as my input array sizes get larger and larger my average should get closer to 5000 and my variance should approach 8333333... Yet I am getting 0 for both. The only thing I can think of is that I'm filling my mydata array/memoryblock incorrectly, but I don't know another way to do it if not the way I have written.
#include <stdio.h> //printf
#include <stdlib.h> //malloc, calloc, realloc, free
#include <math.h> //pow
double average_variance(int data[], int numbers, double* varp){
double sum = 0;
//sum of array
for(int i=0; i < numbers; i++){
sum += data[i];
}
double average = sum/numbers;
double squaresum = 0;
//sum of squares
for(int i=0; i < numbers; i++){
squaresum += pow(data[i], 2);
}
//"hack" to return two values by using a pointer; otherwise you can't return two values in a function
double variance = ((squaresum/numbers) - pow(average, 2));
*varp = variance;
return average;
}
int main(int argc, char **argv)
{
int* mydata = NULL;
int arraysize;
while (1)
{
printf("Enter array size (0 to end): "); scanf("%d", &arraysize);
if (arraysize == 0)
break;
// 1. Add code to allocate memory and assign to the mydata // pointer. If mydata is already allocated, use realloc ...
if(arraysize != 0)
{
//used calloc because in the next if statement I needed to see if values were stored in it or not
mydata = calloc(arraysize, sizeof(int));
if (mydata == NULL) {return -1;}
//if mydata array is already allocated use realloc to make a new memory block
if(mydata != 0)
{
mydata = realloc(mydata, arraysize * sizeof(int));
if (mydata == NULL) {return -1;}
}
}
// 2. Add code to put random numbers in the array
for(; mydata < &mydata[-1]; mydata++)
{
int randnum = random() % 10000;
mydata = &randnum;
}
//included
double variance;
double avg = average_variance(mydata, arraysize, &variance);
printf("average = %f variance = %f\n", avg, variance);
}
free(mydata);
Solution 1:[1]
The array initialization code is broken:
for(; mydata < &mydata[-1]; mydata++)
{
int randnum = random() % 10000;
mydata = &randnum;
}
Errors:
mydata[-1]is not the last element ofmydata(a'la Python) but it is an element just before the array. This element does not exist, Undefined Behavior is invoked.- the
mydatais moved during the initiazation to a local variable in the loop. There is no hope for this construct to work.
Just do:
for (int i = 0; i < arraysize; ++i)
mydata[i] = random() % 10000;
Moreover, the allocation code is needlessly convoluted. Just replace:
if(arraysize != 0)
{
//used calloc because in the next if statement I needed to see if values were stored in it or not
mydata = calloc(arraysize, sizeof(int));
if (mydata == NULL) {return -1;}
//if mydata array is already allocated use realloc to make a new memory block
if(mydata != 0)
{
mydata = realloc(mydata, arraysize * sizeof(int));
if (mydata == NULL) {return -1;}
}
}
with:
mydata = realloc(mydata, arraysize * sizeof *mydata);
if (!mydata) return -1;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | tstanisl |
