0% found this document useful (0 votes)
21 views24 pages

Chapter 8

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)
21 views24 pages

Chapter 8

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/ 24

Map of the course

1. History (of the Programming language C++)


2. Literature
3. A first example
4. Some remarks on the compilation and binding of programs
5. The programming environment Dev-C++
6. Expressions in C++
7. Formatted input and output
8. Control structures
9. Functions
10. Arrays
11. Using files
1
8. Control structures
8.1 One-sided branch

if ( c o n d itio n )
{ // o p e n b lo c k
// a ll e x e c u ta b le s ta te m e n ts
} // c lo s e b lo c k

if ( c o n d itio n ) { // o p e n b lo c k
// a ll e x e c u ta b le s ta te m e n ts
} // c lo s e b lo c k

Example: 2_e
2
8.2 Multiple branches
A multiple branch is a generalization of a one-sided branch.

if ( c o n d itio n ) if ( c o n d itio n 1 )
{ {
} }
e ls e e ls e if ( c o n d itio n 2 )
{ {
} }
e ls e if ( c o n d itio n 3 )
{
}
e ls e
{
}

If one condition is satisfied, the corresponding block (and only this one!) is executed.
If no condition is satisfied,
• either no block is executed (if no else-branch is present) or
• if else is present, the last block is executed (else-branch)
3
2
2nd Assignment: compute the solutions of the quadratic equation a x  b x  c  0

real solution:
input a=? a=0 b=? b0 c
a, b, c x1  
b

a0 b=0

2
b  4 a c
compute discriminant D  2
4 a c=? c0 no solution

D=?
c=0
every number is solution

D<0 D=0 D>0

two complex solutions: real solution: two real solutions:


b b b
x1 / 2    i ( D ) x1   x1 / 2    D
2 a 2 a 2 a

4
I Task
2
Compute the solutions of a x  b x  c  0

II Definition of the problem

Input coefficients a, b, c
Compute solutions
Output solutions

III Design of the program


Read parameters a, b, c
algorithm

IV Program
Write source code

5
algorithm if a  0 then
compute discriminant d= (b² - 4ac) / (4a²)
if d>0
compute real solutions x1 and x2
x1= -b/(2. * a) + sqrt(d)
x2= -b/(2. * a) - sqrt(d)
output of solutions
else if d<0
compute complex solutions x1 and x2
x1= -b/(2. * a) +I* sqrt(-d)
x2= -b/(2. * a) - I* sqrt(-d)
output of solutions
else (i.e. if d=0)
compute solution x1
x1= -b/(2. * a)
output of solution
else (i.e. if a = 0)
if b  0 the
compute real solution
x1= -c/b
output of solution
else (i.e. b=0)
if c  0
no solution
output
else (i.e. if c=0)
each number is solution
output

6
8.3 Loops
while-loop

w h ile ( c o n d itio n )
{
// s ta te m e n ts
}

If the condition is not equal 0 (i.e. logically true), the statements in the following
block are executed.
in t i, s ;
i = 0;
s = 0;
w h ile ( s < 1 0 )
Example: 3_e {
s c a n f ( “ % d “ , & i) ;
s = s + i;
}
p r in tf ( “ T h e s u m is % d “ , s ) ;

7
3rd Assignment: bending moment of a beam

3 3
q 1  l1  q 2  l 2  j I y1
M b
 mit j 
8  ( l1  l 2  j ) I y2

2 2
x q 1  l1 x  x 
M (x)  M   1 for x  l1 where 1   
y b
l1 2  
l1  l1 

2
 x  l1  q 2  l 2
2
x  l1  x  l1 
M (x)  M  1 



 for x  l1 where    
y b
l2  2
2 2  
 l2  l 2 

8
for-loop

f o r ( E x p r e s s io n 1 ; E x p r e s s io n 2 ; E x p r e s s io n 3 )
{
// s ta te m e n ts

• Expression1 is the initialization

• Expression2 break-off criterion

• Expression3 is executed during each loop execution (incrementation or


decrementation)

Example: 4_d
9
Comparison of for and while-loop

Note:
The ‘for‘ loop is equivalent to the following ‘while‘ loop

E x p r e s s io n 1 ;
f o r ( E x p r e s s io n 1 ; E x p r e s s io n 2 ; E x p r e s s io n 3 ) w h i l e ( E x p r e s s io n 2 )
{ {
// s ta te m e n ts // s ta te m e n ts
E x p r e s s io n 3 ;
} }

10
Infinite loops

fo r ( ;;) w h ile ( 1 )
{ {

} }

Infinite loops can be ended either by exit, break or return:


• exit(int exit_code) stops the program and returns an exit code
(needs to include stdlib.h)
• break exits the loop and continues after the loop
• return exits the present function

Examples: 4_e, 4_g


11
4th Assignment: Regula falsi

f (x)  0 where f ( x )  ( x  1)  ( x  1)
f ( a i )  (bi  a i )
ui  ai 
I  [a i , bi ] f ( a i )  f (bi )  0 f (bi )  f ( a i )

y
f (ai )

ui bi
ai x
f (ui )

f (bi ) f (x)

12
• Input of interval (a,b)
Design of the program: • Check interval
• Regula falsi
• Output of solution

Check interval: check if there is a solution in the interval

y y y
f (b)
f(x)

f(a) f(a)

x
a b a b x a b x
f (b)
f(a) f(x) f (b)
f(x)

f ( a )  f (b )  0

13
Regula falsi

f (a )

u3 u2 u1
a b x

f (x)
f (u3)

f (u2) f (b )
f (u1)

14
Stop iteration if either

1.) the absolute value f (u i ) is smaller than a given tolerance , for example 110-6, or

