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

  1. 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?
  2. 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.
  3. 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.

  1. How to declare a while loop that prints:-
  1. All squares less than n. For example, if n is 100, print 0 1 4 9 16 25 36 49 64 81.
  2. All powers of two less than n. For example, if n is 100, print 1 2 4 8 16 32 64.

  1. How to declare a loop that computes:-
  1. The sum of all even numbers between 2 & 100 (inclusive).
  2. The sum of all squares between 1 & 100 (inclusive).
  3. The sum of all odd numbers between a & b (inclusive).
  4. The sum of all odd digits of n. (For example, if n is 32677, the sum would be 3 + 7 + 7 = 17).
  1. How to declare the following code that read a line of input as a string & print
  1. Only the uppercase letters in the string.
  2. Every second letter of the string.
  3. The string, with all vowels replaced by an underscore.
  1. How to declare the following functions & provid3e a program to test them.
  1. Double smallest (double x, double y, double z), returning the smallest of the arguments.
  2. Double average (double x, double y, double z), returning the average of the arguments.
  1. How to declare the following functions & provide a program to test them:-
  1. bool all_the_same (double x, double y, double z), returning true if the arguments are all the same.
  2. bool all_different (double x, double y, double z), returning true if the arguments are all different.
  1. How to declare the following functions:
  1. int first_digit (int n), returning the first digit of the argument.
  2. int last_digit (int n), returning the last digit of the argument.
  3. 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.

  1. 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.
  2. 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!