Question

Using C++

1) Write a function that receives the numerator and denominator of a fraction and returns its corresponding value as a double

cout << Please enter the numerator and the denominator: ; // store the values gotten from the keyboard in the corresponding

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

#include <iostream>
#include <cmath>
using namespace std;

double frac2real(int num, int den,int numOffDigit)
{
//variable declaration
double value;
value = (double) num / den;
  
value = (int)(value * pow(10, numOffDigit) + .5);
value = value / pow(10, numOffDigit);
return value;
}

int main()
{
//variable declaration
int num, den, numOffDigit;
  
//get input from the user
cout<<"Please enter the numerator and the denominator: ";
cin>>num>>den;
cout<<"Now the number of decimal digits for the real number: ";
cin>>numOffDigit;
  
//display output
cout<<endl<<"Numerator: "<<num;
cout<<endl<<"Denominator: "<<den;
cout<<endl<<"Fraction = "<<frac2real(num, den, numOffDigit);

return 0;
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Using C++ 1) Write a function that receives the numerator and denominator of a fraction and...
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++ I am suppose to write a function that gets a number from a user, say...

    c++ I am suppose to write a function that gets a number from a user, say 3.123456789 and then the program gets another number from the user which is used to find out how many decimal digits we want rounded to. So if the user inputs 3 then the number would turn into 3.123 then we are suppose to add two digits next to the rounded number so it would turn into 3.12300. the rounding is suppose to be done...

  • dev c+ Write the function called simplicity, which makes the denominator and denominator values of the...

    dev c+ Write the function called simplicity, which makes the denominator and denominator values of the simple fraction sent to it as simple as possible, together with a main function where the denominator and denominator information are entered from the keyboard. Print the values of variables that hold the numerator and denominator values entered from the keyboard before sending them to the function and their changed states after they are sent to the screen. (The global variable should not be...

  • 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...

  • Problem: Write a short C++ program that gets the side of a cube and the radius...

    Problem: Write a short C++ program that gets the side of a cube and the radius of a sphere from the keyboard and writes to a file the surfaces of these shapes.             ------------------------------------------------------------------------------------------------------------------------ Your task: implement in C++ the algorithm solution shown below. ------------------------------------------------------------------------------------------------------------------------ Part A (79 points) Algorithm solution (in pseudocode): To accomplish this task, your program will have to take the following steps: 1. Declare a variable named outFile that represents an output stream. 2. Declare a...

  • In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with...

    In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with Fraction addition. \**************Homework 5 code*****************************/ #include<iostream> using namespace std; class Fraction { private: int wholeNumber, numerator, denominator;    public: //get methods int getWholeNumber() { return wholeNumber; } int getNumerator() { return numerator; } int getDenominator() { return denominator; } Fraction()// default constructor { int w,n,d; cout<<"\nEnter whole number : "; cin>>w; cout<<"\nEnter numerator : "; cin>>n; cout<<"\nEnter denominator : "; cin>>d; while(d == 0)...

  • 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...

  • Write a C function named date() that receives an integer number of the long date, date...

    Write a C function named date() that receives an integer number of the long date, date form is yyyymmdd, such as 20070412; and the addresses of three variables named month, day, and year. determines the corresponding month, day, and year; and returns these three values to the calling function. For example, if date() is called using the statement     longdate=20200411; date(longdate, &month, &day, &year); the number 4 should be returned in month, the number 11 in day, and the number...

  • C++ Write a function that receives a length in yards, feet, and inches and returns the correspond...

    C++ Write a function that receives a length in yards, feet, and inches and returns the corresponding measurement in centimeters. (1 in = 2.54001 cm). The function is expected to receive the reference parameters yards, feet and inch. Ask users to enter the number of values for yards, feet and inches, and the function will convert into centimeters once the function is called. Use cout in the main() to display the result.

  • answer in c++ Using the table below, complete the C++ code to write the function prototype...

    answer in c++ Using the table below, complete the C++ code to write the function prototype statement, and the void function header. The void function should receive three double variables: the first two by value and the last one by reference. Name the formal parameters num1, num2 and answer. The function should divide the num1 variable by the num2 variable and then store the result in the answer variable. Name the function calcQuotient. Also write an appropriate function prototype for...

  • Write a C++ program and algorithm to create a class of fraction having variables numerator and...

    Write a C++ program and algorithm to create a class of fraction having variables numerator and denominator, and member functions setting and getting values of the fractions. Also create a static member variable of the class having fraction value tolerance of 0.1. Instantiate 10 fraction objects and find out how many of them are below and above tolerance level. Decrement the tolerance to 0.01 by a static member function and find out how many of them are now above and...

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