Question

Write a C++ program that has this FRACTION structure struct FRACTION{ int num; int den; };...

Write a C++ program that has this FRACTION structure

struct

FRACTION{

int num;

int den;

};

That will keep track of numerator and denominator of a fraction.

using pointers

that is :

getFraction() function will now return a pointer too a struct.
multiply()function will now accept two struct via a pointer and return the result via a pointer.
add()function will now accept two struct via a pointer and return the result via a pointer.

1 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

struct FRACTION{
   int num;
   int den;
};

struct FRACTION* getFraction(){
   struct FRACTION* val = new struct FRACTION[1];
   int temp;
   cout<<"Enter numerator: ";
   cin>>temp;
   val->num = temp;
   while(1){
      cout<<"Enter denominator: ";
      cin>>temp;
      if(temp != 0){
         break;
      }
   }
   val->den = temp;
   val;
}

struct FRACTION* multiply(struct FRACTION* val1, struct FRACTION* val2){
   struct FRACTION* val = new struct FRACTION[1];
   val->num = val1->num * val2->num;
   val->den = val1->den * val2->den;
   return val;
}

struct FRACTION* add(struct FRACTION* val1, struct FRACTION* val2){
   struct FRACTION* val = new struct FRACTION[1];
   val->num = val1->num + val2->num;
   val->den = val1->den + val2->den;
   return val;
}

int main() {
   struct FRACTION* val1 = getFraction();
   struct FRACTION* val2 = getFraction();
   struct FRACTION* val = add(val1,val2);
   cout<<"Add result: "<<endl;
   cout<<"numerator = "<<val->num<<endl;
   cout<<"denominator = "<<val->den<<endl;
   val = multiply(val1,val2);
   cout<<"Multiply result: "<<endl;
   cout<<"numerator = "<<val->num<<endl;
   cout<<"denominator = "<<val->den<<endl;
    return 0;
}

\color{red}Please\; upvote\;the \;solution \;if \;it \;helped.\;Thanks!?

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that has this FRACTION structure struct FRACTION{ int num; int den; };...
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
  • C++ Checkpoint 11.33 Look at the following structure definition: struct Date int month int day int year, Write the pr...

    C++ Checkpoint 11.33 Look at the following structure definition: struct Date int month int day int year, Write the prototype for a function called getlnput that accepts a pointer to a Date structure as a parameter. Type your program submission here. Checkpoint 11.28 Look at the following structure definition struct Rectangle int length int width Write the definition of a pointer to a Rectangle structure called rectPtr and assign it the value nullptr Fype your progzam submission heze

  • I need to do object oriented programming for c++. I had to make make a program...

    I need to do object oriented programming for c++. I had to make make a program where it would add,subtract,multiply,and divide fraction. I got that working, but it wont work for negative can anyone fix it and explain how they did it? Source.cpp code: #include "Fraction.h" #include <iostream> using namespace std; int main() {       Fraction f1(-4, 6);    Fraction f2(5, -9);    Fraction sum = sum.add(f1, f2);    sum.print(sum);    Fraction diff = diff.subtract(f1, f2);    diff.print(diff);   ...

  • Using C++ 1) Write a function that receives the numerator and denominator of a fraction and...

    Using C++ 1) Write a function that receives the numerator and denominator of a fraction and returns its corresponding value as a double precision real number. The function also receives the number of digits that the value must be rounded to before returned. To test it, you must implement the algorithm provided below. Note: You must define the most appropriate type of function, the parameter list (using the most appropriate parameters), and the body of the function to be implemented....

  • I need help with the following Java code Consider a class Fraction of fractions. Each fraction...

    I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...

  • Refer to this header file: // Fraction class // This class represents a fraction a /...

    Refer to this header file: // Fraction class // This class represents a fraction a / b class Fraction { public: // Constructors Fraction(); // sets numerator to 1 and denominator to 1 Fraction(int num, int denom); // Setters void setNumerator(int num); void setDenominator(int denom); // getters int getNumerator()const {return num;} int getDenominator()const {return denom;} double getDecimal(){return static_cast<double> num / denom;} private: int num, denom; }; 1.Write the code for the non-member overloaded << operator that will display all of...

  • Having to repost this as the last answer wasn't functional. Please help In the following program...

    Having to repost this as the last answer wasn't functional. Please help In the following program written in C++, I am having an issue that I cannot get the reducdedForm function in the Rational.cpp file to work. I cant figure out how to get this to work correctly, so the program will take what a user enters for fractions, and does the appropriate arithmetic to the two fractions and simplifies the answer. Rational.h class Rational { private: int num; int...

  • Code in C language ADT: typedef struct{ int ID; float salary; int age; }Employee; ...

    code in C language ADT: typedef struct{ int ID; float salary; int age; }Employee; Specification: In this lab, five functions need to be implemented using the given ADT. 1. Employee* readRecord(FILE*) This function receives a FILE pointer created before. It reads a line from the provided csv file, and creates an Employee struct pointer with the information from that line then returns the pointer back to the calling function. Each line in the provided csv file contains the id, salary,...

  • Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert...

    Write a Fraction class. An example of a fraction is 1/2. Note that C/C++ will convert it to 0.5, but for this problem, it should still be displayed as 1/2. You should have at least the following two private member variables: numerator (top part), and denominator (bottom part). Overload the following operators: ==, +, << and >>. Also, implement the default constructor and a second constructor that takes two arguments for the numerator and the denominator. Make sure the denominator...

  • C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use...

    C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use functions. Use a custom library and namespace. Use reference variables as parameters to return values from functions. Create a robust user interface with input error checking. Use exceptions to indicate errors. Resources kishio.h kishio.cpp Assignment requirements You will need eight methods (including main). One is given to you. Their requirements are specified below: menu: The menu function takes no arguments, but returns a char...

  • Structure struct Node int Manth; // Mont h double dAvg: 1/ Average struct Node pNext // with, the linked İist 3hown above the function will return gven that the average is 3.8 Ptr to next -Nod...

    Structure struct Node int Manth; // Mont h double dAvg: 1/ Average struct Node pNext // with, the linked İist 3hown above the function will return gven that the average is 3.8 Ptr to next -Node; Ret (3,3.8) (4,2.5) (20pts)( Recursive function) Show the code for a function that receives a pointer to the head of an ordered singly linked list that uses the structure in the top left. The function will return the pointer node that shows the highest...

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