value returning function problems

here are my practice problems:


the answers that i have are at the bottom.(if you guys can check it and provide the right answer if i got it wrong, that would be great)



5. Write a C++ statement that displays a random integer from 50 through 100 on the computer screen.

6. Write a C++ statement that assigns the square root of a number to a double variable named sqRoot. The number is stored in a double variable named num.

7. Write the C++ code for a function that prompts the user to enter a character and then stores the character in a char variable named response. The functions should return the contents of the response variable. Name the function getCharacter. (The function will not have any actual arguments passed to it.) Also write an appropriate function prototype for the getCharacter function. In addition, write a statement that invokes the getCharacter function and assigns its return value to a char value named custCode.

8. Write the C++ code for a function that receives four double numbers. The function should calculate the average of the four numbers and then return the result. Name the function calcAverage. Name the formal parameters num1, num2, num3, num4. Also write an appropriate function prototype for the calcAverage function. In addition, write a statement that invokes the calcAverage function and assigns its return value to a double variable named quotient. Use the following numbers as the actual arguments: 45.67, 8.35, 125.78, 99.56.

9.Write the C++ statement that adds the cube of the number stored in the num1 variable to the square root of the number stored in the num2 variable. The statement should assign the result to the answer variable. All of the variables have the double data type.

10. Write a C++ statement that assigns to the answer variable the square root of the following expression: x^2 * y^3. The x,y and answer variable have the double data type.

5. 1 + 50 % (100 – 50 + 1)
6. sqRoot (num)
7. char getcharacter (char)
cout<<”Enter a character”:
cin>> response;
getcharacter (response)
{
return custcode;
} // end of getcharacter function
8.
9. answer = pow(num1) + sqrt(num2);
10. answer = pow(x,2) * pow(y,3);





#5 doesn't provide random numbers because the statement is a constant expression.
#6 doesn't define a variable or assign the square root
#7 is invalid syntax
#8 is missing
#9 is missing a parameter to one of the functions
#10 doesn't return the square root
Last edited on
Topic archived. No new replies allowed.