Question

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 in a function and I am suppose to write a prototype. This is what I have so far.

// Insert here the prototype of the function here


int main()
{
   // Declare variable value, valuero that hold double precision real numbers
   double value;
   double valuero;

   // Declare variable decdig that holds whole numbers
   int decdig;
   // Prompt the user to "Enter the real number: "
   cout << "Enter a real number:";

   // Read from keyboard the value entered by the user and assign it to side
   cin >> value;
   // Prompt the user to "Enter number of digits: "
   cout << "Enter number of digits";

   // Read from keyboard the value entered by the user and assign it to decdig
   cin >> decdig;
   // Round the real number to the number of decimal digits specified and assign the result to valuero
  
   // Format the output to display the numbers in fixed format with ten decimal digits

   // Display on the screen, using 23 columns, the message
   //   "The original number is ", value
   cout << "The original number is: " << value << endl;
   // Format the output to display the numbers in fixed format with the number of decimal digits specified plus 2

   // Display on the screen, using 23 columns, the message
   //   "The rounded number is ", valuero
   cout << "The rounded number is: " << valuero << endl;

       system("pause");

// Rounds the value received in the first parameter to the number of digits received in the second parameter

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

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

/******************************************************************************

Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int main()
{
// Declare variable value, valuero that hold double precision real numbers
double value;
double valuero;

// Declare variable decdig that holds whole numbers
int decdig;
// Prompt the user to "Enter the real number: "
cout << "Enter a real number:";

// Read from keyboard the value entered by the user and assign it to side
cin >> value;
// Prompt the user to "Enter number of digits: "
cout << "Enter number of digits";

// Read from keyboard the value entered by the user and assign it to decdig
cin >> decdig;
// Round the real number to the number of decimal digits specified and assign the result to valuero
valuero=(double)round((double)round(value*(double)pow(10,decdig))*(double)pow(10,2))/(double)pow(10,decdig+2);
// Format the output to display the numbers in fixed format with ten decimal digits

// Display on the screen, using 23 columns, the message
// "The original number is ", value
cout << "The original number is: " << value << endl;
// Format the output to display the numbers in fixed format with the number of decimal digits specified plus 2

// Display on the screen, using 23 columns, the message
// "The rounded number is ", valuero
cout<<fixed;
cout << "The rounded number is: " <<setprecision(decdig+2)<< valuero << endl;

system("pause");
return 0;
}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
c++ I am suppose to write a function that gets a number from a user, say...
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
  • 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...

  • 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 my coding for C++! The goal of this is to calculate the...

    I need help with my coding for C++! The goal of this is to calculate the total cost of a product by combining the products price and the amount of tax, then display it to the user. The numbers we have to test out is 100 for the price and 8.25 for the percentage. When I test is out, the formula works but it isn't rounding the 108.25 to 108.3, which is the total price we are supposed to display...

  • I am having trouble figuring out what should go in the place of "number" to make...

    I am having trouble figuring out what should go in the place of "number" to make the loop stop as soon as they enter the value they put in for the "count" input. I am also having trouble nesting a do while loop into the original while loop (if that is even what I am supposed to do to get the program to keep going if the user wants to enter more numbers???) I have inserted the question below, as...

  • IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality...

    IP requirements: Load an exam Take an exam Show exam results Quit Choice 1: No functionality change. Load the exam based upon the user's prompt for an exam file. Choice 2: The program should display a single question at a time and prompt the user for an answer. Based upon the answer, it should track the score based upon a successful answer. Once a user answers the question, it should also display the correct answer with an appropriate message (e.g.,...

  • Assignnment #1 Your Very First C++ Program Write a C+program to achieve the followings. Start a new line for each...

    Assignnment #1 Your Very First C++ Program Write a C+program to achieve the followings. Start a new line for each output a. Declare an integer and assign it with a value 45876 b. Prompt the user to input a floating point number of value 345.24681359 Display the message "Welcome to the 2019 EECE 4272 Summer Class!" c. d. Display the number from part (a) with the following format: () fixed floating-point number, (i) 2 digits after the decimal point, (ii...

  • I need help with using the infile and outfile operations in C++... Here's my assignment requirements:...

    I need help with using the infile and outfile operations in C++... Here's my assignment requirements: Assignment: Cindy uses the services of a brokerage firm to buy and sell stocks. The firm charges 1.5% service charges on the total amount for each transaction, buy or sell. When Cindy sells stocks, she would like to know if she gained or lost on a particular investment. Write a program that allows Cindy to read input from a file called Stock.txt: 1. The...

  • Topics c ++ Loops While Statement Description Write a program that will display a desired message...

    Topics c ++ Loops While Statement Description Write a program that will display a desired message the desired number of times. The program will ask the user to supply the message to be displayed. It will also ask the user to supply the number of times the message is to be displayed. It will then display that message the required number of times. Requirements Do this assignment using a While statement.    Testing For submitting, use the data in the...

  • Problem: Write a program to calculate the force of gravitational attraction between two objects of known...

    Problem: Write a program to calculate the force of gravitational attraction between two objects of known mass at a known distance. Use the formula developed by Isaac Newton known as Law of Universal Gravitation. F G*m *m2/d2 Where F (Force of gravity) is expressed in Newtons (N), mi and m2 (masses of the objects) are expressed in kilograms (kgs) and d (distance from the center of one object to the center of the other) is expressed in meters (m) and...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

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