y y

f (a )
f (a )

a b x a b x
f (x) f (x)
f (b ) f (b )

2.) the number of iterations exceeds a certain value

15
do-while-loop

for-loop Check their condition at the beginning


while-loop

while-loop do-while-loop

w h ile ( c o n d itio n ) do
{ {
// s ta te m e n ts // s ta te m e n ts
} }
w h ile ( c o n d itio n ) ;

Condition is checked at the beginning Condition is checked at the end


Statements are executed at least
Example: 5_b, 6_b, 6_c once
16
8.4 Alternatives
The switch-statement makes it possible to choose between several
alternatives. It checks if an expression has one of several constant integer
values.

s w it c h ( e x p r e s s io n )
{
c a s e c o n s ta n t1 : •The break-expression causes immediate
// s ta te m e n ts exit of the switch-expression
b re a k ;
c a s e c o n s ta n t2 : • If break is omitted, execution continues
// s ta te m e n ts with the next statement, even if a case
b re a k ; expression is passed over!
d e fa u lt:
// s ta te m e n ts
} Examples: 5_c

17
8.5 Frequently encountered programming errors in
connection with control structures

Control structures are often a source of errors, especially

• End of statement
• Break statement
• Semicolon

The source code is often syntactically correct, but it has semantic


errors.

Note: Computers make what we tell them to do, not what they should do!

Example: 5_d

18
8.6 Nested loops and branches

Blocks in branches and loops can again contain loops and


branches. They are called nested structures.

Rules

1. Outer structures must contain inner structures completely.

2. Nested loops must have different loop variables.

3. We recommend indicating loops by means of tabs in the source code...

Example: 5_e
19
Excursion: one-dimensional arrays
Remember this … ? This is what arrays are for:

One Variable – One Value


int a = 5;

Now assume you want to store


many values of same type Taken from Benedetti and Cranley Head First jQuery

int a0, a1, a2, a3, a4, a5; int a[6];


20
Excursion: one-dimensional arrays

Simply indexed variables (one-dimensional arrays) are defined in C++ by

Type name_of_array[size_of_array];

positive integer constant

• size_of_array is a positive integer constant

• each element of the array can be accessed by its index

• indices run from 0 to (size_of_array-1) (as opposed to other languages


like FORTRAN, where the indices start at 1 by default)

21
Example for an algorithm with nested loops

Bubble-Sort algorithm

to sort, for example, 17, 3, 30, 41, 24, 35, 50, 12

for k=1 to n-1 in steps of 1

for j=n-1 to k in steps of -1

aj-1 > aj
yes no

swap
aj-1 and aj

Example: fpsort
22
Initial order: a0 a1 a2 a3 a4 a5 a6 a7
17 3 30 41 24 35 50 12
j=7 17 3 30 41 24 35 12 50
j=6 17 3 30 41 24 12 35 50
j=5 17 3 30 41 12 24 35 50
k=1 j=4 17 3 30 12 41 24 35 50
j=3 17 3 12 30 41 24 35 50
j=2 17 3 12 30 41 24 35 50
j=1 3 17 12 30 41 24 35 50

j=7 3 17 12 30 41 24 35 50
j=6 3 17 12 30 41 24 35 50
j=5 3 17 12 30 24 41 35 50
k=2
j=4 3 17 12 24 30 41 35 50
j=3 3 17 12 24 30 41 35 50
j=2 3 12 17 24 30 41 35 50
23
5th Assignment: bubble-sort

The so-called bubble-sort algorithm shown at the structure chart shall be


implemented. With this algorithm a set of numbers can be sorted.

for k=1 to n-1 in steps of 1

for j=n-1 to k in steps of -1

aj-1 > aj
yes no

swap
aj-1 and aj

24

You might also like