This preview shows pages 1–4. Sign up to view the full content.

CIS 15AG Answers Chapter 5: Selection – Making Decisions 11. Predict the output: if (a > 0 && b > 0) printf("**"); printf("Done\n"); if (a > 0) printf("*"); if (b > 0) printf("*"); printf("Done\n"); 12. What happens when a semicolon is inadvertently inserted as it is shown below? if (a > 0 && b > 0) ; printf("**"); printf("Done\n"); The above fragment of code can be rewritten as shown below: 13. Predict the output: if (a > 0 && b > 0) printf("**"); printf("Done\n"); if (a > 0 && b > 0) { printf("**"); printf("Done\n"); } Indentation is not sufficient when two or more statements depend on the same condition: braces must be used! 1 a b Output 5 5 **Done 5 -5 Done -5 5 Done -5 -5 Done a b Output 5 5 **Done 5 -5 *Done -5 5 *Done -5 -5 Done a b Output 5 5 **Done 5 -5 **Done -5 5 **Done -5 -5 **Done a b Output 5 5 **Done 5 -5 Done -5 5 Done -5 -5 Done a b Output 5 5 **Done 5 -5 -5 5 -5 -5 Unexpected results! When the condition is true nothing happens! Then the next two statements are executed whether the condition is true or not! if (a > 0 && b > 0) ; printf("**"); printf("Done\n");

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
CIS 15AG Answers Chapter 5: Selection – Making Decisions 14. Predict the output: if (a > b) a = b; printf("%d %d", a, b); if (a > b) { a = b; printf("%d %d", a, b); } 15. Predict the output: if (a > b) { hold = a; a = b; b = hold } printf("%d %d", a, b); 16. Predict the output: sum = 0; if (a > 0) sum += a; if (b > 0) sum += b; if (c > 0) sum += c; if (d > 0) sum += d; printf("%d", sum); 2 a b Output 10 5 5 5 5 5 5 5 5 10 5 10 a b Output 10 5 5 5 5 5 5 10 a b Output 10 5 5 10 5 5 5 5 5 10 5 10 a b c d Output 10 10 10 10 40 -10 10 10 10 30 10 -10 10 10 30 10 10 -10 10 30 10 10 10 –10 30 -10 -10 10 10 20 -10 10 -10 10 20 -10 10 10 –10 20 -10 -10 -10 10 10 -10 -10 10 –10 10 -10 -10 -10 -10 0
CIS 15AG Answers Chapter 5: Selection – Making Decisions 17. Predict the output: count = 0; if (a > 0) count++; if (b > 0) count++; if (c > 0) count++; if (d > 0) count++; printf("%d", count); 18. Write a function that receives four integer values and returns the smallest value, then

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}