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

Write a C++ program that has this FRACTION struct struct FRACTION { int num; int

ID: 653102 • Letter: W

Question

Write a C++ program that has this FRACTION struct

struct FRACTION
{
   int num;
   int den;
};


This struct will keep track of numerator and denominator of a fraction.

Declare prototype for a function called getFraction() that has this prototype

FRACTION getFraction();

In your main function, declare three variables of type FRACTION using this statement.

FRACTION f1,f2,f3;


Then read data for each fraction using a function called getFraction() hence it can be read like this. So you must write definition for getFraction()

f1 = getFraction();
f2 = getFraction();


It also mean that you have to write the complete definition of the function getFraction(). Refer to the powerpoint notes for extra help on this.

After reading values for f1 and f2 above, display their two values.

Explanation / Answer

WITHOUT REDUCING THE FRACTIONS

#include <iostream>
using namespace std;
struct FRACTION
{
int num;
int den;
};


FRACTION getFraction(void);
void putFraction(FRACTION);


int main()
{

FRACTION f1,f2,f3;


cout<<"enter values of fraction f1 ";
f1 = getFraction();


cout<<"enter values of fraction f2 ";
f2 = getFraction();


cout<<"fraction f1 = ";
putFraction(f1);


cout<<"fraction f2 = ";
putFraction(f2);

}

FRACTION getFraction()
{
FRACTION temp;


cout<<"enter numerator ";
cin>>temp.num;


cout<<"enter denominator ";
cin>>temp.den;


return temp;
}


void putFraction(FRACTION f)
{
cout<<f.num<<"/"<<f.den<<endl;   
}

OUTPUT:

WITH REDUCING THE FRACTION

#include <iostream>
using namespace std;


struct FRACTION
{
int num;
int den;
};


FRACTION getFraction(void);
void putFraction(FRACTION);
int gcd(int m, int n);


int main()
{
FRACTION f1,f2,f3;


cout<<"enter values of fraction f1 ";
f1 = getFraction();


cout<<"enter values of fraction f2 ";
f2 = getFraction();


cout<<"fraction f1 = ";
putFraction(f1);


cout<<"fraction f2 = ";
putFraction(f2);

}

FRACTION getFraction()
{
FRACTION temp;
int i;


cout<<"enter numerator ";
cin>>temp.num;


cout<<"enter denominator ";
cin>>temp.den;

//LOGIC TO REDUCE THE FRACTION
i=gcd(temp.num,temp.den);
temp.num = temp.num/i;
temp.den = temp.den/i;


return temp;
}


void putFraction(FRACTION f)
{
cout<<f.num<<"/"<<f.den<<endl;   
}


int gcd(int m, int n)
{
int r;

/* Check For Proper Input */
if((m == 0) || (n == 0))
return 0;
else if((m < 0) || (n < 0))
return -1;

do
{
r = m % n;
if(r == 0)
break;
m = n;
n = r;
}
while(true);

return n;
}

OUTPUT:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote