Write code in C++ Add the following prototype functions to source code and make
ID: 3777097 • Letter: W
Question
Write code in C++
Add the following prototype functions to source code and make it work/run in Visual Studio : addFraction() , subFraction() , divFraction(). I already have multiply working. In other words, along with multiplying the two fractions, also make the two fractions add, subtract, and divide. Source code is below.
// ConsoleApplication30.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
struct FRACTION
{
int num;
int den;
};
//multiply
FRACTION multiply(FRACTION f1, FRACTION f2)
{
FRACTION f3;
// multiply the numerator
f3.num = ((f1.num)*(f2.num));
// multiply the denominator
f3.den = ((f1.den) * (f2.den));
return f3;
}
//read fraction
FRACTION getFraction()
{
FRACTION f;
cout << "Enter numerator :";
cin >> f.num;
cout << "Enter denominator :";
cin >> f.den;
return f;
}
int main(int argc, char *argv[])
{
int sum, difference, product, quotient;
FRACTION f1, f2, f3;
f1 = getFraction();
f2 = getFraction();
//multiply fraction
f3 = multiply(f1, f2);
//print
cout << "Fraction " << f1.num << "/" << f1.den << " * " << f2.num << "/" << f2.den << " = " << f3.num << "/" << f3.den << endl;
return 0;
}
Explanation / Answer
Here is the update for you:
// ConsoleApplication30.cpp : Defines the entry point for the console application.
//#include "stdafx.h"
#include <iostream>
using namespace std;
struct FRACTION
{
int num;
int den;
};
//addition
FRACTION addFraction(FRACTION f1, FRACTION f2)
{
FRACTION f3;
// update the numerator
f3.num = ((f1.num)*(f2.den) + (f1.den)*(f2.num));
// update the denominator
f3.den = ((f1.den) * (f2.den));
return f3;
}
//subtraction
FRACTION subFraction(FRACTION f1, FRACTION f2)
{
FRACTION f3;
// update the numerator
f3.num = ((f1.num)*(f2.den) - (f1.den)*(f2.num));
// update the denominator
f3.den = ((f1.den) * (f2.den));
return f3;
}
//divide
FRACTION divFraction(FRACTION f1, FRACTION f2)
{
FRACTION f3;
// update the numerator
f3.num = ((f1.num)*(f2.den));
// update the denominator
f3.den = ((f1.den) * (f2.num));
return f3;
}
//multiply
FRACTION multiply(FRACTION f1, FRACTION f2)
{
FRACTION f3;
// multiply the numerator
f3.num = ((f1.num)*(f2.num));
// multiply the denominator
f3.den = ((f1.den) * (f2.den));
return f3;
}
//read fraction
FRACTION getFraction()
{
FRACTION f;
cout << "Enter numerator :";
cin >> f.num;
cout << "Enter denominator :";
cin >> f.den;
return f;
}
int main(int argc, char *argv[])
{
int sum, difference, product, quotient;
FRACTION f1, f2, f3;
f1 = getFraction();
f2 = getFraction();
//multiply fraction
f3 = multiply(f1, f2);
//print
cout << "Fraction " << f1.num << "/" << f1.den << " * " << f2.num << "/" << f2.den << " = " << f3.num << "/" << f3.den << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.