Output:
C Codes:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
int main() | |
{ | |
int i, j, n; | |
float temp, median; | |
printf("Enter the Sample Size: "); | |
scanf("%d", &n); | |
float x[n]; | |
printf("Enter the Data: "); | |
for(i = 1; i <= n; ++i){ | |
scanf("%f", &x[i]); | |
} | |
/*Sort the Data*/ | |
for(i = 1; i <= n; ++i){ | |
for(j = i + 1; j <= n; ++j){ | |
if(x[i] > x[j]){ | |
temp = x[i]; | |
x[i] = x[j]; | |
x[j] = temp; | |
} | |
} | |
} | |
/*Sorted Data*/ | |
printf("Sorted Data: "); | |
for(i = 1; i <= n; ++i){ | |
printf("%2.2f ", x[i]); | |
} | |
printf("\n"); | |
/*Compute the Median*/ | |
if(n % 2 == 0){ | |
median = (x[n / 2] + x[n / 2 + 1]) / 2; | |
} | |
else{ | |
median = x[n / 2 + 1]; | |
} | |
printf("Median: %2.2f\n", median); | |
return 0; | |
} |
C++ Codes:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<iomanip> | |
using namespace std; | |
int main() | |
{ | |
int i, j, n; | |
float temp, median; | |
cout << "Enter the Sample Size: "; | |
cin >> n; | |
float x[n]; | |
cout << "Enter the Data: "; | |
for(i = 1; i <= n; ++i){ | |
cin >> x[i]; | |
} | |
/*Sort the Data*/ | |
for(i = 1; i <= n; ++i){ | |
for(j = i + 1; j <= n; ++j){ | |
if(x[i] > x[j]){ | |
temp = x[i]; | |
x[i] = x[j]; | |
x[j] = temp; | |
} | |
} | |
} | |
/*Sorted Data*/ | |
cout << "Sorted Data: "; | |
for(i = 1; i <= n; ++i){ | |
cout << x[i] << " "; | |
} | |
/*Compute the Median*/ | |
if(n % 2 == 0){ | |
median = (x[n / 2] + x[n / 2 + 1]) / 2; | |
} | |
else{ | |
median = x[n / 2 + 1]; | |
} | |
cout << endl; | |
cout << "Median: " << setprecision(4) << median << endl; | |
return 0; | |
} |