TheAlgorithms-C/numerical_methods/mean.c
Satbek Abdyldayev 28fc15c65b
fix: possible segmentation faults in numerical_methods/mean.c (#805)
* fixed possible segmentation fault

Fixed possible segmentation fault when no arg is supplied

* Update mean.c

various small changes to print statements.
2021-02-23 12:36:43 -06:00

42 lines
742 B
C

#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_LEN INT_MAX
int main(int argc, char **argv)
{
int *a, n = 10, i, j, temp, sum = 0;
float mean;
if (argc == 2)
{
n = atoi(argv[1]);
if (n >= MAX_LEN)
{
fprintf(stderr, "Maximum %d!\n", MAX_LEN);
return 1;
}
}
a = (int *)malloc(n * sizeof(int));
printf("Random Numbers Generated are: ");
for (i = 0; i < n; i++)
{
a[i] = rand() % 100;
printf("%2d, ", a[i]);
}
putchar('\n');
for (i = 0; i < n; i++) sum = sum + a[i];
mean = sum / (float)n;
printf("\nMean: ");
printf("%f\n", mean);
free(a);
return 0;
}