Question

In: Computer Science

in simple C++: 2. Create a structure Fraction which contains a numerator and a denominator then...

in simple C++:

2. Create a structure Fraction which contains a numerator and a denominator then do the following:
a. Write a function void printFraction(Fraction f) which prints out a fraction in the following format; e.g. if the numerator is 2 and the denominator is 5, it will print out 2/5
b. Write a function Fraction mult(Fraction f1, Fraction f2) which returns a new fraction which is the product of f1 and f2.
c. Write a function Fraction add(Fraction f1, Fraction f2) which returns a new fraction which is the sum of f1 and f2. You may simply multiply each fraction by the other’s denominator to find a common denominator; e.g. (3/4)+(5/6) = (3*6/4*6)+(5*4/6*4) = 38/24. You do NOT have to reduce the fractions.

Solutions

Expert Solution

C++ Code :

#include <bits/stdc++.h>
using namespace std;
struct Fraction// name of the structure storing numerator and denominator
{
int n;//numerator
int d;//denominator
};
void printFraction(Fraction f) // this function is to print the fraction in " n / d " form
{
cout<< (f.n) << "/" << (f.d) << "\n"; // "." is used to make a reference to the variable n and d
}
Fraction mult(Fraction f1, Fraction f2)//this function is to multiply the two fractions
{
Fraction f3;//multiplication is n*n / d*d
f3.n = f1.n * f2.n;
f3.d = f1.d * f2.d;
return f3;
}
Fraction add(Fraction f1, Fraction f2)//this function is to add the two fractions
{
Fraction f3;
f3.n = (f1.n * f2.d ) + (f2.n * f1.d);//this is by easy lcm method
f3.d = f1.d * f2.d;
return f3;
}
int main()
{
int a,b;
cout<<"Enter the numerator of a fraction : ";
cin>>a;
cout<<"Enter the denominator of a fraction : ";
cin>>b;
Fraction f;
f.n=a;
f.d=b;
cout<<"The value of fraction is : ";
printFraction(f);
  
Fraction f1;
Fraction f2;

cout<<"Enter the numerator of a fraction 1: ";
cin>>a;
cout<<"Enter the denominator of a fraction 1: ";
cin>>b;

f1.n = a;
f1.d = b;

cout<<"Enter the numerator of a fraction 2: ";
cin>>a;
cout<<"Enter the denominator of a fraction 2: ";
cin>>b;

f2.n = a;
f2.d = b;;
f=mult(f1,f2);//multiply two fraction and store in f
cout<<"The value of fraction after muliplication is : ";
printFraction(f);
  
f=add(f1,f2);//add two fraction and store in f
cout<<"The value of fraction after addition is : ";
printFraction(f);
return 0;
}

The output is also shown here :

If you are satisfied by the explanation given through the comments in the code Then Please Upvote my answer.


Related Solutions

The denominator of a fraction is 4 more than the numerator. If both the numerator and...
The denominator of a fraction is 4 more than the numerator. If both the numerator and the denominator of the fraction are increased by 3, the new fraction is 5/6 . Find the original fraction.
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member...
First lab: Create a Fraction class Create member variables to store numerator denominator no additional member variable are allowed Create accessor and mutator functions to set/return numerator denominator Create a function to set a fraction Create a function to return a fraction as a string ( common name ToString(), toString())  in the following format: 2 3/4 use to_string() function from string class to convert a number to a string; example return to_string(35)+ to_string (75) ; returns 3575 as a string Create...
7. Fractions You can express a fraction as a list: [numerator, denominator]. For example 1 2...
7. Fractions You can express a fraction as a list: [numerator, denominator]. For example 1 2 can be expressed as the list [1,2]. (a) Write a function called factionAdd() that takes two fractions as lists and adds them. For example, fraction([1,2], [3,4]) returns [5,4] (b) Write a function fractionMult() that multiplies two fractions that are passed as lists. [HINT: You may use the following function gcd ( x ; y ) to help you calculate the Greatest Common Divisor, which...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int type, private and the following methods: one default constructor which will create a fraction of 1/1. one constructor that takes two parameters which will set the values of numerator and denominator to the specified parameters. int getNum() : retrieves the value of numerator int getDenom(): retrieves the value of the denominator Fraction add(Fraction frac): adds with another Fraction number and returns the result in...
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public...
How do I fix my code? public class Fraction {    private int numerator, denominator, numberOfFraction;    public Fraction () {    numerator = 0;    denominator = 1;    numberOfFraction++; }    public Fraction (int n, int d) {    numerator = n;    denominator = d;    numberOfFraction++; } private int gcd (int num1, int num2) {    if (num1 == 0)    return num2;    return gcd (num2 % num1, num1); }    public Fraction add (Fraction third) {    int n = numerator * third.denominator + third.numerator * denominator;    int...
Create a nested structure in C (one structure that contains another) and print out all the...
Create a nested structure in C (one structure that contains another) and print out all the fields in both structures. The main structure should have fields for: movie name and the year it was released. The extended structure should include the original structure as well as fields for: Lead actor, genre and runtime.
When you convert feet to inches, how do you decide which part of the conversion factor should be in the numerator and which in the denominator?
When you convert feet to inches, how do you decide which part of the conversion factor should be in the numerator and which in the denominator?
Write a C program that contains a structure that uses predefined types and union. • Create...
Write a C program that contains a structure that uses predefined types and union. • Create a struct with name, age, kind (Either child, college student, or adult), and kindOfPerson (Either kid, student, or adult) • kids have a school field. Students have college and gpa. Adults have company and salary. • Create one non-dynamic struct with the content for a college student: "Bob", 20, K-State, 3.5 • Create one struct dynamically for a kid with: "Alison", 10, "Amanda Arnold...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not...
Create a c++ class called Fraction in which the objects will represent fractions. Remember:   do not reduce fractions, do not use "const," do not provide any constructors, do not use three separate files. You will not receive credit for the assignment if you do any of these things. In your single file, the class declaration will come first, followed by the definitions of the class member functions, followed by the client program. It is required that you provide these member...
f(x)= 9x^4-2x^3-36x^2+8x/3x^3+x^2-14 -Factor the numerator and denominator of f(x) completely. -Write the domain of f(x) in...
f(x)= 9x^4-2x^3-36x^2+8x/3x^3+x^2-14 -Factor the numerator and denominator of f(x) completely. -Write the domain of f(x) in interval notation. -Locate all hole(s), if any, and write them in the form of coordinate pairs. -Locate all vertical asymptote(s), if any, and give their equations in the form x = c. For each one, describe what happens to f(x) as x approaches c from the left(-), and as x approaches c from the right (+). -Locate the horizontal/slant asymptote, if any, and give...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT