Question

Programming Concepts cin/cout variables/types Functions Reference structs Task Summary: For this first Quest you I have...

Programming Concepts

  • cin/cout

  • variables/types

  • Functions

  • Reference

  • structs

Task Summary:

For this first Quest you I have set up a program in Visual Studio that will use operation between fractions. I have written a basic code example that does all the resources that you need to follow. Your job is to complete the exercises presented below in quest2.cpp.

For this assignment you will learn and demonstrate how to:

  1. Work with functions

  2. use Past-by-Value and Past-by-Reference.

  3. use structs.

  4. use cin to get user input from the keyboard

  5. use Header/Object Files.

Base Code Functionality

The instructor has provided all the necessary

  • base main source code (quest2.cpp),

Required Code Functionality

This program will allow basic operations between fractions, for example, addition, subtraction, multiplication, division and other basic functions that can be performed in fractions.To achieve a seamless integration of the struct fraction into the C++ type system its operations need to be overloaded and you are required to provide such implementation.

Your task is to implement code that:  

  1. Create a fraction [50 points] // (implemented)

  2. Implement the Add two fractions [50 points] //(implemented)

  3. Implement the Add fraction and integer [50 points] //(overload function)

  4. Implement the negation operator for fractions. [50 points]

  5. Implement support for division operations for the new fraction type [100 points]

  6. Implement Multiply fractions [50 points]

  7. Implement Subtract fraction [100 points]

  8. Implement rational::reduce, which will be used to reduce fractions. [100 points]

Follow guidelines in your code. [50 points]

Finally, separate the struct definition with method declarations and function declarations (in a header file), the function and method definitions (in a source file) and the main function in the quest2.cpp file. [100 points].

Note: Use test cases to ensure they actually do work. Create a Test for every function that you implemented.

Task 0: Create project

You will create a project in visual studio that contain the following files:

  • quest2.cpp,

  • rationals.cpp,

  • rationals.h

The quest2.cpp file contains the int main function. The rationals.cpp and rational.h files contain the functions required in the program. The rationals.h file contain the structs and constants you will be using for your program.

Task 1: Test that your code runs

After you create your program, add the next base code to your quest2.cpp

#include <iostream>

using namespace std;

struct Fraction {

int numerator_;

int denominator_;

};

Fraction newFraction() {

Fraction f;

f.numerator_ = 1;

f.denominator_ = 2;

return f;

}

Fraction newFraction(int num, int den) {

Fraction f;

f.numerator_ = num;

f.denominator_ = den;

return f;

}

//Examples input and output  

//Input -> (1\2)+(1/2)

//Output -> (4/4)

void addToFraction(Fraction& f1, const Fraction& f2) {

int denominador = f1.denominator_*f2.denominator_;

Fraction tmp_lhs = newFraction(f1.numerator_*f2.denominator_, denominador);

Fraction tmp_rhs = newFraction(f2.numerator_*f1.denominator_, denominador);

f1.numerator_ = tmp_lhs.numerator_ + tmp_rhs.numerator_;

f1.denominator_ = denominador;

}

//Examples input and output  

//Input -> (1\2)+2

//Output -> (5/2)

void addToFraction(Fraction& f1, int number) {

   

}

void printFraction(Fraction& f) {

cout<< '(' << f.numerator_ << '/' << f.denominator_ << ')' << endl;

}

int main() {

//Creating fraction a and b

Fraction a = newFraction(3,2);

Fraction b = newFraction(4,2);

//Printing fraction a and b

printFraction(a);

printFraction(b);

//Adding two fractions

addToFraction(a,b);

printFraction(a);

//Adding fraction to number

addToFraction(a, 12);

cin.get();

return 0;

}

Then, when you compile and run the program, a test example of add operation is provided, make sure that is working properly.

Task 2: Complete the program

  1. Create a function for each operation between fraction required.

  2. In the main function, you need to create test for every operation that is required.

Task 3: Record your results

Now you want to make a video where you present how each function works. Run each test separately. Every operation that is not presented in the video will not receive points.

Task 4: Submit Completed Assignment

Create a zip file that include your visual studio project and a 5 minute video explaining your results in Blackboard!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

rationals.h


struct Fraction {
int numerator_;
int denominator_;
};

Fraction newFraction();
Fraction newFraction(int num, int den) ;
void addToFraction(Fraction& f1, int number);
void addToFraction(Fraction& f1, const Fraction& f2) ;
void printFraction(Fraction& f) ;
void negationFraction(Fraction& f);///f=-f
void operator-(Fraction &f); /// f1=-f1
void multiplyFraction(Fraction& f1,const Fraction& f2); /// f1=f1*f2
void divideFraction(Fraction& f1,const Fraction& f2); /// f1=f1/f2
void substractFraction(Fraction& f1, const Fraction& f2); ///f1=f1-f2
void rationalReduce(Fraction &f); /// reduce f
-----------------------------------------------------------------------------------------------------------

rationals.cpp

/// the function and method definitions (in a source file = rationals.cpp)
#include <iostream>
#include "rationals.h"
using namespace std;

Fraction newFraction() {

Fraction f;

f.numerator_ = 1;

f.denominator_ = 2;

return f;

}

Fraction newFraction(int num, int den) {

Fraction f;

f.numerator_ = num;

f.denominator_ = den;

return f;

}

//Examples input and output

//Input -> (1\2)+(1/2)

//Output -> (4/4)

void addToFraction(Fraction& f1, const Fraction& f2) {

int denominador = f1.denominator_*f2.denominator_;

Fraction tmp_lhs = newFraction(f1.numerator_*f2.denominator_, denominador);

Fraction tmp_rhs = newFraction(f2.numerator_*f1.denominator_, denominador);

f1.numerator_ = tmp_lhs.numerator_ + tmp_rhs.numerator_;

f1.denominator_ = denominador;

}

//Examples input and output

//Input -> (1\2)+2

//Output -> (5/2)

void addToFraction(Fraction& f1, int number) {
/// multiply number by f1's denominator &
/// divide number by f1's denominator

Fraction temp=newFraction(number * f1.denominator_ , f1.denominator_);
// printFraction(temp);
///now add numerator
f1.numerator_=f1.numerator_ + temp.numerator_;
/// f1 denominator will remain same

}

///Implement the negation operator for fractions.
///input : 4/3
/// output -4/3
///input : 5/4
///output :-5/4
void negationFraction(Fraction &f){
f.numerator_= - (f.numerator_);
}
///Implement the negation operator for fractions. by OVERLOADING - (minus) operator
void operator-(Fraction &f){
f.numerator_= - (f.numerator_);
}

///In multiply of two fractions, simply multiply the two numerators && multiply the two denominators
///input : (4/3) * (5/2)
///output:(20/6)
void multiplyFraction(Fraction &f1,const Fraction &f2){
f1.numerator_ = f1.numerator_ * f2.numerator_;
f1.denominator_=f1.denominator_ * f2.denominator_;
}
///In division of two fractions, the first fraction must
/// be multiplied by the reciprocal of the second fraction.
void divideFraction(Fraction &f1,const Fraction &f2){
f1.numerator_ = f1.numerator_ * f2.denominator_;
f1.denominator_=f1.denominator_ * f2.numerator_;
}
///input = (2/3)-(1/2)
///output =1/6
void substractFraction(Fraction &f1,const Fraction &f2){
int denominador = f1.denominator_*f2.denominator_;

Fraction tmp_lhs = newFraction(f1.numerator_*f2.denominator_, denominador);

Fraction tmp_rhs = newFraction(f2.numerator_*f1.denominator_, denominador);

f1.numerator_ = tmp_lhs.numerator_ - tmp_rhs.numerator_;

f1.denominator_ = denominador;
}
///function to find GCD common divisor
int GCD(int num1, int num2)
{
if (num1 == 0)
return num2;
return GCD(num2 % num1, num1);
}

