TheAlgorithms-C/numerical_methods/mean.c

42 lines
742 B
C
Raw Normal View History

#include <limits.h>
2020-04-08 02:31:29 +03:00
#include <math.h>
#include <stdio.h>
2020-04-08 17:08:25 +03:00
#include <stdlib.h>
2020-04-08 02:31:29 +03:00
#define MAX_LEN INT_MAX
int main(int argc, char **argv)
2016-10-18 17:17:54 +03:00
{
int *a, n = 10, i, j, temp, sum = 0;
float mean;
2020-04-08 02:31:29 +03:00
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));
2020-04-08 02:31:29 +03:00
printf("Random Numbers Generated are: ");
for (i = 0; i < n; i++)
{
a[i] = rand() % 100;
printf("%2d, ", a[i]);
}
putchar('\n');
2016-10-18 17:17:54 +03:00
for (i = 0; i < n; i++) sum = sum + a[i];
2016-10-18 17:17:54 +03:00
mean = sum / (float)n;
printf("\nMean: ");
printf("%f\n", mean);
2016-10-18 17:17:54 +03:00
free(a);
return 0;
2020-04-08 02:31:29 +03:00
}