Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Rational numbers are numbers that can be expressed by dividing one integer with

ID: 3840121 • Letter: R

Question

Rational numbers are numbers that can be expressed by dividing one integer with another nonzero integer. Therefore, rational numbers can be written as follow:
a÷b

where a is the numerator and b is the denominator.
You need to write a program that stores a fraction as a structure having the following elements:
i. Numerator
ii. Denominator

a. Write two functions respectively to:

i. display a fraction object, named print(). The function should accept a fraction object as input and returns nothing. Each fraction should be displayed in fractional, as well as in decimal notation. For example:

Fractions:      2/3 ---- 0.666667
                      10/14 ---- 0.714286
[6 marks]
ii. determine the smallest fraction, named lowestfrac(). The function should accept an array of fraction objects as input and returns nothing. The function also prints the largest fraction in fractional and decimal form to the screen. [8 marks]

b. In main(), create an array of 5 fractions and ask the user to input each fraction. The program should then display the largest and smallest fractions. [6 marks]
c. Save the whole program as numerdenom.cpp in the same folder.


50 miaaaLm] (Tutal sane anabaaltat :n be exprezed ts Gainer en: slass with mart elinic, tatenal mabas c41 b: ulilk 28rkrW" : imarTTTI tur 50resfractiou aa shusirr hrmg the toDowD e mprt and retrn: Donia Each fou inn stola be aspiancin fra ne rh;"snullod firit, armed 1uwurtFrnei 1 The tunitist offractic objects ac unur ad tenans noting, T2'anon al tractOZ LC tanenal and inmal fom to the sinn.

Explanation / Answer

#include<iostream>
using namespace std;
class Fraction
{
   public :
       int x;
       int y;

};


void print(Fraction obj)
{
  
   cout<<obj.x<<"/"<<obj.y<<"------"<<((double)obj.x)/obj.y<<endl;
  
}

void lowestFrac(Fraction arr[],int n)
{
   double d[n];
   int i=0;
   while(i<n)
   {
       d[i]=((double)arr[i].x)/arr[i].y;//calculating fractions//  
       i++;
   }
   //finding lowest fraction
  
   i=0;
   int low=0;
   while(i<n)
   {
       if(d[i]<d[low])
       {
           low=i;  
       }  
       i++;
   }
  
   //printing lowest fraction
   cout<<"The lowest Fraction is:";
   print(arr[low]);
  
}


void largestFrac(Fraction arr[],int n)
{
   double d[n];
   int i=0;
   while(i<n)
   {
       d[i]=((double)arr[i].x)/arr[i].y;//calculating fractions//  
       i++;
   }
   //finding largest fraction
  
   i=0;
   int large=0;
   while(i<n)
   {
       if(d[i]>d[large])
       {
           large=i;  
       }  
       i++;
   }
  
   //printing largest fraction
       cout<<"The largest Fraction is:";
   print(arr[large]);
  
}

int main()
{
  
   cout<<"Enter the size of fractions array:";
   int n,i;
   cin>>n;
  
   Fraction arr[4];
  
   for(i=1;i<=n;i++)
   {
       cout<<"Enter "<<i<<" Fraction: ";
       cout<<"Numerator:";
       cin>>arr[i-1].x;
       cout<<"Denominator:";
       cin>>arr[i-1].y;  
   }
  
  
   //printing all fractions:
       cout<<"Fractions: ";
   for(i=0;i<n;i++)
       print(arr[i]);
  
   //printing lowest fraction
   lowestFrac(arr,n);//function calling
   //printing largest fraction
   largestFrac(arr,n);
  
   return 0;
}

output:-

Enter the size of fractions array:5
Enter 1 Fraction:
Numerator:2
Denominator:3
Enter 2 Fraction:
Numerator:2
Denominator:4
Enter 3 Fraction:
Numerator:2
Denominator:5
Enter 4 Fraction:
Numerator:3
Denominator:8
Enter 5 Fraction:
Numerator:1
Denominator:9
Fractions:
2/3------0.666667
2/4------0.5
2/5------0.4
3/8------0.375
1/9------0.111111
The lowest Fraction is:1/9------0.111111
The largest Fraction is:2/3------0.666667


Process exited normally.
Press any key to continue . . .