You are on page 1of 6

Documentation on Analysis and Design

PROBLEM 1

Purpose: should read in several lines of nonnegative integer and print out the
maximum value for each line.

Specification: The value -1 indicates the end of input for each line. If -1 is used as a first
input, maximum is undefined.

Variables description:

1. int num_lines stores the number of input lines


2. int line_max stores the value of the maximum element in a current line
3. int entry stores the value of the next input

Pseudo code:

 READ num_lines
 FOR (1 <= i < num_lines+1)
 SET line_max TO -1 AND entry TO 0
 WHILE (entry is not -1)
 READ entry
 IF entry > line_max THEN SET line_max TO entry
 END WHILE
 IF line_max == -1 THEN PRINT maximum is undefined
 ELSE PRINT line_max
 END FOR

Analysis: The purpose of assigning a negative value to line_max is to make it less than all
other elements of the line before entering the while-loop. Once we read the next
“entry”, we check whether it is greater than the current maximum in a line (line_max), if
it is - we assign its value to line_max; otherwise we proceed to the next entry, until the
value of -1 is encountered. Afterwards, we check the value of line_max, if the line is
empty, first entry to be entered is -1, therefore line_max stays unchanged. In this case
we print “maximum undefined”; otherwise we print the value of line_max.
Problem 2

Purpose: The program reads in a positive integer number and sequentially prints out its
odd digits in reverse order.

Specification: If no odd digits, then the program prints nothing. The program should
read in numbers until -1 is entered. The program must not use array or string.

Variables description:

1. int number stores the number entered


2. int digit stores the value of the digit being currently processed

Pseudo code:

 SET number TO 0
 WHILE (number is not -1)
 READ number
 IF (number is not -1) THEN
 WHILE (number is not 0)
 COMPUTE digit = number mod 10
 SET number = (number – digit)/10
 IF digit is odd THEN PRINT digit
 END WHILE
 END WHILE

Analysis: To extract the rightmost digit of the number we should consider number
modulus 10, and write this value into digit, afterwards we delete the last digit. It can be
done as shown in the equation number = (number – digit)/10. After that we only need
to check whether digit is odd, and print it out. Since we consider the digits from right to
left, the digits will be printed in the reverse order.
Problem 3

Purpose: The program reads in two time values in 24 hour format and prints out the
time difference between them expressed in minutes

Specification: A number is in 24-hour format if it has 4 digits (may have leading zeros),
and its first two digits fall between 00 to 23 (hour) while its last two digits fall between 00 to
59 minute. The program runs until a sentinel value of -1 is entered as starting time.

Variables description:

1. int start_time and end_time are the starting and ending times respectively
2. int diff stores the time difference between starting and ending time
3. int hours_1, hours_2 are the first two digits of entered time of start_time and
end_time respectively, practically the number of hours.
4. int minutes_1, minutes_2 are the last two digits of entered time of start_time and
end_time respectively, practically the number of minutes.
5. int time_1, time_2 are the time that has passed since 00:00 of that day till starting
time and ending time respectively, expressed in minutes

Pseudo code:

 SET start_time TO 0
 WHILE (start_time is not -1)
 READ start_time
 IF start_time is not -1 THEN
 READ end_time
 SET minutes_1 = start_time mod 100, minutes_2 = end_time mod 100
 SET hours_1 = (start_time-minutes_1)/100, hours_2 = (end_time – minutes_2)/
100
 SET time_1 = hours_1*60+minutes_1, time_2 = hours_2*60+minutes_2
 IF time_1 < time THEN SET diff = time_2 – time_1
 ELSE THEN diff = 1440 + time_2 – time_1
 PRINT diff
 END WHILE

Analysis: time_1 is the time difference between starting time and 00:00, so
automatically it equals the starting time. One hour consists of 60 minutes, that’s how we
derive the formula time_1 = hours_1*60+minutes_1. Same applies to time_2. So if
time_1 < time_2, then we simply subtract time_1 from time_2. Otherwise, clock needs
to reach 00:00, which takes 1440 – time_1 minutes (because 24hours = 1440 minutes).
After the other time_2 minutes the clock turns end_time, which gives 1440 –
time_1+time_2 minutes in total.
Problem 4

Purpose: The program should contain the method that accepts an array of nonnegative
integers and displays corresponding vertical bar chart on the screen.

Specification: No error checking on the parameter of the method is required. The


program will stop running until a sentinel value of “–1” is entered as user input.

Variables description:

1. int cols is the size of entered array, simultaneously the number of columns in chart
2. int max stores the value of maximum element in the array, the number of rows in
chart
3. int entry is the current entry of array

Pseudo code of method only:

 SET cols TO 0
 WHILE (array[i] > 0)
 SET cols TO cols+1
 END WHILE
 SET max=0
 FOR (0 <= i <cols )
 IF array[i] > max THEN max =array[i]
 END FOR
 SET rows TO max
 SET chart TO new char[rows][cols];
 FOR (0 <=j < cols )
 FOR (0 <=i < rows)
 IF (i < rows - array[j]) THEN SET chart[i][j] TO ' '
 ELSE SET chart[i][j] TO '*'
 END FOR
 END FOR
 FOR (0 <=i < rows )
 FOR (0 <=j < cols+1)
 IF j = 0 THEN PRINT (rows-i+"| " )
 ELSE THEN PRINT (chart[i][j-1] + " ")
 END FOR
 END FOR
 FOR (0 <= j < cols+1)
 IF (j = 0) THEN PRINT(" ")
 ELSE THEN PRINT("---");
 END FOR
 FOR (0 <= j < cols+1)
 IF (j = 0) THEN PRINT(" ")
 ELSE THEN PRINT("["+j+"]");
 END FOR

Analysis: If the array is not initialized from the beginning, we need to estimate the
number of its entries, simultaneously the number of columns in the chart. Afterwards,
we find the maximum element among entries of array. Obviously the number of rows in
the chart is the same as this maximum element. The chart itself is implemented using
the double for-loop, we proceed column by column, and in each column if row’s index is
no less than number of rows minus the height of vertical bar, i.e. i >=rows - array[j]
,then we fill this cell with *. In the following two for-loops we basically design and
implement a frame for the array (column and row numerations), and at the same time
we print the chart. In the main class we only need to initialize the array and call the
method.
Problem 5

Purpose: The program should contain the method that accepts a 2-dimensional array of
integers with 5 rows and uncompresses the number data in each row of the input array by
printing out 0s and 1s on the screen

Specification: the first number in each input row indicates the display output starts with
0 or 1; this leading digit is followed by a sequence of positive numbers in the input to
indicate the number of alternative 0s and 1s in each row. Finally, once you encounter the
sentinel number -1 in an input row, you can ignore the rest of integers in that input row. We
can assume that the row length is always 25

Variables description:

1. int rows stores the number of rows


2. int cols stores the number of columns
3. int entry is the variable that is used to compute whether the next digit 1 or 0

Pseudo code:

 FOR ( 0 <= i < rows)


 SET j TO 1
 WHILE (array[i][j] is not -1)
 COMPUTE entry=(j+1+array[i][0]) mod 2
 FOR (0 <= k < array[i][j])
 PRINT entry
 END FOR
 SET j TO j+1
 END WHILE
 END FOR

Analysis: The uncompressed data is printed row by row. So the first for-loop runs
through each row of array. As the first number in each row of array should not be
uncompressed, we set j to 1, and proceed by increasing j, by using while-loop, until the
sentinel value of -1 is read. Since the j-th number in i-th row of array will be represented
as the series of 1 or 0’s of length array[i][j], we trigger the other for-loop (inside of
while-loop), which runs array[i][j] times and produces the same value (1 or 0). To figure
whether it is 0 or 1 to be printed, the formula entry =( j+1+array[i][0]) mod 2 is used. If
the row starts by 1, i.e. array[i][0] = 1, then first output will be the series of “array[i][1]”
1s, because 1+1+1 mod 2 = 1. Afterwards entry takes 0 value, and so on. So the formula
works indeed. The derivation of this formula is based on the observation that the entry
depends on the value of array[i][0] and on position of number in row, i.e. j. Clearly the
dependence is linear, so as long as array[i] +/- j produces the opposite result, it is
enough to add 1 to the expression.

You might also like