| 3.1 | Prompt the user for an integer and test if its even or odd. Consider using the way
Ruby rounds integers.
|
| |
| 3.2 | Modify compare.rb so that it
a) prompts the user for an integer between five and ten (inclusive), and displays
whether or not the input was correct. Implement this using an if/else statement.
b) prompts the user for an integer between five and ten and then informs them if the
integer was blow the range in the range or above the range. Implement this using
a case statement.
|
| |
| 3.3 | 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].
|
| |
| 3.4 | Prompt the user for integers x, y and z. Implement the following decision tree based
on the values using if statements. Output how many of the variables are greater then 0.
|
| |
| 3.5 | Achieve the same result as in question 4 but using only one case statement.
|
| |
| 3.6 | A GPS system might triangulate your position on the globe, and subsequently look up
those coordinates to figure out your altitude. Think of the figure below as a
topographic map, with altitude values of 0, 1m and 2m for the dark, medium and light
areas respectively. Using either IF or CASE statements, write a program that takes x
and y integer coordinates from a user and outputs the altitude.
|
| |
| 3.7 | Take a four digit number as an input from the user one digit at a time. Then take
another one digit integer as an input. Determine if the first number is divisible by
the second by implementing the following rules:
2 If the last digit is even, the number is divisible by 2.
3 If the sum of the digits is divisible by 3, the number is also.
4 If the last two digits form a number divisible by 4, the number is also.
5 If the last digit is a 5 or a 0, the number is divisible by 5.
6 If the number is divisible by both 3 and 2, it is also divisible by 6.
7 Take the last digit, double it, and subtract it from the rest of the number;
if the answer is divisible by 7 (including 0), then the number is also.
8 If the last three digits form a number divisible by 8,
then so is the whole number.
9 If the sum of the digits is divisible by 9, the number is also.
|
| 3.8 | Write a program to solve a system of two linear equations. Make sure to account for
the cases where there is no solution/there are an infinite number of solutions.
|
| |
| 3.9 | A leap year is a year containing an extra day. Every year divisible by 4 is a leap
year. However, if the year is also divisible by 100, it will not be a leap year
unless the year is also divisible by 400. Write a program that asks a user for an
input year and determines if that year is a leap year.
|
| |
| 3.10 | Write a program that outputs Good morning/Good afternoon/Good evening/Good night
world based on the time of day.
|
| |
| 3.11 | Draw a diagram that illustrates the actions a program would take that takes an integer
input, and then two more after which it is output if the most recent input is larger,
smaller, or equal to the previous one.
a) Draw a diagram to illustrate the program's execution
b) Write the program using if statements.
c) Write the program using case statements.
|
| 3.12 | Draw a diagram that illustrates the actions a program would take that takes three
integer inputs and then outputs the largest and the smallest of the inputs.
a) Draw a diagram to illustrate the program's execution
b) Write this program using if statements.
c) Write this program using case statements.
|
| 3.13 | What does the following code do?
array = [gets, gets, gets, gets]
array.sort!
puts array
|
| 3.14 | Write a program to output whether a given input is even or odd.
|
| |
| 3.15 | Numbers in ruby have a method named 'between?' on them. This is very useful in 'if'
statements. An example of this is:
1.5.between?(1,2)
This will evaluate to 'true'. For example:
number = 1.5
if number.between?(1,2)
print number, ' is between 1 and 2!'
else
print number, ' is not between 1 and 2!'
end
a) Use this to determine if a number is between 1 and 100
b) Use this to determine if one input is between the next two inputs
|
| 3.16 | Similar to the first example in the book, 'gets' can be used to to get any string of
characters from the keyboard until a special 'newline' character, created when you
press the return key.
input = gets
puts input # outputs: stuff\n
input = gets
puts input.chomp # outputs: stuff
We want to strip this newline key from the input like so:
input = gets
input.chomp!
puts input
Note how Chomp has a '!' after it. The '!' means to modify the variable in place.
a) Use this to match input for the letter 'a'
b) Use this to match input for the word 'awesome'. If it matches, output "You're
right!", otherwise output "Cheese"
c) Use this to match input for the string '42'. NOTE: '1' is a string; 1 is an
integer, so don't use input.to_i.
|
| 3.17 | You are ordering pizza for yourself and a bunch of friends. Each of your friends
requests some number of slices, you sum all those numbers and arrive at a total
amount of slices. Pizza comes in pies with 8 slices each. Write a program that takes
the total amount of slices, and returns the smallest amount of pies that would be
enough.
|
| |
| 3.18 | Write a program that, given two points on a two-dimensional graph, outputs if the
line that connects them is horizontal, vertical, or if the slope is positive or
negative.
|
| |
| 3.19 | Write a program that takes two integer inputs and outputs if either input is
divisible by the other.
|
| |
| 3.20 | You have three friends in your class. Write a program that asks who they are, then
says something different for each one if it maches.
|
| |