) Write a complete C++ program in one file which takes a double value from the user, cubes it, and prints the result. Your program must use a function which takes a parameter for the value to be cubed and returns the result by value. The function may not print anything or read anything directly from the user (i.e. no cin/cout in the function). Hint: n cubed is defined as n*n*n. Upload as cubeValue.cpp. (8 pts

Respuesta :

Answer:

The cpp program is given below.

#include <stdio.h>

#include <iostream>

using namespace std;

//function for cubing declared

double cubeValue(double n);

int main()

{

   //variable to hold user input

   double num;

   std::cout <<"Enter a number to be cubed: ";

   cin>>num;

   //variable to hold cube

   double cube;

   //function returns cube

   cube = cubeValue(num);

   std::cout <<"The cube of " <<num <<" is "<<cube <<std::endl;

   return 0;

}

//function to compute cube of the given parameter

//function definition

double cubeValue(double n)

{

   double d;

   d = n*n*n;

   return d;

}

OUTPUT

Enter a number to be cubed: 3.4

The cube of 3.4 is 39.304

Explanation:

1. The function for cubing is declared in the beginning of the program. The function has double return type as well as takes a double parameter. The function is defined after the main() method. The function only returns the cube of the double parameter passed to it.

double cubeValue(double n);

2. Inside main(), user input is taken for the number whose cube is to be computed.

3. The user input is taken in variable, num.

4. Another double variable, cube, is declared.

5. The method, cubeValue(), is called by passing variable num as parameter.

6. The value returned by the method, cubeValue() is assigned to the variable, cube.

cube = cubeValue(num);

7. The cube is displayed to the user using cout.

8. The program ends with return statement.

return 0;

The return statement assures successful execution of the program.

The program may not contain any syntax errors but contain logic errors which is indicated by the error message. The error message is displayed in red with return value -1.

9. The program is saved as cubeValue.cpp.

10. The method, cubeValue(), does not takes user input and does not prints any message.

11. The code is not written inside the class. The program contains only two methods.

12. The program can be tested for both integer and double values.

13. The program is written in cpp as per the requirements.