Chapter 2Review Qs Answers
2.8-1 At Algonquin, we use a standard header block-comment that contains program Purpose, History, Inputs, Outputs, and the pseudocode Algorithm.
2.8-2 Only income, c3po, and item are valid variable names in C.
2.8-3 Nothing in the program is illegal; you wont get arrested for anything thats written there. The following things arent correct C:
- the scanf() is missing ampersands in front of the two variables: &c, &r
- the constant macro PI is used incorrectly on the left-hand side of an assignment statement.
And note that the program doesnt check that variable r is zero before using it as a denominator. This isnt a C language error; its just bad programming style.
2.8-4 All the names are legal constant macros in C; however, the convention for constant macro names is that they must be all capital letters, so only G and MAX_SPEED are good choices. G is a terrible name for a constant macro; it is so short that we dont know what it represents. So only MAX_SPEED is good.
2.8-5 Data requirements: the length and width of both the yard and the house.
Formulae: area = width * length; grass_area = yard_area - house_area; cutting_time = grass_area / 2
Algorithm (somewhat abbreviated):
Issue a prompt for, a GET statement, and echo what you got, for the four required input variables: yard_width, yard_length, house_width, and house_length
Put out the calculated time:
PUT The time to mow is ((yard_width * yard_length) - (house_width * house_length)) / 2.0
2.8-6 pH ranges from 0 to 7, and one decimal of precision is plenty. So we only need two digits plus the decimal point in the output: printf(The pH value is %3.1f\n, avg_citrus_pH);
2.8-7 Any three of: int, char, float, double (or any others you find in the text
)
2.8-8 Batch mode means getting rid of the prompts and making sure the input is echoed. We can also combine all the input into one statement to make the program shorter (less code is better code):
scanf(%c%c%d%d%d, &c1, &c2, &n, &m, &p);
printf(The two characters are %c and %c , c1, c2);
printf(and the three integers are %d, %d, and %d\n, n, m, p);
2.8-9 Here, we can combine the echoing of the input with the output PUT statement:
PUT Your integer myint doubled, minus 10, is (myint*2) - 10