CPS 6, Quiz Questions, Feb 2 - 4, Spring 2000

Two or three of these questions will be used for an in-class quiz at the end of the week. It's possible that the questions may be modified slightly.
  1. 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 
    
      |---|---|---|---|---|---|
      |---|---|---|---|---|---|
      
    

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

  3. 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.

    
    
    
    
    
    
    
    
  4. 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.
     |-----|-----|                  |---|---|---|---|
     |-----|-----|                  |---|---|---|---|
    

  5. 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