wpe3B5.jpg (4091 bytes) Question #2(a-c)/Answer


Question 2A

You are asked to refine a top-level design for a program intended to test a design for manipulating text. The program accepts a single line of text from the keyboard. It then scans along the line, writing the characters to the screen, except that the charcater '{' is replaced by the pair of single characters '(*'. Thus, the input string

"{This is the beginning of a Pascal comment"

would be written to the screen as follows.

"(*This is the beginning of a Pascal comment"

The top-level design and data table are given below.

1	read in line of text and initialise other data
2	loop while there are still characters to process
3	  write out current character or replacements
4       loopend
Type Identifier Description
String Line Text entered from the keyboard
Integer Index Index of individual elements of Line

Refine this design so that it is ready for coding in C++. You should assume that the line of text entered is non-empty so you need not include any data validation. [7]


Question 2B

You are asked to refine a top-level design for a program intended to test a design for a data validation task. The program repeatedly asks the user for a line of text until one meeting the following requirements is entered. The text must contain only letters and numbers (no spaces or punctuation) and must also have a minimum length of 8.

The top-level design and data table are given below.

1	read in line of text and initialise other data
2	loop while the line of text is unsatisfactory
3	  write out a suitable message
4         readin another line of text
5       loopend
6       write out a suitable message
Type Identifier Description
String Line Text entered from the keyboard
Integer Index Index of individual elements of Line

Refine this design so that it is ready for coding in C++. You should assume that the line of text entered is non-empty so you need not include any data validation. [7]


Question 3C

You are asked to refine a top-level design for a program design for converting a string to a non-negative integer. The program accepts a line of text from the keyboard which should only contain the characters '0' to '9'. It scans the line from left to right, adding the value of the current character to a running total. The final total is written to the screen as a check.

The top-level design and data table are given below.

1	read in line of text and initialise other data
2	loop while there are still characters to process
3	  multiply current total by ten and add value of current character
4       loopend
5       write out final total

(Note: that the algoritm represented by this design is correct.)

Type Identifier Description
String Line Text entered from the keyboard
Integer Index Index of individual elements of Line
Integer Total Running total for output

Refine this design so that it is ready for coding in C++. You should assume that the line of text is entered correctly. You may wish to note that the numeric value represented by a character C in the range '0' to '9' can be calculated as
      C - '0'
as we are using the ASCII code.. [7]

first previous key.gif (977 bytes) next last