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.
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 . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.