Question

In: Computer Science

Lab 1 Write a program in the C/C++ programming language to input and add two fractions...

Lab 1

Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions:

a/b + c/d = (a*d + b*c)/(b*d)

Example:

1/2 + 1/4 = ( 1(4) + 2(1) ) / 2(4) = 6/8 = 3/4

Lab 2

Re-design lab1 using data modeling design methods using the struct mechanism as discussed in the lecture notes entitled “Data Modeling (w/ structures)”. Your program should function exactly like lab1 with the only difference being the object-oriented manner in which it is written. Use the design example from the notes to assist you in the re-design.

I finished the above 2 labs but I need help with the following lab.

Re-design lab2 using a C++ class. Use examples from the study notes on data modeling (w/ classes). Convert your data model from a structure to a C++ class. Convert your data model operations from using functions to class member functions. Re-design your main program using objects from your class definition to perform the same functionality as in lab2. Use the example(s) from the notes to assist you in the re-design.

Please help with this thank you

Solutions

Expert Solution

Answer:

#include<iostream>
using namespace std;

class Fraction
{
private:
int numerator;
int denominator;
public:
Fraction()
{ numerator = 0;
denominator=1;
}
void getFraction();
void showFraction();
Fraction operator+(Fraction);
};

void Fraction::getFraction()
{
cout<<"\nEnter the fraction as follows : \n";
cout<<"Enter numerator : ";
cin>>numerator;
cout<<"Enter denominator : ";
cin>>denominator;
}

void Fraction::showFraction()
{
cout<<"The fraction is : "<<numerator<<"/"<<denominator<<"\n";
}

Fraction Fraction::operator+(Fraction f)
{
Fraction final;
final.numerator = (numerator*f.denominator)+(f.numerator*denominator); //Calculates numerator of result after addition
final.denominator = denominator*f.denominator; //Calculates denominator of result after addition
int flag;
do //This loop simplifies result with the least common denominator
{ int n=2;
flag=0;
if(final.numerator%final.denominator==0)
{
final.numerator=final.numerator/final.denominator;
final.denominator=1;
break;
}
while(n<=final.denominator/2)  
{
if(final.denominator%n==0)
if(final.numerator%n==0)
{
final.numerator = final.numerator/2;
final.denominator = final.denominator/2;
flag=1;   
}
n++;
}  
}while(flag==1);
  
return final;
}

int main()
{
Fraction f1,f2,f3;
f1.getFraction();
f1.showFraction();
f2.getFraction();
f2.showFraction();
f3 = f1+f2; //Here '+' is the overloaded constructor used to add fractions f1,f2 and stores their result in f3
cout<<"\nThe result after adding the fractions gives \n";
f3.showFraction();  
return 0;
}

#please consider my effort and give me a like...thank u....


Related Solutions

Objective: Write this program in the C programming language Loops with input, strings, arrays, and output....
Objective: Write this program in the C programming language Loops with input, strings, arrays, and output. Assignment: It’s an organization that accepts used books and then sells them a couple of times a year at book sale events. Some way was needed to keep track of the inventory. You’ll want two parallel arrays: one to keep track of book titles, and one to keep track of the prices. Assume there will be no more than 10 books. Assume no more...
C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
Write a program to create a tree randomly. You can use C++ programming language. The input...
Write a program to create a tree randomly. You can use C++ programming language. The input is the number of vertices in the tree, and the output is an adjacent list of the tree. (Managed to complete this assignment with a binary tree. But, was told I needed a general tree instead)
in the c programming language input is given in the form The input will be of...
in the c programming language input is given in the form The input will be of the form [number of terms] [coefficient k] [exponent k] … [coefficient 1] [exponent 1] eg. 5 ─3 7 824 5 ─7 3 1 2 9 0 in this there are 5 terms with -3x^7 being the highest /* Initialize all coefficients and exponents of the polynomial to zero. */ void init_polynom( int coeff[ ], int exp[ ] ) { /* ADD YOUR CODE HERE...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a...
In C programming language, write the program "3x3" in size, calculating the matrix "c = a * b" by reading the a and b matrices from the outside and writing on the screen?
Programming Language C++ Task 1: Write a program to calculate the volume of various containers. A...
Programming Language C++ Task 1: Write a program to calculate the volume of various containers. A base class, Cylinder, will be created, with its derived classes, also called child classes or sub-classes. First, create a parent class, Cylinder. Create a constant for pi since you will need this for any non-square containers. Use protected for the members. Finally, create a public function that sets the volume. // The formula is: V = pi * (r^2) * h Task 2: Create...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
Write a program that computes the product of two fractions. The user provides 4 input which...
Write a program that computes the product of two fractions. The user provides 4 input which represent the numerator and the denominator of two fractions and prints the result of the operation. The program uses two functions. One called MultiplyNumerator(…) which calculates the product of the numerators of the fractions, and a second one called MultiplyDenom(…) which calculates the product of the denominator of the fractions. Call your program MultiplyFrac.cpp
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT