Write a program to find the median of a user-defined array using bubble sort
#include<stdio.h>
//This is a function to find an array of user defined array
int median(int array[], int size_array)
{
if (size_array % 2 != 0)
return array[size_array/2];
return (array[(size_array-1)/2] + array[size_array/2])/2.0;
}
int main()
{
int i,j;
int n; //Size of the array
int arr[n];
printf("Enter the size of the array\n");
scanf("%d", &n);
printf("Enter the elements of the array\n");
for( i=0; i<n; i++){
printf("Enter element %d\n", (i+1));
scanf("%d", &arr[i]);
}
printf("\nThe elements of the array are: \n");
for(i=0; i<n; i++){
printf(" %d\t", arr[i]);
}
//Sorting the element of the array using bubble sort
for(j = 0; j<n; j++)
{
int counter = 0;
i = 0;
while(i<n-1)
{
if (arr[i] > arr[i+1])
{
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
counter = 1;
}
i++;
}
if (!counter)
break;
}
printf("\nThe array after sorting the elemets:\t");
for( i=0; i<n; i++){
printf(" %d\t", arr[i]);
}
printf("\n The median of the array is %d\t",median(arr,n));
return 0;
}
Comments
Leave a comment