Download C++_Lab4

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Principia Mathematica wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
Faculty of Engineering
Computer Engineering Department
Islamic University of Gaza
2011
C++ Programming Language Lab # 4
Selections
C++ Programming Language Lab # 4
Selections
Objective:
To be familiar with C++ Programming Language.
Introduction:
Comparison Operators:
Operator Name
Example
Result
<
less than
1 < 2
true
<=
less than or equal to
1 <= 2
true
>
greater than
1 > 2
false
>=
greater than or equal to
1 >= 2
false
==
equal to
1 == 2
false
!=
not equal to
1 != 2
true
One-way if Statements:
if (Boolean Expression)
{
statement(s);
}
Example:
if (x != 0)
{
y = 10/x ;
cout << "The value of y= 10/x = " << y<< endl;
}
2
Exercise 1: Compare two numbers:
Notes:
 If there is one single statement I there Is no need for the brackets
Outer parentheses required
Braces can be omitted if the block contains a single
statement
if ((i > 0) && (i < 10))
{
cout << "i is an " <<
"integer between 0 and 10";
}
Equivalent
if ((i > 0) && (i < 10))
cout << "i is an " <<
"integer between 0 and 10";
(a)
(b)
 Adding a semicolon at the end of an if clause is a common mistake
Empty Body
Logic Error
if (radius >= 0);
{
area = radius * radius * PI;
cout << "The area "
<< " is " << area;
}
(a)
The if...else Statement:
if (booleanExpression)
{
statement(s)-for-the-true-case;
}
else {
statement(s)-for-the-false-case;
}
3
Equivalent
if (radius >= 0) { };
{
area = radius * radius * PI;
cout << "The area "
<< " is " << area;
}
(b)
true
Statement(s) for the true case
Boolean
Expression
false
Statement(s) for the false case
Exercise 2:
The LeVan Car Rental company charges $0.25/mile if the total mileage does not exceed 100. If
the total mileage is over 100, the company charges $0.25/mile for the first 100 miles, then it
charges $0.15/mile for any additional mileage over 100.
Write a program so that if the clerk enters the number of miles, the program would display the
total price owed.
4
Nested if Statements:
if (i > k)
{
if (j > k)
cout << "i and j are greater than
k";
}
else
cout << "i is less than or equal to k";
Note:
The else clause matches the most recent if clause in the same block.
int i = 1;
int j = 2;
int k = 3;
Equivalent
if (i > j)
if (i > k)
cout << "A";
else
cout << "B";
This is better
with correct
indentation
(a)
Multiple Alternative if Statements:
Exercise 3:Grading code :
5
int i = 1;
int j = 2;
int k = 3;
if (i > j)
if (i > k)
cout << "A";
else
cout << "B";
(b)
Logical Boolean operators:
Operator
Name
!
not
&&
and
||
or
Truth Tables (!)
p
!p
true
false
!(1 > 2) is true, because (1 > 2) is false.
false
true
!(1 > 0) is false, because (1 > 0) is true.
Example
Truth Table (&&)
p1
p2
p1 && p2
false
false
false
false
true
false
true
false
false
true
true
true
Example
(3 > 2) && (5 >= 5) is true, because (3 >
2) and (5 >= 5) are both true.
(3 > 2) && (5 > 5) is false, because (5 >
5) is false.
Truth Table ( ||)
p1
p2
p1 || p2
false
false
false
false
true
true
true
false
true
true
true
true
Example
(2 > 3) || (5 > 5) is false, because (2 > 3)
and (5 > 5) are both false.
(3 > 2) || (5 > 5) is true, because (3 > 2)
is true.
The (&&) is referred to as the conditional or short-circuit AND operator, and ( ||) is referred to
as the conditional or short-circuit OR operator.
6
Exercise 4: A year is a leap year?
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
Note:
if (number % 2 == 0)
even = true;
else
even = false;
Equivalent
bool even
= number % 2 == 0;
This is better
(b)
(a)
Equivalent
if (even == true)
cout <<"It is even.";
(a)
This is better
if (radius >= 0)
area = radius * radius * PI;
cout << "The area "
<< " is " << area;
(a) Wrong
7
if (even)
cout << "It is even.";
(b)
if (radius >= 0)
{
area = radius * radius * PI;
cout << "The area "
<< " is " << area;
}
(b) Correct
Conditional Operator:
(booleanExp) ? exp1 : exp2
Example:
cout << ((num % 2 == 0) ? "num is even" : "num is odd");
y = (x > 0) ? 1 : -1;
Exercise 5:
The following program outputs the ratio of two doubles that are input from the keyboard. The
calculation yields a value that is always a fraction.
The code:
Switch Statements:
switch (status) {
case 0: ……………;
status is 0
break;
case 1: ……………;
break;
break
Compute tax for married file jointly
break
Compute tax for married file separatly
break
Compute tax for head of household
break
status is 1
status is 2
case 2: ……………;
break;
Compute tax for single filers
status is 3
case 3: ……………;
default
Default actions
break;
default: ……………;
Next Statement
System.exit(0);
}
8
Switch Flow Chart:
switch (switch-expression ) {
// switch-expression must yield a value of char, byte, short, or int type and must always be
enclosed in parentheses
case value1: statement(s)1;
break;
// If the break statement is not present, the next case statement will be executed.
// The value1, ..., and valueN must have the same data type as the value of the switchexpression.
case value2: statement(s)2;
break;
……….
default: statement(s)-for-default;
}
Exercise 6:
Cola machine code:
Lab work:
 Apply the programming exercises practically on DEV C++ Program , and show the
results to your instructor.
9
Home work:
 write a program that reads in two integers and determines and prints if the first is a multiple of
the second ?
 writing a program that evaluates the roots of a quadratic equation ax2 + bx + c = 0. Here the
real coefficients a, b and c are inputs to the program. Basically, the real roots can be
calculated from the equation:
You will enter three values for a,b,c ,and check the value (b2 – 4ac) ,if the value is positive
(greater than or equal zero ) then you can calculate the roots and print them ,otherwise you
will print "No real root exists" .
Hint : there is two roots ,because there is two equations once with +ve and the other with –ve.
The output :
 use if ,else if and else statements to implement the following function, enter the value of x
from keyboard, and print the value of f :
 writing a program that reads two numbers and an operator, and then applies one of the four
basic operations ( + , - , * , / ) to the numbers. five sample runs are given in the screen output
column. Hint : use switch Statements:
The output :
10
When you enter invalid operator, the following message will appear :
11