Trying to understand malloc and calloc, so I watched some of TheNewBoston's video about the subject. But for some reason I get a really strange output, let me show the code to start with.
#include <stdio.h>
#include <stdlib.h>
int
main()
{
int i, howMany;
int total;
float average = 0.0;
int * pointsArray;
printf("How many do you want to store: \n");
scanf("%d", &howMany);
pointsArray = (int *) malloc(howMany * sizeof(int));
printf("Enter your numbers: \n");
for(i = 0; i < howMany; i++)
{
scanf("%d", &pointsArray[i]);
total += pointsArray[i];
}
average = (float)total / (float)howMany;
printf("Average is: %f\n", average);
free(pointsArray);
return 0;
}
It is supposed to calculate the mean value of the numbers you enter. The problem is that the value of the variable "total" get a start value of 4195808 for some reason. First I made an own "interpretation" of TheNewBoston's code, but after I got this error I just copied it but the same problem remains. Why does this happen? I can not find any info about anyone having a similar problem.