CS Logo

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);

b) foo || foo || bar;

c) foo && (bar && foobar);

d) (!foo && !bar);

e) foo && foo && bar;

 

2) (5 points) Which of the following statements are true if "foo" is 4, "bar" is 9 and "foobar" is -1:

a) foo >= bar;

b) foo > bar*foobar;

c) !(foo <= foobar);

d) !(foo != bar);

e) foo == foo;

 

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));

b) (foo && !bar) && (!foo && bar); 

d) ((foo && bar) || (bar && foo)) || !foo;

e) (!(!foo && bar) && (foo || bar)) == foo;

e) (!(foo && !bar) && (foo || bar)) == foo;

 

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).

b) (5 points)

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.