Computing Concepts with C++ Essentials Laboratory Notebook Chapter 4 - Decisions

Cay S. Horstmann Geof Pawlicki

Once this form has been customized for your institution, you can use this button to send your lab work. Be sure to read the instructions before starting your work.

Lab Objectives

To gain experience in

• implementing simple decisions
• writing expressions with relational operators
• implementing repeated and nested branches
• using Boolean variables
• using Boolean operations

R1. The if statement

In C++, decisions to do or not to do something can be made by using the statement if (test expression). The test expression must be an expression that can be evaluated as either true or false. If it evaluates to true, a succeeding statement or group of statements enclosed in { . . . }, called a block statement will be executed.

How many circles will be drawn when the following code is executed with radius = 7 ?

```#define CCC_WIN
#include "ccc.h"

int main()

{   Circle c1 = Circle(Point(0,0), radius - 5.0);
cwin << c1;
}

cwin << c2;
return EXIT_SUCCESS;
}
```

R2. Relations and Relational Operators

The relational operators in C++ are == != < > <= and >=

• Formulate the following conditions in C++:
 x is positive x is zero or negative x is at least 10 x is less than 10 x and y are both zero
• Formulate the following conditions for the employee object e.
 The last name of e starts with the letter H The salary of e is at least \$50,000
• Formulate the following conditions on the variable harrys_birthday of type Time.
 Harry is at least 18 years old. Harry was born in January. Harry was born in a leap year (assume his birthday was between 1901 and 2099).

P1. Input validation

• Build and run the following program. Describe what happens when the two points have the same x coordinate?

```#include "ccc.h"

main()
{  float slope;

float xcoord, ycoord;
Point p1, p2;

cout << "Input x coordinate of the first point" << "\n";
cin >> xcoord;
cout << "Input y coordinate of the first point" << "\n";
cin >> ycoord;

p1 = Point(xcoord, ycoord);

cout << "Input x coordinate of the second point" << "\n";
cin >> xcoord;
cout << "Input y coordinate of the second point" << "\n";
cin >> ycoord;

p2 = Point(xcoord, ycoord);

slope = (p2.get_y() - p1.get_y()) / (p2.get_x() - p1.get_x() );

cout << "The slope of the line between Points 1 and 2 is " << slope << "\n";
return EXIT_SUCCESS;
}
```
• Correct and rebuild the program to dissallow a vertical line (denominator = 0).

• What are the results when point1=(4,2) and point2 = (4,2) ?

• What are the results when when point1=(4,2.5) and point2 = (3,1.5) ?

P2. The if/else statement

In the previous example, your program probably responded to user input by ignoring cases that would result in a divide by zero. Instead, you can use the if/else format to explicitly specify the action to be taken.

```if (test_expression)
/* do something ... */
else
/* do something different ... */
```

A wholesaler advertises a volume discount on blank CD ROMs of 10 cents/disk for each thousand purchased above 10000. (The regular price is \$950.00 per 1000, or 95 cents per disk). Write a program that receives the number of blank CD ROMs in an order, then calculates and displays the total cost.

 2000 disks? 15000 disks? 180000 disks?

P3. Multiple alternatives

The if/else decision in the preceding example can be extended to select from more than twp possible outcomes. The if ... else if . . else syntax is used to select exactly one of several possible actions.

```if (test expression 1)
/* do something ... */
else if (test expression 2)
/* do something different ... */
else
/* do something generic ...   */
```

Write a program to compute the cost of a purchase of blank CD ROMs as above, but this time, have there be no discount on the first 10000, 5 cents/disk on the second 10,000, 10 cents/disk over 20,000 and 20 cents/disk on any over 50,000.

P4. Nested Branches

If there are multiple conditions, it can happen that a conditionally executed block contains further decisions. Here is an example.

Extend the following code to test whether two circles - each having a fixed center point and a user-defined radius - are disjoint, overlapping or mutually contained.

```#define CCC_WIN
#include "ccc.h"

int main()

cwin << circle1 << circle2;

/*
*/

}
```

P5. Logical Operations

C++ has three logical operations, and, or and not. These are traditionally written as &&, || and !. Both variants are legal under ANSI C++. We find the former easier to read, but many C++ programmer use the latter out of habit. Feel free to use either form in your answers.

Using these operations, express the following:

x and y are both positive or neither of them is positive.

The following program determines if a particular package is elligible for Unidentified Delivery Service's special Northwest Urban Rate Discount. Simplify the nested branches by using Logical Operations and, or, not wherever possible.

```int main()
string from_city = cwin.get_string("From City: ");
string from_state = cwin.get_string("From State/Province: ");

string to_city = cwin.get_string("To City: ");
string to_state = cwin.get_string("To State/Province: ");

if (from_state == "OR")
{   if (from_address.substr(0,11) != "Rural Route")
first = true;
else
first = false;
}
else if(from_state == "WA")
{   if (from_address.substr(0,11) != "Rural Route")
first = true;
else
first = false;
}
else if (from_state == "BC")
{   if (from_address.substr(0,11) != "Rural Route")
first = true;
else
first = false;
}
else
first = false;

if (to_state == "OR")
{   if (to_address.substr(0,11) != "Rural Route")
second = true;
else
second = false;
}
else if (to_state== "WA")
{   if (to_address.substr(0,11) != "Rural Route")
second = true;
else
second = false;
}
else if to_state == "BC")
{   if (to_address.substr(0,11) != "Rural Route")
second = true;
else
second = false;
}
else
second = false;

if (first and second)
cout << "Package eligible for Northwest Urban Rate Discount!" << "\n";
else
cout << "Package eligible for Standard Rate" << "\n";
}
```

R3. Using Boolean variables

According to the following program, what color is the resulting mixture under the following inputs?

 Y N Y Y Y N N N N
```#include "ccc.h"

int main()
{  string mixture;
bool red, green, blue;
string string_bool;

cout << "Include red in mixture? (Y/N) " << "\n";
cin >> string_bool;
if (uppercase(string_bool) == "Y")
red = true;
cout << "Include green in mixture? (Y/N) " << "\n";
cin >> string_bool;
if (uppercase(string_bool) == "Y")
green = true;
cout << "Include blue in mixture? (Y/N) " << "\n";
cin >> string_bool;
if (uppercase(string_bool) == "Y")
blue = true;

if (not blue and not green)
mixture = "RED";
else if (not red and not blue)
mixture = "GREEN";
else if (not red and not green)
mixture = "BLUE";
else if (red)
{  if (green or blue)
{  if (green and blue)
mixture = "BLACK";
else if (green)
mixture = "YELLOW";
else
mixture = "PURPLE";
}
}
else
{  if (blue and green)
mixture = "CYAN";
else
mixture = "WHITE";
}
cout << "Your mixture is " << mixture << "\n";

return EXIT_SUCCESS;
}
```