My code is supposed to take command line arguments argv[1]: trials argv[2]: samples argv[3]: minimum val argv[4]: max val
I wrote a loop from 0 to argv[1] to get the number of trials specified. However, the data does not change for each trial.
int main(int argc, char *argv[])
{
for (int i = 1; i < argc; i ++)
{
if (is_valid_int(argv[i]) == 1)
{
continue;
}
else
{
printf("%s is not a valid integer\n", argv[i]);
}
}
for (int k = 0; k < atoi(argv[1]); k++)
{
int size = atoi(argv[2]);
int lower = atoi(argv[3]);
int upper = atoi(argv[4]);
int *min = NULL;
int *max = NULL;
double *mean = NULL;
double *stddev = NULL;
int *result = generate_population(size, lower, upper);
get_stats(result, size, min, max, mean, stddev);
}
return 0;
}
void get_stats(int *a, int size, int *min, int *max, double *mean, double *stddev)
{
min = &a[0];
max = &a[0];
double tempMean = 0;
for (int i = 0; i < size; i ++)
{
printf("%d\n", a[i]);
tempMean = tempMean + a[i];
}
tempMean = tempMean / size;
mean = &tempMean;
printf("mean: %g\n", *mean);
double sumSquared = 0;
for (int l = 0; l < size; l++)
{
sumSquared = sumSquared + ((a[l] - *mean)*(a[l] - *mean));
}
double tempStandardDev = sqrt((sumSquared/size));
stddev = &tempStandardDev;
printf("standard Dev: %g\n", *stddev);
for (int j = 0; j < size; j++)
{
if (a[j] < *min)
{
min = &a[j];
}
}
printf("Minimum: %d\n", *min);
for (int k = 0; k < size; k++)
{
if (a[k] > *max)
{
max = &a[k];
}
}
printf("max: %d\n", *max);
free(a);
}
int *generate_population(int size, int lower, int upper)
{
int *my_array=(int*)malloc(size * sizeof(int));
srand(time(NULL));
for (int i = 0; i < size; i++)
{
my_array[i] = (rand() % (upper - lower + 1)) + lower;
}
return my_array;
}
Is my srand() in the wrong location? I am also suspecting im incorrectly using pointers which is causing me not to change the value after each trial.