rec7.pptx

advertisement
Programming
exercises
Chapter 8 : #1
• Rewrite the following pseudocode segment using a loop
structure in the specified languages:
k = ( j + 13 ) / 27
loop:
if k > 10 then goto out
k=k+1
i=3*k-1
goto loop
out : …
1) Java?
2) Python?
k = ( j + 13 ) / 27
loop:
if k > 10 then goto out
k=k+1
i=3*k-1
goto loop
out : …
Java
Loops ( for, while, do...while)
for (initialization; termination; increment)
{
statement(s)
}
k = ( j + 13 ) / 27
loop:
if k > 10 then goto out
k=k+1
i=3*k-1
goto loop
out : …
Java
Loops ( for, while, do...while)
for (initialization; termination; increment)
{
statement(s)
}
k = ( j + 13 ) / 27
loop:
if k > 10 then goto out
k=k+1
i=3*k-1
goto loop
out : …
Java
Loops ( for, while, do...while)
for (initialization; termination; increment)
{
statement(s)
}
k = ( j + 13 ) / 27
loop:
if k > 10 then goto out
k=k+1
i=3*k-1
goto loop
out : …
Java
Loops ( for, while, do...while)
for (initialization; termination; increment)
{
statement(s)
}
k = ( j + 13 ) / 27
loop:
if k > 10 then goto out
k=k+1
i=3*k-1
goto loop
out : …
Java
Increment by 1 first
• Solution:
for (k = (j + 13) / 27; k <= 10; i = 3 * (++k) - 1)
k = ( j + 13 ) / 27
loop:
if k > 10 then goto out
k=k+1
i=3*k-1
goto loop
out : …
Loops ( for, while)
for iterating_var in sequence:
statements(s)
Python
k = ( j + 13 ) / 27
loop:
if k > 10 then goto out
k=k+1
i=3*k-1
goto loop
out : …
Loops ( for, while)
for iterating_var in sequence:
statements(s)
Python
k = ( j + 13 ) / 27
loop:
if k > 10 then goto out
k=k+1
i=3*k-1
goto loop
out : …
Loops ( for, while)
for iterating_var in sequence:
statements(s)
Python
k = ( j + 13 ) / 27
loop:
if k > 10 then goto out
k=k+1
i=3*k-1
goto loop
out : …
Loops ( for, while)
for iterating_var in sequence:
statements(s)
Python
k = ( j + 13 ) / 27
loop:
if k > 10 then goto out
k=k+1
i=3*k-1
goto loop
out : …
• Solution:
for k in xrange ( ( j + 13 ) / 27, 10 ) :
i=3*(k+1)-1
Python
Chapter 8 : #3
• Rewrite the following code segment using a multiple-selection
in the following languages:
1) Java?
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
2) Python?
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
Multiple-selection ( switch, if-then-else)
switch(expression){
case value :
//Statements
break; //optional
case value :
//Statements
break; //optional
//You can have any number of case statements.
default : //Optional
//Statements
}
Java
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
Multiple-selection ( switch, if-then-else)
switch(expression){
case value :
//Statements
break; //optional
case value :
//Statements
break; //optional
//You can have any number of case statements.
default : //Optional
//Statements
}
Java
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
Multiple-selection ( switch, if-then-else)
switch(expression){
case value :
//Statements
break; //optional
case value :
//Statements
break; //optional
//You can have any number of case statements.
default : //Optional
//Statements
}
Java
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
• Solution:
Java
switch (k) {
case 1: case 2:
j = 2 * k - 1;
break;
case 3: case 5:
j = 3 * k + 1;
break;
case 4:
j = 4 * k - 1;
break;
case 6: case 7: case 8:
j = k - 2;
break;
default:
printf("Error in switch, k =%d\n", k); }
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
Multiple-selection ( if statements)
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
Python
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
Multiple-selection ( if statements)
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
Python
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
Multiple-selection ( if statements)
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
Python
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
• Solution:
Python
if k == 1 or k == 2 :
j=2*k-1
elif k == 3 or k == 5:
j=3*k+1
elif k == 4:
j=4*k–1
elif k == 6 or k == 7 or k == 8:
j=k-1
else:
print (‘error’)
Q4(Pg 384)
Consider the following C program segment. Rewrite the switch
case using no gotos or breaks.
Solution
Anything missing in this
code?
Updating the key after
every condition branch.
Q5(Pg 384)
The following code segment was used as an evidence that
readability of some code with gotos is better than equivalent
code without gotos.
This code finds the first row of an n X n integer matrix named x
that has nothing but zero
Solution
Rewrite the code without gotos in C.
Compare the readability of the two codes.
Download