CS 99

Summer 2002: HW 9                                                                         07.24

Functions

Due Date: 07.30

1.    Objectives

Completing all tasks in this assignment will help you write simple functions.

First skim, then carefully read the entire assignment before starting any tasks.  

 

2.    Perfect numbers revisited
Write a function called isperfect that returns 1 if its input parameter p is a perfect number, and 0 otherwise.  Use your results from HW 5.

3.    Time-conversion
Part A:

Write a function called
hms2s that converts a time expressed in hours, minutes, seconds to a time expressed in seconds. Function hms2s returns one variable, seconds, and takes three input arguments: hours (h), minutes (m), and seconds (s).

Part B:
Now write a function
s2hms that performs the opposite operation: function s2hms takes one input argument (seconds) and returns the three variables h, m, and s.

 

4.    Palindromes
Write a function isPalindrome( w ) that takes an array of characters and returns true if the characters in the array constitute a palindrome, or false otherwise. A palindrome is the same forwards as backwards, e.g. ['a' 'b' 'c' 'b' 'a'].

 

5.     Shifting Matrices
Write a function upShift( M ) that shifts the rows of its input matrix M, 1 position upwards. The first row must be added at the bottom (wrapping around). Return the up-shifted matrix as a result.

 

6.    Summing lower triangles*

Write a function called sumDownTri that calculates the sum of the lower left triangular part of a rectangular matrix containing numbers.  The three sides of the triangle have the same number of elements.  Three examples of rectangular matrices are shown below.  The cells in the lower left of the triangle are labeled d and the remaining cells are labeled x.

                 d x x x x x x          x x x            d x x x
             d d x x x x x          x x x            d d x x
             d d d x x x x          d x x            d d d x
             d d d d x x x          d d x            d d d d
                                          d d d

Function sumDownTri takes on rectangular matrix M as the input argument and has one output argument triSum that contains the sum of the elements in the lower left triangular part of matrix M.  Assume the matrix is at least 2-by-2 in size.  Do not use the MATLAB predefined function tril.

 

7.     Submitting Your Work
Type your name, student ID, and the date at the top of each file.  Print and sign each file.  Hand the signed documents along with your output to the teaching assistant at the beginning of the lab session on Tuesday 30 July 2002.

*Challenge problem