0% found this document useful (0 votes)
72 views19 pages

Exercise 2

The document discusses exercises from a programming course. It provides the solutions and explanations for a series of exercises involving operators, data types, branching statements, and program analysis in C++. The exercises get students familiar with operators, literals, type conversions, logical expressions, if/else statements, switch statements, and analyzing small programs. The document also discusses constants, if/else if/else statements, and translating verbal logic expressions into code.

Uploaded by

Jenn Aguilar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views19 pages

Exercise 2

The document discusses exercises from a programming course. It provides the solutions and explanations for a series of exercises involving operators, data types, branching statements, and program analysis in C++. The exercises get students familiar with operators, literals, type conversions, logical expressions, if/else statements, switch statements, and analyzing small programs. The document also discusses constants, if/else if/else statements, and translating verbal logic expressions into code.

Uploaded by

Jenn Aguilar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Informatik I (D-ITET)

Group 7, 17:00-19:00, ETZ E9

Exercise 2

(10/10/2011)

Discussion on Series 1 exercises (already delivered)

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

Discussion on Series 1 exercises (already delivered)

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

Discussion on Series 1 exercises (already delivered)

Exercise 3 (Explicit Type Conversion) :


(double) (9/5)

= (double) 1.0

(int) 14.0 / 4

= (int) 3

7 / (double) 2

= (double) 3.5

(int) (15.0 / 6) = (int) 2

Exercise 4:

2 < 6 < 4

(7 > 6) == false

true (since (2 < 6) is true it will be evaluated to 1

false

Discussion on Series 1 exercises (already delivered)

Exercise 5:

true && !false

!(9 % 2) || 9 % 3

true

!(1) || 0 false

Another example of type conversion. A zero value assigned to

a boolean variable is converted to false, and a nonzero to true.

Discussion on Series 1 exercises (already delivered)

Exercise 6:
53

char a = 5;

b = d = 81.0 / a + 1;

b = 2.0

double b = 0.0;

double c = 0.0;

c = 12.3 <= 3-2 || (e = 7);

c = 1.0
d = 2

int d = 3;
int e = 0;

a = &

a = (char)((int)M/2);

e = 7

77
The assignment operator

returns the value stored in


the left-hand side variable.

Discussion on Series 1 exercises (already delivered)

Exercise 7 (Program Analysis) :


01 #include <iostream>
02 using namespace std;

03
04 int main()
05 {
06

int s1, s2 = 3;

07

double d1, d2 = 5;

08
09

s1 = 1;

10

d1 = 4.0;

11

s1=?, s2=3, d1=?, d2=5

s1=2, s2=3, d1=4, d2=5

12

d2 = d2 + 10 / d1;

13

s1 = 12 * s2 + 15;

14
15

cout << '1' + '2' << endl;

16

cout << 1 + 2 + 4 << endl;

17

cout << "1" << "2" << endl;

18

cout << d2 << " " << d1*2+1 << endl;

19

return 0;

20 }

s1=51, s2=3, d1=4, d2=5

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

Floating point (float, double, long double)


1.0, 2.0f, 3.0l, 2e4

Character
c, Lw, \n, \x00DC

String
U, exercise

Boolean
true, false

Array, struct
.

Section - Subsection

Constants

Character (e.g. z, y) and string (Hello World) literals


Note: x is a variable, x is a character constant.
Escape sequences
\n

newline

\r

carriage return

\t

tab

\v

vertical tab

\b

backspace

\f

form feed (page feed)

\a

alert (beep)

\'

single quote (')

\"

double quote (")

\?

question mark (?)

\\

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

Notice the lack of ;

int main () {
double r=5.0; // radius
double circle;

circle = 2 * PI * r;
cout << circle;
cout << NEWLINE;
return 0; }

Alternative: const double pi = 3.14159;


You can also concatenate strings, e.g.
"this forms" "a single" "string" "of characters"

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

IF-ELSE IF-ELSE Statement


statement1;
if (expression1)
statement2;

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

SWITCH Statement (Instead of several if-else if-else)


switch (expression)
{
case constant1:
group of statements 1;
break;
case constant2:
group of statements 2;
break;
.
.
.
default:
default statements
};

Expression must be convertible to integer integer/char/wchar_t/bool


Cases must be constants
Should use break at the end of each case otherwise execution continues with
next case

Should use default to catch all not-mentioned cases


14

Exercise 1 Getting familiar with data types.

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

constants to represent type limits, e.g. TYP_MIN and TYP_MAX will


give you the minimum and maximum values respectively of TYP.
Of course for boolean variables you know that they can only take
two values (true and false).
#include <limits>
Use numerical_limits<type>.digits
Use numerical_limits<type>.min/max

15

Exercise 2 Translate verbal expressions into formal ones

You are given a verbal expression involving variables and relations


and you are asked to formally translate them into equivalent C++
expressions (using logical operators we introduced in the first lecture).
Of course there is not a single correct answer, e.g.
verbally: a is smaller than 3 and is not smaller or equal to zero
formally:
(a < 3) && !(a <= 0)
or
(a < 3) && (a > 0)

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

Exercise 3 Analyze the program

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

Exercise 3 Analyze the program

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

Exercise 4 Learn how to use branching statements

Write a program (in C++) that satisfies:

Input: A positive 2-digit number with maximally one


decimal digit (e.g. 87.1 )
Output: The German/English verbal equivalent (e.g.
siebenundachzigkommaeins)

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy