Answer to Question #212113 in C for sai

Question #212113

Write a program to find the median of a user-defined array using bubble sort


1
Expert's answer
2021-06-30T05:05:30-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS