Finding Median Value From Array C Programming

In previous post we learned how to write a C program to find mean value from array elements. In this post we will learn how to find median value from array elements. Median value is the centered value in sorted (ascending order) elements. We also learned in our previous post about how to sort array elements in ascending order using bubble sort function. In following simple program we will create a function which will accept an array as an input. Furthermore that function will return median value from the array elements.


#include <stdio.h>

int median(int arr[], int size); // function prototype
void bubbleSort(int arr[], int size); // function prototype

int main(void){

   int arrayName[10] = {90,44,33,83,49,34,51,84,56,44}; // defined an array with 10 elements

   //output each element of array
   printf("Original values of the array\n\n");
   printf("Array index\t\t\tValue\n");
   for(int j=0; j<10; j++){
      printf("%11d\t\t\t%4d\n", j, arrayName[j]);
   }

   int a = median(arrayName, 10); // function call

   printf("\n\nMedian Value is: %d", a);

   return 0;
}

//function definition
int median(int arr[], int size){
   bubbleSort(arr, size);
   int middle = size/2;
   return arr[middle-1];
}

//function definition
void bubbleSort(int arr[], int size){
   //BubbleSort algorithm will sort the array elements in ascending order. Outer loop is for passes and inner loop will do comparisons
   for(int l=1; l<=size; l++){
      for(int m=0; m<size-1; m++){
         if(arr[m]>arr[m+1]){
            int temp;
            temp = arr[m];
            arr[m] = arr[m+1];
            arr[m+1] = temp;
         }
      }
   }
}

 

Output:

Hits: 1

Spread the love
  •  
    1
    Share
  • 1
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *