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:
- digits.cpp
- seconds.cpp
- primes.cpp
(Note that the filenames are all lowercase)
Exercise 1
Filename: digits.cpp
- 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).
- This function should not depend on any LIMIT to the number of
digits. Assume no limit to how many digits a number can have (remember
the range of an int depends on the computer architecture -- it differs
for different machines -- the function should work for any number of
digits).
- 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
- 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).
- 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
- 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:
- return true if X is a prime number
- return false if X is not a prime number.
(Hint: The % operator is good for checking for divisibility of
one integer by another)
- 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
- No global variables, other than constants
- The required tasks must be performed with the functions
specified (not just with a single main() routine)
- Note that each exercise requires the writing of a function, and a
main routine to test that function.
- Each function should do exactly the task specified in item 1
of each exercise
- Item 2 of each exercise specifies what to do in main() --
this will always involve CALLING the function, sometimes more
than once
- Note that there is NO keyboard-input/screen-output specified in the
functions themselves (i.e. "return" does not mean "print")
- All input and output must be done with streams
- You may use the iostream and iomanip libraries
(the ones that have been discussed in class)
- When you write source code, it should be readable and
well-documented.
- Your program should only use standard ANSI header files (make sure
to
follow the directions exactly on the handout for creating Visual C++
projects, so that Windows-specific headers like stdafx.h and conio.h are
not placed into your file)
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