Assignment #4 - Functions

Due: Mon, June 28

Objective

This assignment will consist of writing several different small programs that involve practice writing and calling functions.

Task

Write the following programs, each in a separate file. Filenames should be: (Note that the filenames are all lowercase)

Exercise 1

Filename: digits.cpp
  1. Write a function called SumDigits that takes in an integer parameter and computes and returns the sum of its digits. If the incoming integer is negative, then disregard the negative (and still compute the sum of the digits, as a positive result).
  2. To test this function, write a main() routine that enters a loop, in which the user is prompted and allowed to enter any integer (0 to exit the loop), and the integer is sent to the function and the result printed. The user should keep being asked for inputs until they type a 0 to exit.

Sample Run

(user input underlined)
  Please enter an integer (0 to quit): 124566

  The sum of the digits of 124566 is 24

  Please enter an integer (0 to quit): 23453245

  The sum of the digits of 23453245 is 28

  Please enter an integer (0 to quit): 45454545

  The sum of the digits of 45454545 is 36

  Please enter an integer (0 to quit): -9189845

  The sum of the digits of -9189845 is 44

  Please enter an integer (0 to quit): 0

  Goodbye


Exercise 2

Filename: seconds.cpp
  1. Write a function called Seconds that takes in three integer parameters (representing hours, minutes, and seconds) and returns the number of seconds since the last time the clock "struck 12" (i.e. was at 12:00:00 -- AM or PM doesn't matter since you're not tracking this).
     
  2. To test this function, write a main() routine (in the same file) that prompts the user to enter hours, minutes, and seconds for two different clock times; then uses the Seconds function to calculate the shortest amount of time in seconds between the two times (both of which are within one 12-hour cycle of the clock); the print out the number of seconds since "striking 12" for both clocks, as well as the number of seconds between the two clock times

Sample Run 1:

(user input underlined)
  Input first clock time...
  Hours: 6
  Minutes: 45
  Seconds: 30

  Input second clock time...
  Hours: 4
  Minutes: 50
  Seconds: 12

  It's been 24330 seconds since the first clock struck 12:00
  It's been 17412 seconds since the second clock struck 12:00
  The two times are 6918 seconds apart.

Sample Run 2:

(user input underlined)
  Input first clock time...
  Hours: 12
  Minutes: 43
  Seconds: 16

  Input second clock time...
  Hours: 7
  Minutes: 11
  Seconds: 59

  It's been 2596 seconds since the first clock struck 12:00
  It's been 25919 seconds since the second clock struck 12:00
  The two times are 23323 seconds apart.

Exercise 3

Filename: primes.cpp
  1. Recall that an integer is a prime number if it is divisible only by 1 and itself. Write a function called IsPrime that takes in one integer parameter X and determines whether or not X is prime. The function should return a bool value: (Hint: The % operator is good for checking for divisibility of one integer by another)
     
  2. To test this function, write a main() routine that asks the user to input a positive integer N. Using your IsPrime() function, find and print all the prime numbers less than or equal to N, where the output has 8 numbers per line (you can use tab characters to separate numbers on a line).

Sample Run

(user input underlined)
 Please input a positive number: 500
 The prime numbers less than or equal to 500 are:
 2       3       5       7       11      13      17      19
 23      29      31      37      41      43      47      53
 59      61      67      71      73      79      83      89
 97      101     103     107     109     113     127     131
 137     139     149     151     157     163     167     173
 179     181     191     193     197     199     211     223
 227     229     233     239     241     251     257     263
 269     271     277     281     283     293     307     311
 313     317     331     337     347     349     353     359
 367     373     379     383     389     397     401     409
 419     421     431     433     439     443     449     457
 461     463     467     479     487     491     499

Requirements for all programs


Submitting:

Program submissions should be done through the submission web page, linked from the main course web site. Submit the files:
 digits.cpp
 seconds.cpp
 primes.cpp