Write a program to Calculate the Median of the array.
Median: It is defined as the middle element of the array in a sorted array, if the array contains an even number of terms then the median will be the average of the middle two elements
Example:
Input: arr[]={3,4,5,6,7,2} Output: Median is 4.500000
C Program
#include <stdio.h>void sortArray(int arr[], int n){for (int i = 0; i < n; i++){for (int j = 0; j < n - i - 1; j++){if (arr[j] > arr[j + 1]){int temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}}int main(){int arr[] = {3, 4, 5, 6, 7, 2};int n = sizeof(arr) / sizeof(arr[0]);sortArray(arr, n);if (n % 2 == 0){float median = (float)(arr[n / 2] + arr[n / 2 - 1]) / 2;printf("Median is %f ", median);}else{float median = (float)(arr[n / 2]);printf("Median is %f ", median);}return 0;}
No comments:
Post a Comment