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

#include <iostream> // For I/O

using namespace std;

int main()
{


int score = -1; // Initialize to an invalid value

cout << "Enter a score: " << endl;
cin >> score;

// Check that the score is valid, if it is print the letter grade
if ( score <= 0 )
{
  cout << "Error: the grade entered must be greater than zero." << endl;
}
else if ( score > 105 )
{
  cout << "Error: the maximum grade is 105." << endl;
}
else if ( 100 <= score && score <= 105 )
{
  cout << "A+" << endl;
}
else if ( 93 <= score && score < 100 )
{
  cout << "A" << endl;
}
else if ( 90 <= score && score < 93 )
{
  cout << "A-" << endl;
}
else if ( 87 <= score && score < 90 )
{
  cout << "B+" << endl;
}
  else if ( 83 <= score && score < 87 )
{
  cout << "B" << endl;
}
  else if ( 80 <= score && score < 83 )
{
  cout << "B-" << endl;
}
  else if ( 77 <= score && score < 80 )
{
  cout << "C+" << endl;
}
  else if ( 73 <= score && score < 77 )
{
  cout << "C" << endl;
}
  else if ( 70 <= score && score < 73 )
{
  cout << "C+" << endl;
}
else if ( 67 <= score && score < 70 )
{
  cout << "D+" << endl;
}
else if ( 63 <= score && score < 67 )
{
  cout << "D" << endl;
}
else if ( 60 <= score && score < 63 )
{
  cout << "D-" << endl;
}
else if ( 0 <= score && score < 60 )
{
  cout << "F" << endl;
}

return 0;
}

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.


// Program to convert raw percentage scores into letter grades

#include <iostream> // For I/O

using namespace std;

int main()
{
int score = -1;

cout << "Enter a score: " << endl;
cin >> score;

while ( score != -1 )
{

// Check that the score is valid, if it is print the letter grade
if ( score <= 0 )
{
cout << "Error: the grade entered must be greater than zero." << endl;
}
else if ( score > 105 )
{
cout << "Error: the maximum grade is 105." << endl;
}
else if ( 100 <= score && score <= 105 )
{
cout << "A+" << endl;
}
else if ( 93 <= score && score < 100 )
{
cout << "A" << endl;
}
else if ( 90 <= score && score < 93 )
{
cout << "A-" << endl;
}
else if ( 87 <= score && score < 90 )
{
cout << "B+" << endl;
}
else if ( 83 <= score && score < 87 )
{
cout << "B" << endl;
}
else if ( 80 <= score && score < 83 )
{
cout << "B-" << endl;
}
else if ( 77 <= score && score < 80 )
{
cout << "C+" << endl;
}
else if ( 73 <= score && score < 77 )
{
cout << "C" << endl;
}
else if ( 70 <= score && score < 73 )
{
cout << "C+" << endl;
}
else if ( 67 <= score && score < 70 )
{
cout << "D+" << endl;
}
else if ( 63 <= score && score < 67 )
{
cout << "D" << endl;
}
else if ( 60 <= score && score < 63 )
{
cout << "D-" << endl;
}
else if ( 0 <= score && score < 60 )
{
cout << "F" << endl;
}

cout << "Enter a score: " << endl;
cin >> score;
}

return 0;
}