COSC450/550 HW 6

1. Rewrite the following pseudocode segment using a loop structure in the specified language:
k = (j + 13) / 27
loop:
if k 10 then goto out
k = k + 1
i = 3 * k – 1
goto loop
out: . . .
a. C, C++, Java or C#
b. Python
c. Ruby
Assume all variables are integer type. Discuss which language, for this code, has the best writability, the best readability, and the best combination of the two.
2. Rewrite the following code segment using a multiple-selection statement in the following language:
if ((k == 1) || (k == 2)) j = 2 * k - 1
if ((k == 3) || (k == 5)) j = 3 * k + 1
if (k == 4) j = 4 * k - 1
if ((k == 6) || (k == 7) || (k == 8)) j = k - 2
a. C, C++, Java or C#
b. Python
c. Ruby
Assume all variables are integer type. Discuss the relative merits of the use of these languages for this particular code.
COSC450/550 HW 6
3. In a letter to the editor of CACM, Rubin (1987) uses the following code segment as evidence that the readability of some code with gotos is better than the equivalent code without gotos. This code finds the first row of an n by n integer matrix named x that has nothing but zero values.
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++)
if (x [i] [j] != 0)
goto reject;
println (‘ First all-zero row is : ‘, i);
break;
reject:
}
Rewrite this code without gotos in one of the following languages: C, C++, Java, or C#. Compare the readability of your code to that of the example code.
Powered by