- Write code in main to print a fence as shown below wehre
the user enters the number of fence-posts.
prompt> fence
how many posts: 4
|---|---|---|
|---|---|---|
prompt> fence
how many posts: 7
|---|---|---|---|---|---|
|---|---|---|---|---|---|
-
A number is Perfect if it is the sum of its divisors (other
than itself). For example:
6 = 1 + 2 + 3
28 = 1 + 2 + 4 + 7 + 14
Write the function IsPerfect that returns true if the parameter
num is perfect, and false otherwise.
bool IsPerfect(int num)
// post: returns true if num is perfect, false otherwise
Hint: test all numbers less than num as divisors, accumulating
a sum of the divisors.
- Write a function DigitSum that returns the sum of the
digits in a number. DigitSum(1075) evaluates to 13 since 1 + 0
+ 7 + 5 = 13. DigitSum(81) evaluates to 9 since 8 + 1 = 9.
int DigitSum(int num)
// pre: 0 <= num
// post: returns sum of digits in num
Hint: use num % 10 to get each digit and num /= 10 to chop off a digit.
- Write a void function Fence that draws a fence with fenceposts
(see problem one). The number of fenceposts and the length of each
cross-piece are parameters. For example, Fence(3,5) should
draw the fence on the left and Fence(5,3) draws the fence on
the right.
|-----|-----| |---|---|---|---|
|-----|-----| |---|---|---|---|
- Using the class Date write a function that counts the
number of Mondays in a year. If d is a Date object,
then d.DayName is "Monday" or "Tuesday" and so on.
int Mondays (int year)
// post: returns number of Mondays in year
Hint: you'll probabaly want to either
- Construct a different date object for every date in the year
OR
- Construct a date object for January 1 and then increment it 365
times (or 366 if the year is a leap year) testing for Monday.