Skip to content

Commit 9f16bcb

Browse files
committed
median done
1 parent 77938e7 commit 9f16bcb

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

questions/question24.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
3. The median of a finite list of numbers can be found by arranging all the numbers from the lowest value to
3+
the highest value, and then picking the middle one (e.g., the median of {3, 3, 5, 9, 11} is 5). Your task is to take
4+
"N" real values, probably containing repitions, through command-line, and find the median of those numbers.
5+
Remember that all commnad line arguments are of "char *" type, hence you need to use the "atof()" function.
6+
INPUT: Example: ./a.out 2.355 4.5 3.6 2.12 3.6 6.4 2.355
7+
OUTPUT: 3.6 (median of the given numbers)
8+
*/
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
#include <string.h>
12+
13+
void insertionSort(int arr[], int n){
14+
int i, key, j;
15+
for (i = 1; i < n; i++){
16+
key = arr[i];
17+
j = i-1;
18+
while (j >= 0 && arr[j] > key){
19+
arr[j+1] = arr[j];
20+
j = j-1;
21+
}
22+
arr[j+1] = key;
23+
}
24+
}
25+
26+
void print_array(int array[],int n){
27+
for (int i = 0; i < n; ++i){
28+
printf("%d\t",array[i] );
29+
}
30+
printf("\n");
31+
}
32+
33+
int main(int argc, char const *argv[]){
34+
35+
int n = argc-1;
36+
int array[argc];
37+
for(int i=1;i<argc; i++){
38+
array[i-1] = atof(argv[i]);
39+
}
40+
41+
insertionSort(array, n);
42+
print_array(array,n);
43+
44+
if(n%2==0){
45+
printf("The median of given numbers is:%f\n",(float)(array[n/2]+array[n/2-1])/2);
46+
}else{
47+
printf("The median of given numbers is:%f\n",(float)(array[n/2]));
48+
}
49+
50+
}

0 commit comments

Comments
 (0)