Question: Please, explain clearly your answers Decision Structures Suppose x & y are variables of type double. How to declare them with a code fragment that
Please, explain clearly your answers
Decision Structures
- Suppose x & y are variables of type double. How to declare them with a code fragment that sets y to x if x is positive and to 0 otherwise?
- Suppose x & y are variable of type double. Write a code fragment that sets y to the absolute value of x without calling the abs function. Use an if statement.
- How to declare a code fragment that reads in two floating-point numbers and tests whether they are the same up to two decimal places.
Output:
Enter two floating-point numbers: 2.0 1.99998
They are the same up to two decimal places.
Enter two floating-point numbers: 2.0 1.98999
They are different.
- How to declare a while loop that prints:-
- All squares less than n. For example, if n is 100, print 0 1 4 9 16 25 36 49 64 81.
- All powers of two less than n. For example, if n is 100, print 1 2 4 8 16 32 64.
- How to declare a loop that computes:-
- The sum of all even numbers between 2 & 100 (inclusive).
- The sum of all squares between 1 & 100 (inclusive).
- The sum of all odd numbers between a & b (inclusive).
- The sum of all odd digits of n. (For example, if n is 32677, the sum would be 3 + 7 + 7 = 17).
- How to declare the following code that read a line of input as a string & print
- Only the uppercase letters in the string.
- Every second letter of the string.
- The string, with all vowels replaced by an underscore.
- How to declare the following functions & provid3e a program to test them.
- Double smallest (double x, double y, double z), returning the smallest of the arguments.
- Double average (double x, double y, double z), returning the average of the arguments.
- How to declare the following functions & provide a program to test them:-
- bool all_the_same (double x, double y, double z), returning true if the arguments are all the same.
- bool all_different (double x, double y, double z), returning true if the arguments are all different.
- How to declare the following functions:
- int first_digit (int n), returning the first digit of the argument.
- int last_digit (int n), returning the last digit of the argument.
- int digits (int n), returning the number of digits of the argument
For example, first_digit(1729) is 1, last_digit(1729) is 9, and digits(1729) is 4.
- How to declare a function string middle (string str) that returns a string containing the middle character in str if the length of str is odd, or the two middle characters if the length is even. For example, middle (middle) returns dd.
- How to declare a function string repeat (string str, int n) that returns the string str repeated n times. For example, repeat (ho, 3) returns hohoho.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
