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.
To gain experience in
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() { float radius = cwin.get_float("Radius: "); if (radius > 5.0 ) { Circle c1 = Circle(Point(0,0), radius - 5.0); cwin << c1; } Circle c2 = Circle(Point(0,0), radius); cwin << c2; return EXIT_SUCCESS; }
The relational operators in C++ are == != < > <= and >=
#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; }
/* paste program here */
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.
According to your program, how much will it cost to buy
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.
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() { float radius1 = cwin.get_float("Radius: "); Circle circle1(Point(0,0), radius1); float radius2 = cwin.get_float("Radius: "); Circle circle2(Point(0,4), radius2); cwin << circle1 << circle2; /* Your work goes here */ }
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_address = cwin.get_string("From Address: "); string from_city = cwin.get_string("From City: "); string from_state = cwin.get_string("From State/Province: "); string to_address = cwin.get_string("To Address: "); 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"; }
According to the following program, what color is the resulting mixture under the following inputs?
#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; }
Don't forget to send your answers when you're finished.