TheAlgorithms-C/numerical_methods/median.c

52 lines
730 B
C
Raw Normal View History

2020-04-08 07:31:29 +08:00
#include <stdio.h>
#include <math.h>
2020-04-08 21:52:08 +08:00
#include <stdlib.h>
2020-04-08 07:31:29 +08:00
int main()
2016-10-18 22:17:54 +08:00
{
2020-04-08 07:31:29 +08:00
int a[10], n, i, j, temp;
float mean, median;
2016-10-18 22:17:54 +08:00
printf("Enter no. for Random Numbers :");
2020-04-08 07:31:29 +08:00
scanf("%d", &n);
for (i = 0; i < n; i++)
2016-10-18 22:17:54 +08:00
{
2020-04-08 07:31:29 +08:00
a[i] = rand() % 100;
2016-10-18 22:17:54 +08:00
}
printf("Random Numbers Generated are :\n");
2020-04-08 07:31:29 +08:00
for (i = 0; i < n; i++)
2016-10-18 22:17:54 +08:00
{
2020-04-08 07:31:29 +08:00
printf("\n%d", a[i]);
2016-10-18 22:17:54 +08:00
}
printf("\n");
printf("\nSorted Data:");
2020-04-08 07:31:29 +08:00
for (i = 0; i < n; i++)
2016-10-18 22:17:54 +08:00
{
2020-04-08 07:31:29 +08:00
for (j = 0; j < n; j++)
2016-10-18 22:17:54 +08:00
{
2020-04-08 07:31:29 +08:00
if (a[i] < a[j])
2016-10-18 22:17:54 +08:00
{
2020-04-08 07:31:29 +08:00
temp = a[i];
a[i] = a[j];
a[j] = temp;
2016-10-18 22:17:54 +08:00
}
}
}
2020-04-08 07:31:29 +08:00
for (i = 0; i < n; i++)
2016-10-18 22:17:54 +08:00
{
2020-04-08 07:31:29 +08:00
printf("\n%d", a[i]);
2016-10-18 22:17:54 +08:00
}
2020-04-08 07:31:29 +08:00
if (n % 2 == 0)
2016-10-18 22:17:54 +08:00
{
2020-04-08 07:31:29 +08:00
median = (a[n / 2] + a[(n / 2) - 1]) / 2;
2016-10-18 22:17:54 +08:00
}
else
{
2020-04-08 07:31:29 +08:00
median = a[n / 2];
2016-10-18 22:17:54 +08:00
}
2020-04-08 07:31:29 +08:00
printf("\nMedian is : %f", median);
return 0;
}