Вы находитесь на странице: 1из 2

Fundamentals of Programming ¾ Homework Semester 1

HOMEWORK #7 (REPETITION IN C++)

7. Implement in C++ the following PDL program (partially developed for Homework #6), that reads
in an integer value representing somebody's year of birth (expressed in the YYYY format) and
produces that person's "magic lucky number". Such a number is obtained by repetitively adding
up all the digits in that year until they reduce to a single digit.
For example, the magic number for 1982 is 2 (first 1 + 9 + 8 + 2 = 20, then 2 + 0 = 2).

global birthYear, //hold the (four-digit) year number


magicNumber //hold the corresponding (one-digit) magic number

magicYourYear
call getBirthYear //read in a valid year number
call produceMagicNumber //calculate the magic number
output( "You were born in : ", birthYear)
output( "The magic number is: ", magicNumber) //output magic number

//It reads in a number representing a valid year of birth (i.e. positive or zero).
//If the user enters an invalid value the program displays an error message
//and ask for another input until a valid value is read in
//pre-condition: none
//post-condition: produces birthYear
proc getBirthYear
output( "Enter the (four-digit) number representing a year: ")
input( birthYear)
while ( birthYear < 0) do
output( "ERROR: negative value!")
output( "Enter the (four-digit) number representing a year: ")
input( birthYear)
endwhile
endproc

//It produces the year's "magic number".


//pre-condition: ( birthYear ≥ 0)
//post-condition: produces magicNumber
proc produceMagicNumber
set magicNumber to birthYear
while ( magicNumber > 9) do
call sumUpNumber
endwhile
endproc

//It calculates the sum of the digits in number


//pre-condition: (magicNumber ≥ 0)
//post-condition: produces magicNumber
proc sumUpNumber
local digit, numberSum
set numberSum to 0
while (magicNumber > 0) do
set digit to (magicNumber mod 10) //find right-most digit
set numberSum to (numberSum + digit) //add it to numberSum
set magicNumber to (magicNumber div 10) //ignore that digit
endwhile
set magicNumber to numberSum
endproc

26219327.doc, 02/11/21 Module 4374, Homework¾1


Fundamentals of Programming ¾ Homework Semester 1

For this exercise, you should:


 Use any of the C++ facilities introduced so far that you may need.
 Keep the structure of the PDL algorithm given in your C++ program (same procedures,
same order andsame logic).
 Keep the identifiers given in the PDL solution for the variables & functions used in your
C++ implementation.
 Use the global and local variables/constants declared.
 Implement each of the pre-conditions given in PDL by an assert statement in C++.
 Put comments (between the two // up to the end of the line or between /* and
*/) to give information about your program, the functions & variables it used so
as to make your solution easier to understand.
 Make sure that your algorithm works for ANY year in the valid range.

You do NOT have to check the validity of the type of the input (i.e. you can assume that the user
will only enter a number for the year).

26219327.doc, 02/11/21 Module 4374, Homework¾2

Вам также может понравиться