Kth Largest number

 

#include <stdio.h>


int main()
{
    int size,i,j,k,temp;


    printf("\nEnter size of array: ");
    scanf("%d",&size);


    //creating an array of size specified by user
    int arr[size]; 
    printf("\nEnter array values: ");
    for(i=0;i<size;i++){
        scanf("%d",&arr[i]);    
    }


    printf("\nEnter k value: ");
    scanf("%d",&k);


    //Sorting in increasing order using bubble sort
    for(i=0;i<size;i++){
        for(j=0;j<size;j++){
            if(arr[i]<arr[j]){
                //swap arr[i] and arr[j]
                temp=arr[i];
                arr[i]=arr[j];
                arr[j]=temp;
            }
        }
    }


    printf("\nSorted array is: ");
    //display output
    for(i=0;i<size;i++){
        printf("%d\t",arr[i]);
    }


    //display the Kth largest value
    printf("\n\n%d'th largest value is %d",k,arr[k-1]); //k-1 because array index starts from 0
    return 0;
}

Comments

Popular posts from this blog

C program to copy the contents of one file to another

Division of two complex numbers

Student marks by using structures