Homework 2 This homework is intended to gauge your understanding of boolean operations, nested if... else... statements, and the "while" loop. 1) (5 points) Which of the following statements are true if "foo" is true, "bar" is false and "foobar" is true: a) !(foo || bar); false b) foo || foo || bar; true c) foo && (bar && foobar); false d) (!foo && !bar); false e) foo && foo && bar; false
2) (5 points) Which of the following statements are true if "foo" is 4, "bar" is 9 and "foobar" is -1: a) foo >= bar; false b) foo > bar*foobar; true c) !(foo <= foobar); true d) !(foo != bar); false e) foo == foo; true
3) (15 points) For what truth values of foo and bar are the following statements true: (hint: there are only four possible truth value combinations for two variables) Example 1: ((foo && !bar) || (bar && !foo)) && foo; Answer: This statement is true only when foo is true and bar is false. Example 2: (foo || !bar) && (foo || bar) == (foo || (!bar && bar)); Answer: The statement is true for all values of foo and bar. (The expressions on either side of the equals operator are both true when foo is true and false otherwise so the equals operator always returns true). a) (foo || bar) || (!foo || ( foo && !bar)); True for all values of foo and bar. b) (foo && !bar) && (!foo && bar); False for all values of foo and bar. d) ((foo && bar) || (bar && foo)) || !foo; False when foo is true and bar is false, true otherwise. e) (!(!foo && bar) && (foo || bar)) == foo; True for all values of foo and bar. e) (!(foo && !bar) && (foo || bar)) == foo; True when foo and bar have the same truth value, false otherwise. Equivalent to foo == bar.
4) a) (20 points) Write a program that asks the user to input an integer and then reads in the integer. If the integer is less than zero your program should print "Error: the grade entered must be greater than zero." If the integer entered is greater than 105 print "Error: the maximum grade is 105." If the grade is greater than 0 and 105 or less print the corresponding letter grade as described in the grade translation section of the syllabus. (This question is intended to test your use of nested if and if ... else... statements so your program must contain only one return statement and no exit() statements). // Program to
convert raw percentage scores into letter grades
b) (5 points) (Extra Credit) Using a "while" or "do...while" loop keep asking the user to enter more grades until they enter the character -1 Once the user enters -1 quit the program.
|