Using concept of DMA, find the third largest element of an array of size n - C

Q. Write a program using concept of DMA to find the third largest element of an array of size N.
 #BIT-FirstSem-2018-3b-8Marks-Solution
==> Code goes like this:

#include<stdio.h>
#include<stdlib.h>
void get(float *, int);
void sort(float *, int);
int main()
{
int n;
float *num; //pointer variable of float data type
printf("Enter the size of array:");
scanf("%d",&n);
num=(float *)malloc(n*sizeof(float));
get(num,n);
sort(num,n);
return 0;
}
void get(float *nums, int n)
{
int i;
printf("Enter %d numbers:",n);
for ( i = 0; i < n; i++)
{
    scanf("%f",nums+i);
}
}
void sort(float *nums, int n)
{
float temp;
int i,j;
for ( i = 0; i < n-1; i++)
{
    for ( j = i+1; j < n; j++)
    {
        if(*(nums+i) < *(nums+j))
        {
            temp=*(nums+i);
            *(nums+i)=*(nums+j);
            *(nums+j)=temp;
        }
    }
}
printf("The third largest number is %.2f",*(nums+2));
}

Output:










If you are using Turbo C++ IDE, then use conio.h in link section after stdio.h
and use getch(); to hold the output screen, just before the end of main function.
If you have any confusion regarding the program, please, comment below.
Happy Coding !
Thank you 




Share on Google Plus

About Nepali Xoro

Interested in Information Technology, Spreading Knowledge for Nepalease IT Students."Loves Traveling", Listen Music and Surfing web.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment