mirror of
https://github.com/TheAlgorithms/C
synced 2024-11-24 06:19:37 +03:00
28fc15c65b
* fixed possible segmentation fault Fixed possible segmentation fault when no arg is supplied * Update mean.c various small changes to print statements.
42 lines
742 B
C
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;
|
|
}
|