Write the following in C program: 2. Find the smallest and the 89th largest numb
ID: 3812190 • Letter: W
Question
Write the following in C program:
2. Find the smallest and the 89th largest numbers from the list below (there are 200 numbers): a[200] {1.78091,-2.83548, 1.01073, 3.53668, -2.06947, -2.1656, -2.49212, -1.17734, 1.58854, 1.04566, 2.53079, 3.55779, -1.2953, -1.52073, 0.700845, -1.54328, 1.51809, 4.4131, -1.41465, 1.19518, 0.524027, 1.326, -2.13747, 4.2306, 3.74311, 0.16148, 1.8518, 2.69222, 1.81258, -1.67292, 0.342221, -0.128743, -2.17595, 2.28141, 2.81313, 1.31347, 2.51934, -0.197853, -1 88771, -1.14326, -2.99874, 0.389048, 4.52694 2.66156, 1.47723, 4.06305, 2.66441, -2.56904, 2.73412, -0.0984338, -3.18722, -0.262954, -3.07846, -2.42551, 1.46869, 4.86579, -3.30251, 0.293073, 3.65555, -0.447677, -0.821855, -3.50907, 1.54326, -3.30442, -1.82311, 1.10188, 2.01632, -0.965983, 1.69965, 2.03883, 4.35191, -2.22695, 3.96553,-1.86273, 3.5223, 2.86601, 3.044, -3.43722, -1.92922, 3.00022, 2.34651, 1.2697, -0.584945, -0.552104, -0.831638, 0.778779, 2.87179, -1.24768, -3.00852, 4.6769, -3.14453, 4.7183, 0.291822, -1.36193, -2.49644, 3.11525, 1.32629, -3.4992, -1.03574, -3.75076, 3.28229, 4.93802, -3.10634, -1.75098, -3.06421,-0.33168, 2.4786, 3.80113, 2.76742, 3.88954, 4.60681, 1.04881, 1.77595, 4.21264, 3.75134, 1.33051, -2.51587, 1.57457, 2.24777, 3.21526, 1.15784, 1.07377, -0.71649, 2.96602, 2.87554, 1.13574, -1.61015 0.717, 1.92276, -2.53258, 0.911255, 1.91587, 4.17233, -1.42212,Explanation / Answer
#include<stdio.h>
#include<stdio.h>
//Swapping process
void swap(double *a, double *b)
{
double t;
t = *a;
*a = *b;
*b = t;
}//end of function
//Generates minimum heap
void minHeapify(double a[], int size, int i)
{
//calculates left
int l = 2*i;
//calculates right
int r = 2*i+1;
//Initialize smallest to i value
int smallest = i;
//Checks if left is less than size and array l position is less than array smallest position
if(l<size && a[l]<a[smallest])
//Update smallest with l value
smallest = l;
//Checks if right is less than size and array r position is less than array smallest position
if(r<size && a[r]<a[smallest])
//Update smallest with r value
smallest = r;
//Checks if smallest is not equals to i value
if(smallest!=i)
{
//swap
swap(&a[i],&a[smallest]);
//Recursively call function
minHeapify(a,size,smallest);
}//End of if
}//end of function
//Creates a min heap
void buildMinHeap(double a[], int size)
{
int i;
for(i=size/2;i>=0;i--)
minHeapify(a,size,i);
}
//Returns nth largest from the list
double nthLargest(double a[], int size, int k)
{
double minHeap[k];
int i;
for(i=0;i<k;i++)
minHeap[i] = a[i];
buildMinHeap(minHeap,k);
for(i=k;i<size;i++)
{
if(a[i]>minHeap[0])
{
minHeap[0]=a[i];
minHeapify(minHeap,k,0);
}
}
return minHeap[0];
}//End of function
//Returns the smallest number from the list
double smallNumber(double a[], int n)
{
//Initializes arrays zero position as smallest
double s = a[0];
int c;
//Loops till end
for(c = 1; c < n; c++)
//checks arrays current position is less than the earlier value of s
if(a[c] < s)
//Update the smallest number by array current position
s = a[c];
return s;
}//End of function
int main()
{
//Initializes the double array
double a[] = {1.78091, -2.83548, 1.10173, 3.53668, -2.06947, -2.1656, -2.49212,
-1.17734, 1.58854, 1.04566, 2.53079, 3.55779, -1.2953, -1.52073,
0.700845, -1.54328, 1.51809, 4.4131, -1.41465, 1.19518, 0.524027,
1.326, -2.13747, 4.2306, 3.74311, 0.16148, 1.8518, 2.69222, 1.81258,
-1.67292, 0.342221, -0.128743, -2.17595, 2.28141, 2.81313, 1.31347,
2.51934, -0.197853, -1.88771, -1.14326, -2.99874, 0.389048, 4.52694,
2.66156, -0.197853, -1.88771, -1.14326, -2.99874, 0.389048, 4.52694
-3.18722, -0.262954, -3.07846, -2.42551, 1.46869, 4.86579, -3.30442};
int size = sizeof(a)/sizeof(a[0]);
int nthPos;
printf(" Enter the nth position: ");
scanf("%d", &nthPos);
printf(" %dth largest number = %lf ", nthPos, nthLargest(a,size,nthPos));
printf(" Smallest Number = %lf", smallNumber(a, size));
return 0;
}
Sample run1:
Enter the nth position: 10
10th largest number = 2.661560
Smallest Number = -3.304420
Sample run2:
Enter the nth position: 22
22th largest number = 1.313470
Smallest Number = -3.304420
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.