Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This lab introduces logical comparisons and Boolean operators. These operators are used to create the conditions that govern whether a loop will be repeated, whether

This lab introduces logical comparisons and Boolean operators. These operators are used to create the conditions that govern whether a loop will be repeated, whether the code within an if statement will be executed, and which branch of an if-else statement will be executed.
Comparison operators compare two values and evaluate to false (actually the value 0) if the comparison is false, and evaluate to true (the value 1) if the comparison is true. For example, the expression 7<8 has the value 1, whereas the expression 8<7 has the value 0.
The common comparison operators are:
<(less than) Example: A < B is true if A is less than B and is false otherwise
>(greater than) Example: A > B is true if A is greater than B and is false otherwise
<=(less than or equal to) Example: A <= B is true if A is less than or equal to B and is false otherwise
>=(greater than or equal to) Example: A >= B is true if A is equal to or greater than B and is false otherwise
==(equal)(note the double equal signs) Example: A == B is true if A is equal to B and is false otherwise
!=(not equal) Example: A != B is true if A is not equal to B and is false otherwise
Boolean operators (named after the mathematician and logician George Boole) make it possible to create more complex logical tests. If you want to determine if two things, A and B, are true, or if either of two things, A or B, are true, then you need to use Boolean operators. For example, the expression:
(x < y) && (x < z)
is true if the value held by variable x is less than the value held by variable y and (the symbol && means and) the value held by variable x is less than the value held by variable z.
The common Boolean operators are:
||(OR) example: A || B is true if A is true or B is true (or both are true) and is false otherwise. (The vertical line is usually above the \ key on the keyboard.)
&& (AND) example: A && B is true only if A is true and B is true, otherwise it is false.
!(NOT) example: !A is true if A is false and is false otherwise
This lab consists of three parts.
Part I: Hypothesize whether each of the 20 Boolean statements and mathematical expressions given below is true or false. Write your hypotheses in a file named hypotheses.txt that can be uploaded to Canvas. You will not be graded on whether you get these right or wrong, but you do need to turn them in.
Consider which expressions were the most difficult to understand. Create 3 more of your own expressions to try to clarify how Boolean and Mathematical expressions are evaluated in C/C++. Add these expressions along with your hypotheses about their values to the list given below (for a total of 23 expressions).
Part II: Write a program to determine whether the Boolean statements and mathematical expressions given below, plus the three that you created, are true or false. This program will be used to test your hypotheses. Remember that C++(and C) use a 0 for false and 1 for true. Therefore, to determine the validity of a Boolean statement you only need to embed it in a cout statement. For example, the command:
cout <<(7<9)||(4<0)<< endl;
will print 0 if the statement is false and 1 if the statement is true.
To determine if a mathematical expression is treated as true or false an if-else statement can be used. For example, to determine whether the expression: 5*6-30 is treated as true or false you can use the code:
if (5*6-30){
cout << "It's true
";
} else {
cout << "It's false
";
}
Make sure you can turn in the results from your program as well as the program itself. Remember to use the script command to create an output file. The command script lab2output.txt will create a file called lab2output.txt that you can submit.
Part III: Use the results of your program to mark in your original file (containing your hypotheses) which hypotheses were correct and which ones were incorrect.
Boolean and Mathematical Expressions to Test:
0
-7
-1+-2
-7+7
7<8
10>10
(7<10)||(7>10)
(7<10) && (7>10)
-5>=5
(7>10)||(-5==5)
!(6==7)
(10!=11) && (7>8)
(7==6)||(7==7)||(7==8)
(6<(8 && 9))[This one is tricky.]
(1==(6<8))[Also tricky.]
(0==(0||1))[Still more trickiness.]
6<(5+2)
6+(5==5)
(6<7)+(7>=5)
(0<7)-(0<7)
Turn in: Your file of hypotheses in hypotheses.txt regarding the expressions, with the correct answers as determined by your program. The source code of the program (or programs) that you used to test your hypotheses and the output of the program(s). All of these items should be submitted to your instructor via Canvas.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Put Your Data To Work 52 Tips And Techniques For Effectively Managing Your Database

Authors: Wes Trochlil

1st Edition

0880343079, 978-0880343077

More Books

Students also viewed these Databases questions