a) Implement the following three external functions: int fillArray(rational arr[
ID: 3574097 • Letter: A
Question
a) Implement the following three external functions:
int fillArray(rational arr[], int size);
// The user decides n, the number of rational numbers he wants to enter into the array. The function then prompts the user to
// enter n rational numbers, one at a time and store them in the array
// Precondition: n <= size.
// Postcondition: the array is loaded with n rational numbers entered by the user and n is returned by the function.
void displayArray(rational arr[], int n);
// Postcondition: n rational numbers stored in the array are displayed
void sort(rational r[], int n)
// Postcondition: the the array has been sorted in descending order
b) Write a main() function that declares a rational array of size 10. It then test the three external functions to produce an output similar to the below sample display.
CAWindowslsystem32cmd.exe xxx Demonstrate sorting a rational array xxx You may enter up to 10 rational numbers How many? 5 r [01: Enter values for numerator followed by denominator 6 7 r[1]: Enter values for numerator followed by denominator 1 4 Enter values for numerator followed by denominator 5 8 r 21 r[3]: Enter values for numerator followed by denominator -1 2 r [4]: Enter values for numerator followed by denominator 34 Before sorting, array contains 1 /4 5/8 1/2 3/4 After sorting, array contains 3/4 5/8 1 /4 1/2 Press any key to continueExplanation / Answer
#include <stdio.h>
#include <stdlib.h>
typedef struct rational
{
int nume;
int deno;
}rational;
void swap(rational *a,int pos1,int pos2)
{
int tempNume=a[pos1].nume;
a[pos1].nume=a[pos2].nume;
a[pos2].nume=tempNume;
int tempDeno=a[pos1].deno;
a[pos1].deno=a[pos2].deno;
a[pos2].deno=tempDeno;
}
double fractionValue(rational *a,int index)
{
return (((double) a[index].nume)/a[index].deno);
}
int partition(rational *a,int p,int r)
{
int i;
double temp=fractionValue(a,r);
int left=p-1,right=p;
for( ;right<r;right++)
{
if(fractionValue(a,right)<=temp)
{
left++;
swap(a,left,right);
}
}
left++;
swap(a,left,right);
return left;
}
void quicksort(rational *a,int p,int r)
{
if(p<r)
{
int pivot=partition(a,p,r);
quicksort(a,p,pivot-1);
quicksort(a,pivot+1,r);
}
int i;
}
int fillArray(rational arr[], int size)
{
int i;
for(i=0;i<size;i++)
{
printf("r[%d]: Enter values for numerator followed by denominator:",i);
scanf("%d%d",&arr[i].nume,&arr[i].deno);
}
return size;
}
void displayArray(rational arr[], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d/%d ",arr[i].nume,arr[i].deno);
}
printf(" ");
}
void sort(rational r[], int n)
{
quicksort(r,0,n-1);
}
int main()
{
rational arr[10];
int size;
printf("***Demonstarte sorting of a rational array*** ");
printf("you may up to enter 10 ratinal numbers. How many ? ");
scanf("%d",&size);
if(size>10)
{
printf("sorry you want to enter rational number greater then size of array ");
exit(0);
}
int n=fillArray(arr,size);
printf("Before sorting, array contains: ");
displayArray(arr,n);
sort(arr,n);
printf("After sorting, array contains: ");
displayArray(arr,n);
return 0;
}
/*
output:-
***Demonstarte sorting of a rational array***
you may up to enter 10 ratinal numbers. How many ? 5
r[0]: Enter values for numerator followed by denominator:6 7
r[1]: Enter values for numerator followed by denominator:1 4
r[2]: Enter values for numerator followed by denominator:5 8
r[3]: Enter values for numerator followed by denominator:-1 2
r[4]: Enter values for numerator followed by denominator:3 4
Before sorting, array contains:
6/7 1/4 5/8 -1/2 3/4
After sorting, array contains:
-1/2 1/4 5/8 3/4 6/7
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.