1
|
- Homework 1 due in 4 days (June 16th)
- Variables, mathematical and logical operators, input/output, and the
“if” operator.
- Project 1 Due in 11 days (June 23rd)
- Write a binomial root solver using the quadratic equation.
- Homework 2 will be assigned on Monday and will be due Thursday.
|
2
|
- Whitespace and Input/Output Revisited
- Boolean Operators
- The Programming Cycle
- The if control structure
- Lab: We wrote the parity program
|
3
|
- Homework 1
- The “if … else” control structure
- Nested “if” and “if … else” statements
- Project 1
- LAB
|
4
|
- Problem 1, Example 1
- Assume int x = 2; and int y = 3;
- cout << x << “+”
<< y;
- The answer should look like this:
|
5
|
- Problem 1, Example 2
- Assume int x = 2; and int y = 3;
- cout << x + y;
- The answer should look like this:
|
6
|
- Problem 1, Example 3
- Assume int x = 2; and int y = 3;
- cout << x << +
<< y;
- The answer should look like this:
|
7
|
- Problem 2, Example 1
- Is the following statement a valid C++ representation of the equation y
= ax3 + 7.
- y = a*x* (x * x ) + 7;
- The answer should look like this:
|
8
|
- Problem 2, Example 1
- Is the following statement a valid C++ representation of the equation y
= ax3 + 7.
- y = axxx + 7;
- The answer should look like this:
|
9
|
- Problem 3, Example 1
- x = 7 + 2/8 * 10 * 6 * (6 + 7* (3 + 4));
- The answer should look like this:
|
10
|
- Problem 3, Example 2
- x = 7 + 10 * 2/8 * 6 * (6 + 7 * ( 3 + 4 )));
- The answer should look like this:
|
11
|
- Problem 3, Example 3
- x = 7 + 10 * 2/8 ** 6 * (6 + 7 * ( 3 + 4 )));
- The answer should look like this:
|
12
|
- Problem 4, Example 1
- Write a single C++ statement to do the following:
- Declare two floating point variables and call them foo and bar.
- The answer should look something like one of the following:
- float foo, bar;
- double foo = 0.0, bar = 0.0;
|
13
|
- Problem 4, Example 2
- Write a single C++ statement to do the following:
- Print the values of foo and bar;
- The answer should look something like one of the following:
- cout << foo << “ ” << bar;
- cout << foo << “,” << bar << endl;
|
14
|
- Syntax:
- if ( boolean_expression )
- {
- statements 1…
- }
- else
- {
- statements 2…
- }
- statements 3…
|
15
|
- int x = 10;
- if ( x % 2 == 0 )
- {
- cout << “x is even”;
- }
- else
- {
- cout << “x is odd”;
- }
- cout << “, done” << endl;
|
16
|
- int x = 9;
- if ( x % 2 == 0 )
- {
- cout << “x is even”;
- }
- else
- {
- cout << “x is odd”;
- }
- cout << “, done” << endl;
|
17
|
- int foo = 10, bar = 15;
- if ( (foo < bar) && ((bar % 2) == 0) )
- {
- foo = 2*bar;
- }
- else
- {
- bar = 2*foo;
- }
- cout << foo << “, ” << bar << endl;
|
18
|
- int foo = 10, bar = 12;
- if ( (foo < bar) && ((bar % 2) == 0) )
- {
- foo = 2*bar;
- }
- else
- {
- bar = 2*foo;
- }
- cout << foo << “, ” << bar << endl;
|
19
|
- Syntax:
- if ( bool_expr_1 )
- {
- if ( bool_expr_2 )
- {
- statements …
- }
- }
|
20
|
- Syntax:
- if ( bool_expr_1 )
- {
- statements…
- if ( bool_expr_2 )
- {
- statements…
- }
- statements…
- }
- Not the same as bool_expr_1 && bool_expr_2
|
21
|
- int x = 2, y = 5;
- if ( x < y)
- {
- cout << “x is smaller than y”;
- if ( (x % 2) == 0)
- {
- cout << “ and even”;
- }
- }
- cout << “, done” << endl;
|
22
|
- int x = 3, y = 5;
- if ( x < y)
- {
- cout << “x is smaller than y”;
- if ((x % 2) == 0)
- {
- cout << “ and even”;
- }
- }
- cout << “, done” << endl;
|
23
|
- Syntax:
- if ( bool_expr_1 )
- {
- statements…
- }
- else if ( bool_expr_2 )
- {
- statements …
- }
- else
- {
- statements …
- }
|
24
|
- int grade = 82;
- if ( grade > 90 )
- {
- cout << “A”;
- }
- else if ( grade > 80 )
- {
- cout << “B”;
- }
- else if ( grade > 70 )
- {
- cout << “C”;
- }
- else
- {
- cout << “D or F”;
- }
|
25
|
- int grade = 10;
- if ( grade > 90 )
- {
- cout << “A”;
- }
- else if ( grade > 80 )
- {
- cout << “B”;
- }
- else if ( grade > 70 )
- {
- cout << “C”;
- }
- else
- {
- cout << “D or F”;
- }
|
26
|
- It is legal to leave out braces if you only execute one statement.
- if( x > y)
- cout << “x greater than y”;
- if( x > y )
- cout << “x greater than y”;
- else if
- cout << “x less than y”;
|
27
|
|
28
|
|
29
|
- Write a root solver using the quadratic equation.
- There are four cases. The cases are determined by the value of the
discriminant (b2 - 4ac).
- You are going to use multiple nested “if” or “if … else” control
structures to execute the appropriate code based on the value of the
discriminant.
|
30
|
|
31
|
|
32
|
- Use if … else… to write a program which takes two floating point
numbers and prints whether the second number is a square root of the
first one.
- If the first number entered is negative print “Error: enter a positive
number.”
- You may use only one return statement in your program and no exit()
statements.
|