# CMSC201 Homework Assignment 2 Solution

## Shapiro's Sections ONLY

3.11

a) if (age <= 65)  /* ; removed */

b) initialize total = 0.

c) While not capitalized
curly braces added around body of loop

d) y is decremented instead of incremented

3.12

a) order b) algorithm c) totaler d) initialization e) sentinel dummy
signal
flag
f) flowchart g) arrow/flowline h) beginning, end i) assignment printf
scanf
j)condition

3.13

The program prints:
1
4
9
16
25
36
49
64
81
100
Total is 385

3.14

a) prints "enter two numbers"
b) p = x+y+z
c) if m is greater than 2v
do this:
else
do this:
d) input s, input r, input t

3.15

a) Input first number
Input second number
Output the sum

b)
Input num1 from kbd
Input num2 from kbd
if num1 > num2
print num1
else if num2 > num1
print num2
else
print that num1 == num2.

c)
Input a number from the kbd
while that number != -1
add it to the running total
input next number
Print the sum.

3.16

a) F. The algorithm is.

b) T

c) F Flowlines indicate the order.

d) F Ususally contain conditionals, not arithmetic.

e) T

3.29

****
++++++++
****
++++++++
****
++++++++
****
++++++++
****
++++++++

3.30

>>>>>>>>>>
<<<<<<<<<<
>>>>>>>>>>
<<<<<<<<<<
>>>>>>>>>>
<<<<<<<<<<
>>>>>>>>>>
<<<<<<<<<<
>>>>>>>>>>
<<<<<<<<<<

3.31

a) x=9, y=11:
*****
\$\$\$\$\$

x=11, y=9:
\$\$\$\$\$

b)
x=9, y=11:
*****

x=11, y=9
#####
\$\$\$\$\$

3.32
a) insert { after 1st if.
insert } at end.

b)
insert { after else
insert } at end

c)
insert { after else
insert } before last line

d)insert { after 1st if and after else
insert } before else and at end

9.4
a)
printf("%-15.8u", (unsigned) 40000);

b)
scanf("%x", &hex);

c)
printf("%+d %d\n", 200, 200);

d)
printf("%#x\n", 100);

e)
scanf("%[^p]", s);

f)
printf("%09.3f\n", 1.234);

g)
scanf("%d%*c%d%*c%d", &hour, &minute, &second);

h)
scanf("\"%[^\"]", s);

i)
scanf("%d:%d:%d", &hour, &minute, &second);

9.5

a) 10000

b) error

c) 1024.988

d)
021
0X11
1.008837e+03

e)
1000000
+1000000

f)
4.45E+02

g)
4.45e+02

h) error

9.6

a) sring must be enclosed by "

b) same as a + print string with %s

c) print string with %s

d) put \ before " inside the conversion string ( "\"%s\"")

e) don't need the [3] when printing

f) should use "

g) forgot to put " around conversion string ("%f")

h) use %c to print chars

i) put & before s[7] in scanf.