In: Computer Science
(C++ ONLY) You will define your own data type. It will be a struct called Fraction. The struct will have 2 integer fields: numerator and denominator.
You will write a function called reduce that takes a Fraction as a parameter and returns that Fraction in its reduced form. For example, if the fraction 2/4 is passed to the function, the fraction 1/2 will be returned. Consider any fraction with a denominator of 1 to be in reduced form.
You will write a main function. The main function will prompt the user to enter a numerator and denominator. The program will print the fraction as it was entered by the user, and it will also print the fraction in its reduced form.
Here is an example run of the program, where the user input is shown in blue:
Enter numerator: 5 Enter denominator: 10 Entered: 5/10 Reduced: 1/2 Please show it as 1 header file and 2 implementation files
here is the required solution
here is the code
#include <iostream>
using namespace std;
//structure defination
struct fraction
{
int numerator;
int denominator;
};
//function to reduce fraction
struct fraction reduce(fraction f)
{
for(int i=f.numerator;i>1;i--)
{
if(f.numerator%i==0 && f.denominator%i==0)
{
f.numerator=f.numerator/i;
f.denominator=f.denominator/i;
}
}
return f;}
//function to print fraction
void print(fraction f)
{
cout<<f.numerator<<"/"<<f.denominator<<"\n";
}
int main()
{ //declaration of variable
struct fraction f,freduced;
//user input
cout<<"Enter numerator:";
cin>>f.numerator;
cout<<"Enter denominator:";
cin>>f.denominator;
//call the reduce fraction
freduced=reduce(f);
//print the result
cout<<"Entered:";
print(f);
cout<<"Reduced:";
print(freduced);
return 0;
}