Exercise #6 - Functions
Exercise #6 - Functions
1. Write a function called printRange that accepts two integers as arguments and prints the sequence of numbers
between the two arguments, enclosed in square brackets. Print an increasing sequence if the first argument is smaller
than the second; otherwise, print a decreasing sequence. If the two numbers are the same, that number should
beprinted between square brackets. Here are some sample calls to printRange:
printRange(2, 7);
printRange(19, 11);
printRange(5, 5);
The output produced from these calls should be the following sequences of numbers:
[2, 3, 4, 5, 6, 7]
[19, 18, 17, 16, 15, 14, 13, 12, 11]
[5]
2. Write a function called evenSumMax that should prompt the user for a number of integers, then prompt the integer
that many times. Once the user has entered all the integers, the function should print the sum of all the even numbers
the user typed, along with the largest even number typed. You may assume that the user will type at least one
nonnegative even integer. Here is an example dialogue:
3. Write a function called isVowel that accepts a character as input and returns 1 if that character is a vowel (a, e, i,
o, or u), 0 otherwise. For an extra challenge, make your function case-insensitive.
4. Write a function called contains7 that accepts an integer parameter as input and returns 1 if this number contains 7 as
a digit, 0 otherwise.
5. Write an improved version of the following function, which returns whether the given number of cents would require
any pennies (as opposed to being an amount that could be made exactly using coins other than pennies):
For each of the following calls, indicate the value that is returned:
mystery(3, 3);
mystery(5, 3);
mystery(2, 6);
mystery(12, 18);
mystery(30, 75);
7. Write a function called isLeapYear that accepts an integer parameter as input and returns 0 if the input, the year
value, is a leap year and 0 otherwise. For example, the call isLeapYear(2023) should return 0 while isLeapYear(2024)
should return 1.
8. Write a function called isSumEven that accepts an integer parameter as input and returns 0 if the sum of the digits of
the integer input is even, and 0 otherwise. For example, the call isSumEven(20239732) should return 1 while
isSumEven(12024) should return 0.
9. Write a function called hopscotch that accepts an integer number of “hops” as input and prints a pattern of
numbers that resembles a hopscotch board. A “hop” is a three-number sequence where the output shows two numbers
on a line, followed by one number on its own line. 0 hops is a board up to 1; one hop is a board up to 4; two
hops is a board up to 7; and so on. For example, the call of hopscotch(3); should print the following output:
1
2 3
4
5 6
7
8 9
10
A call of hopscotch(0); should print only the number 1. If it is passed a negative value, the function should produce
no output.
10. Write a function called monthApart that accepts four integer parameters, m1, d1, m2, and d2, representing two
calendar dates. Each date consists of a month (1 through 12) and a day (1 through the number of days in that month
[28–31]). Assume that all parameter values passed are valid. The function should return true if the dates are at least
a month apart and false otherwise. For example, the call of monthApart(4, 15, 5, 22) would return 1(true)
while the call of monthApart(9, 19, 10, 17) would return 0(false). Assume that all the dates in this problem
occur during the same year. Note that the first date could come before or after the second date.
11. Write a function named swapDigitPairs that accepts an integer n as a parameter and returns a new integer
whose value is similar to n’s but with each pair of digits swapped in order. For example, the call of
swapDigitPairs(482596) would return 845269. Notice that the 9 and 6 are swapped, as are the 2 and 5, and the
4 and 8. If the number contains an odd number of digits, leave the leftmost digit in its original place. For example, the
call of swapDigitPairs(1234567) would return 1325476.
12. Write a function called randomWalk that performs steps of a random one-dimensional walk. The random walk
should begin at position 0. On each step, you should either increase or decrease the position by 1 (each with equal
probability). Your code should continue making steps until a position of 3 or -3 is reached, and then report the maximum
position that was reached during the walk. The output should look like the following:
position = 1
position = 0
position = –1
position = –2
position = –1
position = –2
position = –3
max position = 1