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

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)
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?
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...
C++ programming language. Write a program that will read in id numbers and place them in...
C++ programming language. Write a program that will read in id numbers and place them in an array.The array is dynamically allocated large enough to hold the number of id numbers given by the user. The program will then input an id and call a function to search for that id in the array. It will print whether the id is in the array or not. Sample Run: Please input the number of id numbers to be read 4 Please...
In C Programming Language Write a program to output to a text log file a new...
In C Programming Language Write a program to output to a text log file a new line starting with day time date followed by the message "SUCCESSFUL". Please screenshot the results.
Using the C Programming language, write a program that sums an array of 50 elements. Next,...
Using the C Programming language, write a program that sums an array of 50 elements. Next, optimize the code using loop unrolling. Loop unrolling is a program transformation that reduces the number of iterations for a loop by increasing the number of elements computed on each iteration. Generate a graph of performance improvement. Tip: Figure 5.17 in the textbook provides an example of a graph depicting performance improvements associated with loop unrolling. Marking:- Optimize the code for an array of...
In the C# programming language... Write a program to perform student record manage for class IST311....
In the C# programming language... Write a program to perform student record manage for class IST311. Create student record class (Student.cs) and it should get the student information from the user and set up student record class. The program will perform recording student’s grade information and compute final grade, and then print out the student’s class information. Student class definitions: It should contain attributes as following: first name, last name, 5 labs grade, 3 grade, final grade. Its member functions...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT