Question

Read each question carefully before you answer. For full credit trace and show that value of...

Read each question carefully before you answer. For full credit trace and show that value of all the isted variables and walkthrough your code where indicated. Use the function names provided to complete the question and read the associated descriptions to understand how the function works. Your program must use functions, and unless otherwise indicated assume file input.

2-Create a struct to hold a fractio Suct to hold a fraction. The struct should have an integer numerator and ger denominator member. Write a function called addFractions that takes born fractions, adds them together. Return the result to the calling function.

c++ programming

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

#include<iostream>
using namespace std;

//struct fraction
typedef struct fraction {
   //numerator and denominator
   int numerator;
   int denominator;
} Fraction;

Fraction addFraction(Fraction f1, Fraction f2) {
   /*
       f1 = a/b
       f2 = c/d
      
       f1 + f2 = a/b + c/d = (a.d + b.c) / bd
   */
  
   Fraction f3;
   f3.numerator = f1.numerator*f2.denominator + f2.numerator*f1.denominator;
   f3.denominator = f1.denominator*f2.denominator;
  
   return f3;  
}


//helper function to print content of strcuct Fraction
void printFraction(Fraction f) {
   cout << "Numerator: " << f.numerator;
   cout << "\nDenominator: " << f.denominator << endl;
}


int main() {
  
   //declaring and initialising two fractions f1, f2
   Fraction f1, f2;
   f1.numerator = 5;
   f1.denominator = 7;
  
   f2.numerator = 3;
   f2.denominator = 5;
  
   cout << "Fraction f1: " << endl;
   printFraction(f1);
  
   cout << "Fraction f2: " << endl;
   printFraction(f2);
  
   Fraction f3 = addFraction(f1, f2);
   cout << "Fraction f1+f2: " << endl;
   printFraction(f3);
  
}

IF THERE IS ANYTHING THAT YOU DO NOT UNDERSTAND, OR NEED MORE HELP THEN PLEASE MENTION IT IN THE COMMENTS SECTION.

Add a comment
Know the answer?
Add Answer to:
Read each question carefully before you answer. For full credit trace and show that value of...
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 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