CSCI 261 Programming Concepts (C++)

Winter/Spring 2012

Homework 5: Boolean Expressions

Concepts

Focus on two main programming concepts in this assignment: boolean expressions and how cin and cout interact with bool variables.

Teaching the Machine about Things That are True or False

Look outside. Is it a nice day? Aha, your mind has just evaluated numerous conditions that you subcousciously use to determine the answer to that question. Is it sunny? Is the temperature mild? Is the wind still? And do you consider sunny, mild, non-windy days to be "nice?" Analyze yourself for a moment in the context of the question, "Is it a nice day?" Notice how you have specific conditions you use to make more complex decisions.

You can teach the machine how to make decisions like this, but first, we have to learn about how the machine considers conditions to be true or false. We do this with the bool datatype and boolean expressions. Let's take a look.

bool sunny = true;
bool mild = true;
bool windy = false;

We've just told the computer about some truths and falsehoods about the world. If we want the computer to consider sunny, mild, wind-less days to be "nice," we might implement something like this:

bool niceDay = sunny && mild && !windy;

This is like telling the computer, "Hey computer, it's a nice day if it is sunny AND mild AND NOT windy." What if you wanted the machine to act like someone who prefers cold, dreary days? You might do something like this:

bool sunny = false;
bool cold = true;
bool rainy = true;
bool snowing = false;
bool niceDay = !sunny && cold && (rainy || snowing);

Now the computer thinks "It's a nice day when it is not sunny AND it is cold AND it is either rainy OR snowing." Notice the use of parentheses in that last expression. Without those parentheses, the meaning changes:

bool niceDay = !sunny && cold && rainy || snowing;

Now the computer thinks, "It's a nice day when it is not sunny AND it is cold AND rainy. Or, if it is snowing then it's a nice day." Be sure you understand boolean operator precedence and how boolean operator precedence works within the context of other mathematical operators like +, * and the like.

The Secret Love Affair betweeen bools and ints

You should have read about how you can use the keywords true and false to represent truth and falsehood. These symbols true and false are reserved words in C++ that allow you to express some semantics. Realize that internally, the machine represents true and false as 1 and 0. Realize that in C++, actually any non-zero value will evaluate to true in a boolean context. So 0 is false, and everything else is true.

Why are we telling you this? Because of the fact that there are other datatypes that can represent 0 and 1, you should be aware that ints can also be used in a boolean context (so can doubles and chars, but don't worry about that for now). Let's take a look.

int sunny = 1;
bool mild = true;
bool niceDay = sunny && mild;

What does 1 && true yield? It yields true. Do you see the secret relationship between ints and bools? How scandalous!

How cin and cout read and print bools

In this assignment, you are going to witness how cin is finicky when reading what you type and assigning that to a bool variable. You will notice that whenever you use:

bool happy;
cin >> happy;

The machine will expect that you type either 0 or a 1. What happens if you don't type a 0 or a 1, will the world suddenly end? Once your program is working, try it out.

As for cout, note that the machine won't automagically print a boolean variable as the words "true" or "false"; it will print either 0 or 1.

Instructions

Open the solution file in apps\homework\05_booleanExpressions.

Hit Ctrl-F5 to "build and run" the provided program. It should compile and run without error -- but it does nothing.

Now, open the file main.cpp and add your name properly to the comment header. For this assignment, you must write a program that exhibits the following behavior on the console (user input is in orange):

Hello, I'm so lucky that you have programmed me.
Enter values for three boolean variables (use 0 or 1).
a: 0
b: 1
c: 0
The expression !(a && b && c) && !(a || b || c) when a=0 b=1 c=0 yields 0

Note the requirements below. Be sure to start simple. Can you print a greeting to the screen? Compile and run. Can you prompt for and capture one value? Compile and run. Can you capture three values? Compile and run. Can you calculate the provided boolean expression? Compile and run. Can you print the final output? Compile and run.

Requirements and Rubric

Your program must prompt for only three values and assign those values to boolean variables a, b and c.

Your program must evaluate the expression !(a && b && c) && !(a || b || c) and print the result as shown above in blue. How do you know if your expression is correct? That's part of the challenge of this assignment -- look at the expression and think to yourself, "For what values a, b and c would the expression yield true? How about false?"

Your program must print the line

The expression !(a && b && c) && !(a || b || c) when a=A b=B c=C yields X

Where A, B, C and X are either 0 or 1 (depending on input) and X is either 0 or 1.

Anything else your program "says" is up to you.

This work is worth 24 points.

Requirement Points Notes
Correct submission of src directory as a .zip file. 1
Place your name in the comment header in main.cpp 1
Correct datatypes used 6 bools!
Correctly prompts for only three inputs 6
Correctly computes the boolean expression 8
Correctly prints the required output 2

Concepts Exercised: compilation, program design, making decisions, declaring facts, I/O