Exercise 2
Exercise 2
Exercise 2
(10/10/2011)
Priority order
Exercise 2 :
15.0 / 5
= (double) 3
9 / 5 * 5
= (int) 5
11.0 / 4.0
= (double) 2.75
30 % 7
= (int) 2
2 * 5 / 4.0
= (double) 2.5
Source: http://www.cplusplus.com/doc/tutorial/operators/
2
Exercise THESE
2:
ARE MOSTLY MIND-GAMES TO GET
4 * 3 / 5.0
= (double)
2.4
USED
TO OPERATORS
11 / 6 * 5
USAGE
= (int) 5
2 + 9 / 4
= (int)
4
IN REAL-LIFE
USE
PARENTHESES
TO GIVE PRIORITIES
5 AND
17.0/4.0
= (double)
0.75 UNDERSTANDABLE
MAKE
YOUR CODE
Priority order
e.g.
2 * 5 / 4.0
res = (4 * 3) / 5.0 ;
= (double) 2.5
Source: http://www.cplusplus.com/doc/tutorial/operators/
3
= (double) 1.0
(int) 14.0 / 4
= (int) 3
7 / (double) 2
= (double) 3.5
Exercise 4:
2 < 6 < 4
(7 > 6) == false
false
Exercise 5:
!(9 % 2) || 9 % 3
true
!(1) || 0 false
Exercise 6:
53
char a = 5;
b = d = 81.0 / a + 1;
b = 2.0
double b = 0.0;
double c = 0.0;
c = 1.0
d = 2
int d = 3;
int e = 0;
a = &
a = (char)((int)M/2);
e = 7
77
The assignment operator
03
04 int main()
05 {
06
int s1, s2 = 3;
07
double d1, d2 = 5;
08
09
s1 = 1;
10
d1 = 4.0;
11
12
d2 = d2 + 10 / d1;
13
s1 = 12 * s2 + 15;
14
15
16
17
18
19
return 0;
20 }
99
7
12
7.5 9
7
Literals
In C++ we have the following kinds of literals:
Integer ([unsigned] short, int, long)
1, 2U, 3L, 4UL, 0xf, 0100
Character
c, Lw, \n, \x00DC
String
U, exercise
Boolean
true, false
Array, struct
.
Section - Subsection
Constants
newline
\r
carriage return
\t
tab
\v
vertical tab
\b
backspace
\f
\a
alert (beep)
\'
\"
\?
\\
backslash (\)
\Xhhhh
Unicode character
Constants
For example Hello World \n will print the characters and then change line.
#define identifier value
#include <iostream>
using namespace std;
#define PI 3.14159
#define NEWLINE '\n
int main () {
double r=5.0; // radius
double circle;
circle = 2 * PI * r;
cout << circle;
cout << NEWLINE;
return 0; }
String literals can extend to more than a single line of code by putting a backslash
sign (\) at the end of each unfinished line, e.g.
"string expressed in \
two lines
10
IF Statement
statement1
statement1;
if (expression1)
statement2;
expression1
statement3;
true
statement2
false
statement3
IF-ELSE Statement
statement1
statement1;
if (expression1)
statement2;
else
false
statement3
expression1
true
statement2
statement3;
statement4;
statement4
11
Dangling else
if (a)
if (b)
statement1;
else
statement2;
if (a){
if (b)
if (a)
{
if (b)
statement1;
statement1;
}else
else
statement2;
statement2;
}
12
Example
else if (expression2)
#include <iostream>
using namespace std;
statement3;
else
statement4;
int main() {
int x;
cout << Please give a number: ;
cin >> x;
if ((x % 2) == 0) {
cout << x << is even << endl;
}
else {
cout << x << is odd << endl;
}
return 0;
}
13
In the first lecture we introduced the basic data types (char, short, int, long,
float, double).
Now you are asked to write a program which prints on screen the size that
each data type occupies in memory (in Bytes), and also the minimum and
maximum values it supports.
To do so you need to either:
#include <climits> (maybe also #include <cfloat>)
Use function sizeof(type) to get the size of the various types
In the header file climits you will find predefined symbolic
15
Logical Expressions:
OR(||) - AND( && ) are sequence points, i.e. any value changes indicated on the left side take place before the
right side is evaluated, e.g.
i 6 || i j will first evaluate i from the left side and then use it in the expression on the right
Getting from left to right, C++ will not bother evaluating parts of a logical expression, if the final outcome has already been
decided, e.g.
b ! 0 & & abs(a/b) 1 will save you from a division by zero
16
dy
0
0
dx
Bool reachable():
Takes as arguments
(piece, x,y,dx,dy) and
returns true
If figure piece can reach
field (dx,dy) from (x,y)
otherwise returns false.
You are asked to perform
reverse engineering, i.e.
based on the code find out
which chess figures (springer,
turm, dame, bauer etc)
correspond to the integer
values of variable piece.
17
For example:
bool b = (x==dx || y==dy);
if (piece==3) {
if (b) {
return true;
} else {
dy
piece=6;
}
Piece=3 -> ?
Piece=6 -> ?
0
0
dx
7
White side
18
In order to do that:
Test initially if the number given as input (xy.z) is between zero and 100.
Get the integer part xy and the decimal part z
Use the division and modulo operators (/ and %) to break the integer part into x and
y.
Use if-else and/or switch statements to discriminate between the different
possibilities.
Take care of all special cases (0, 1, 11, 12, 16, 17, 1xx, x0 1, x11, x12, x16, x17).
Enter the comma z part.
Make sure that you take care of the und case, for example in sieben und achzig.
19