4.8 Exercises

  1. Prompt the user for an integer and test if it’s even or odd. Consider using the way Ruby rounds operations on integers when appropriate. Save this program in a file called compare.rb.

  2. Modify compare.rb so that it:

    1. Prompts the user for an integer between 5 and 10 (inclusive) and displays whether or not the input was correct. Implement this using an if/else statement.

    2. Prompts the user for an integer between 5 and 10 and then informs the user if the integer was below the range, in the range, or above the range. Implement this using a case statement.

  3. Write a program that, given two points on a two-dimensional graph, outputs a message (string) if the line that connects them is horizontal or vertical, or if the slope is positive or negative.

  4. Write a program to solve a system of two linear equations. A linear equation is a mathematical equation that describes a line through its slope (m) and its y-intercept (b), and it will take the form y = mx + b. Make sure to account for the cases where there is no solution or where there are an infinite number of solutions.

  5. The unit pulse function, d[n], is very important in digital signal processing. This function is defined for integers only. It is equal to 1 when n is 0, and it is equal to 0 when n is any other integer. Write a program that prompts the user for an integer n, and returns the value d[n].

  6. Prompt the user for integers x, y, and z. Implement the decision tree shown in Figure 4-6 based on the values using if statements. Output how many of the variables are greater than 0. Note: each right flow option represents false, and each left flow option represents true.

Decision tree for Exercise 6
Figure 4-6. Decision tree for Exercise 6
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.138.178