Question 1: Write a program [7.5]
Write a complete and correct C++ program that fulfills all the requirements below:
Some sample runs of the program, user input shown in bold italics: | |
Please enter a number in the range 0 to 1: -1 That value is too small. | Please enter a number in the range 0 to 1: foo That is not a number. |
Please enter a number in the range 0 to 1: 1.01 That value is too large. | Please enter a number in the range 0 to 1: 0.25 The square root of 0.25 is 0.5. |
SAMPLE SOLUTION #include <cstdio> #include <cmath> int main() { float userVal, root; printf("Please enter a number in the range 0-1:\n"); int numRead = scanf("%g", &userVal); if (numRead == 0) { printf("That is not a number\n"); } else if (userVal < 0) { printf("That value is too small\n"); } else if (userVal > 1) { printf("That value is too large\n"); } else { root = sqrt(userVal); printf("The square root of %g is %g\n", userVal, root); } return 0; } |