///input : 20/5 GCD(20,5)=5 output=4/1
///input : 22/5 GCD (22, 5) =
void rationalReduce(Fraction &f){
///If both numerator and denominator of fraction
/// is divisible by their greatest common divisor then fraction can be reduce
///hence we divide numerator and denominator by gcd

int gcd=GCD(f.numerator_, f.denominator_);
f.numerator_ =f.numerator_/gcd;
f.denominator_ = f.denominator_/gcd;

}

void printFraction(Fraction& f) {
cout<< '(' << f.numerator_ << '/' << f.denominator_ << ')'<<endl;
}

//--------------------------------------------------------------------------------------------------------------------

/// quest2.cpp

#include <iostream>
#include "rationals.h"
using namespace std;

int main() {
/*
//Creating fraction a and b

Fraction a = newFraction(3,2);

Fraction b = newFraction(4,2);

//Printing fraction a and b

printFraction(a);

printFraction(b);

//Adding two fractions

addToFraction(a,b);

printFraction(a);

//Adding fraction to number

int num=15;
addToFraction(a, num);
cin.get();
*/

int choice=0;
do{


cout<<"\nEnter choice :";
cout<<"\n1.Add Two Fractions\n2.Add Fraction and Number\n3. Subtract fraction";
cout<<"\n4.Multiply Fractions\n5.Division of Fractions\n6.Negation of Fraction";
cout<<"\n7.Reduce Fraction to simple form\n0.exit \t";
cin>>choice;


switch(choice){

case 1:
{
int num,den;
cout<<"\nEnter first fraction";
cout<<"\nNumerator = ";
cin>>num;
cout<<"\nDenominator = ";
cin>>den;
Fraction f1=newFraction(num,den);

cout<<"\nEnter second fraction";
cout<<"\nNumerator = ";
cin>>num;
cout<<"\nDenominator = ";
cin>>den;
Fraction f2=newFraction(num,den);

addToFraction(f1,f2);
cout<<"\nAnswer = ";
printFraction(f1);
cout<<endl;

break;
}

case 2:
{
int num,den;
cout<<"\nEnter first fraction";
cout<<"\nNumerator = ";
cin>>num;
cout<<"\nDenominator = ";
cin>>den;
Fraction f1=newFraction(num,den);
cout<<"\nEnter second fraction";
cin>>num;
addToFraction(f1,num);
printFraction(f1);
break;
}

case 3:
{
int num,den;
cout<<"\nEnter first fraction";
cout<<"\nNumerator = ";
cin>>num;
cout<<"\nDenominator = ";
cin>>den;
Fraction f1=newFraction(num,den);

cout<<"\nEnter second fraction";
cout<<"\nNumerator = ";
cin>>num;
cout<<"\nDenominator = ";
cin>>den;
Fraction f2=newFraction(num,den);

substractFraction(f1,f2);
cout<<"\nSubtract = ";
printFraction(f1);
cout<<endl;

break;
}

case 4:
{
int num,den;
cout<<"\nEnter first fraction";
cout<<"\nNumerator = ";
cin>>num;
cout<<"\nDenominator = ";
cin>>den;
Fraction f1=newFraction(num,den);

cout<<"\nEnter second fraction";
cout<<"\nNumerator = ";
cin>>num;
cout<<"\nDenominator = ";
cin>>den;
Fraction f2=newFraction(num,den);

multiplyFraction(f1,f2);
cout<<"\nMultiply = ";
printFraction(f1);
cout<<endl;

break;
}

case 5:
{
int num,den;
cout<<"\nEnter first fraction";
cout<<"\nNumerator = ";
cin>>num;
cout<<"\nDenominator = ";
cin>>den;
Fraction f1=newFraction(num,den);

cout<<"\nEnter second fraction";
cout<<"\nNumerator = ";
cin>>num;
cout<<"\nDenominator = ";
cin>>den;
Fraction f2=newFraction(num,den);

divideFraction(f1,f2);
cout<<"\nDivide = ";
printFraction(f1);
cout<<endl;

break;
}

case 6:
{
int num,den;
cout<<"\nEnter first fraction";
cout<<"\nNumerator = ";
cin>>num;
cout<<"\nDenominator = ";
cin>>den;
Fraction f1=newFraction(num,den);
-f1;
cout<<"\nAfter negation = ";
printFraction(f1);
negationFraction(f1);
cout<<"\nAgain another After negation = ";
printFraction(f1);

cout<<endl;

break;
}

case 7:
{
int num,den;
cout<<"\nEnter first fraction";
cout<<"\nNumerator = ";
cin>>num;
cout<<"\nDenominator = ";
cin>>den;
Fraction f1=newFraction(num,den);
cout<<"\nAfter reduce \n";
rationalReduce(f1);
printFraction(f1);
cout<<endl;


break;
}

}

}while(choice!=0);

return 0;

}

/*

output


*/

Add a comment
Know the answer?
Add Answer to:
Programming Concepts cin/cout variables/types Functions Reference structs Task Summary: For this first Quest you I have...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing...

    Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing an array of structs to a function, and returning a struct as the value of a function. A function in the program should read several rows of data from a text file. (The data file should accompany this assignment.) Each row of the file contains a month name, a high temperature, and a low temperature. The main program should call another function which...

  • C++ For this assignment you will be building on the Original Fraction class you began last...

    C++ For this assignment you will be building on the Original Fraction class you began last week. You'll be making four major changes to the class. [15 points] Delete your set() function. Add two constructors, a default constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Since Fractions cannot have...

  • This is a C++ probelm, I am really struggling with that...... Can anyone help me?? Write...

    This is a C++ probelm, I am really struggling with that...... Can anyone help me?? Write a fraction class whose objects will represent fractions. For this assignment you aren't required to reduce your fractions. You should provide the following member functions: A set() operation that takes two integer arguments, a numerator and a denominator, and sets the calling object accordingly. Arithmetic operations that add, subtract, multiply, and divide fractions. These should be implemented as value returning functions that return a...

  • C Programming For this task, you will have to write a program that will prompt the...

    C Programming For this task, you will have to write a program that will prompt the user for a number N. Then, you will have to prompt the user for N numbers, and print then print the sum of the numbers. For this task, you have to create and use a function with the following signature: int sum(int* arr, int n); Your code should look something like this (you can start with this as a template: int sum(int* arr, int...

  • java only no c++ Write a Fraction class whose objects will represent fractions. You should provide...

    java only no c++ Write a Fraction class whose objects will represent fractions. You should provide the following class methods: Two constructors, a parameter-less constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Arithmetic operations that add, subtract, multiply, and divide Fractions. These should be implemented as value returning methods...

  • C++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

  • Create a header file timer.h containing the following functions (you can have more functions, but the...

    Create a header file timer.h containing the following functions (you can have more functions, but the below ones are required. Do not modify the given function signatures. // Initialize the timer with the user-provided input void initTimer(ClockType *clock, int minutes, int seconds); // Run the timer -- print out the time each second void runTimer(); // Clean up memory (as needed) void cleanTimer(ClockType *clock); Create a file timer.c and implement (at least) these three functions and any other functions you...

  • Main topics: Files Program Specification: For this assignment, you need only write a single-file ...

    C program Main topics: Files Program Specification: For this assignment, you need only write a single-file C program. Your program will: Define the following C structure sample typedef struct int sarray size; the number of elements in this sanple's array floatsarray the sample's array of values sample Each sample holds a variable number of values, all taken together constitute a Sample Point Write a separate function to Read a file of delimited Sample Points into an array of pointers to...

  • You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and...

    You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and functions. a. Add four arithmetic operators, +, -, *, and /. Remember that to add or subtract two Fractions, you first must convert them to Fractions with a common denominator. You multiply two Fractions by multiplying the numerators and multiplying the denominators. You divide two Fractions by inverting the second Fraction, then multiplying. After any arithmetic operation, be sure the Fraction is in proper...

  • // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given...

    // Write the compiler used: Visual studio // READ BEFORE YOU START: // You are given a partially completed program that creates a list of patients, like patients' record. // Each record has this information: patient's name, doctor's name, critical level of patient, room number. // The struct 'patientRecord' holds information of one patient. Critical level is enum type. // An array of structs called 'list' is made to hold the list of patients. // To begin, you should trace...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT