Lecture3 Loops
Lecture3 Loops
Lecture3 Loops
TASK 7
Calculate the expression
𝟓
𝒚 = 𝐬𝐢𝐧 𝒙 + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙
Mathematical functions e.g.
sin(x) cos(x) tan(x) tanh(x) – tangens hyperbolic
sqrt(x) - square root acos(x) – arcus cosinus
pow(x,y) - xy exp(x) - ex
log(x) - natural logarithm abs(x) – Absolute value |x|
log10(x) - common logarithm
All functions, such as sqrt (square root), round (rounds a number) and log (natural logarithm),
can be found in the <cmath> header file:
List of other popular Math functions (from the <cmath> library) can be found in the table below:
Function Description
expm1(x) Returns ex -1
int main()
i=i+1 i++
Increasing the value of the variable i by 1
i=i-1 i--
Decrease the value of the variable i by 1
TASK 8
Generating arithmetic sequence up to a specified maximum value
Maths model
value x
value xmax
arithmetic sequence with difference 5.2
xmax=50
x = 0 5.2 10.4 15.6 ... xmax
START
declaration x, xmax
x=0
xmax = 50
FALSE
x<=xmax
loop
TRUE
Show x
x = x +5.2
STOP
x 0 loop 1 x= x+5.2 0+5.2
Loops are handy because they save time, reduce errors, and they make code more readable.
The while loop loops through a block of code as long as a specified condition is true:
Note: Do not forget to increase the variable used in the condition, otherwise the loop
will never end!
TASK 9
Functions in the table – discretization
Generating numbers of an arithmetic sequence x to a specified
maximum value and calculating the value of the function y = f (x) for
these values according to the formula:
𝟓
𝒚 = 𝐬𝐢𝐧(𝒙/𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙
Maths model
value x value xmax arithmetic sequence with difference 0.2
x=0.2 0.4 0.6 … xmax value y = f(x)
𝟓
𝒇(𝒙) = 𝐬𝐢𝐧(𝒙 /𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙
x y
0.2 0.53964055
0.4 1.200007353
0.6 1.654297702
0.8 2.017387069
1 2.327194697
1.2 2.601181993
1.8 3.286301878
2 3.482311716
2.2 3.665817131
2.4 3.838118569
2.6 4.000183446
2.8 4.152746027
declaration x, xmax, y
x = 0.2
xmax = 50
FALSE
x<=xmax
y = f(x)
loop
TRUE
Show x, y
x = x +0.2
STOP
x 0.2 y 0.53 loop 1 x= x+0.2 0.2+0.2
x 0.4 y 1.2 loop 2 x= x+0.2 0.4+0.2
x 0.6 y 1.65 loop 3 x= x+0.2 0.6+0.2
x 0.8 y 2.01 loop 4 x= x+0.2 0.8+0.2
x 1.0 y 2.32 loop 5 x= x+0.2 1.0+0.2
x 1.2 y 2.60 loop 6 x= x+0.2 1.2+0.2
x 1.4 y 2.84 loop 7 x= x+0.2 1.4+0.2
x 1.6 y 3.07 loop 7 x= x+0.2 1.6+0.2
Maths model
value k ,
arithmetic sequence with difference 1 ,
the number of elements of the sequence n=19
k= 0 1 2 3 4 …. n
START
declaration k, n
k=0
n = 19
FALSE
k<=n
TRUE
loop
Show k
k = k +1
STOP
k 0 loop 1 k++ 0+1
k 1 loop 2 k++ 1+1
k 2 loop 3 k++ 2+1
k 3 loop 4 k++ 3+1
k 4 loop 5 k++ 4+1
k 5 loop 6 k++ 5+1
k 6 loop 7 k++ 6+1
k 7 loop 8 k++ 7+1
k 19 loop 20
Statement 1 is executed (one time) before the execution of the code block.
Statement 3 is executed (every time) after the code block has been executed.
TASK 11
Generate n random integers
Maths model
value k ,
value n=15
function rand() - a function that generates random integers in a range
(0 - RAND_MAX). (RAND_MAX =32767)
START
declaration integer i, k, n
n = 15
i=1
FALSE
i<=n
TRUE
k = rand()
loop
Show k
i = i +1
STOP
#include <iostream>
#include <cstdlib> // library of random number function rand()
using namespace std;
int main()
{ // start of the program
Maths model
sequence
Value i , value sum
value n=15
𝑛
1 1 1 1 1
∑ 2 = + + + ⋯+ 2
𝑖 1 4 9 𝑛
𝑖=1
START
declaration i, sum, n
n = 15
i = 1 sum=0
FALSE
i<=n
loop
TRUE
sum = sum +1./i2
i = i +1
Show sum
STOP
int i, n(15);
double sum;
for(sum=0,i=1; i<n ; i++){
sum=sum + 1./(i*i);
}
cout << " sum= "<< sum << endl;
Loop do while
do
A block of commands that are executed if condition is true
while (condition) ; // there must be semicolon !!
------------------------------------------------------------------------------------------------
double x = 0, xmax= 50; //declaration and assigning
// value
do { // start of the loop
cout<< x <<endl;
x=x+5.2;
} while (x<=xmax); // end of the loop
The do/while loop is a variant of
the while loop. This loop will execute the code
block once, before checking if the condition is
true, then it will repeat the loop as long as the
condition is true.
declaration x, xmax
x=0
xmax = 50
Show x
loop
x = x +5.2
TRUE
x<=xmax
FALSE
STOP
while do while
break - termination (break) of a loop
Breaks the loop and jumps to the first statement after the loop.
-----------------------------------------------------------------------------------------
int i, n(15);
double sum;
for(sum=0,i=1; i<n ; i++){
sum=sum + i*i;
if (sum>500) break; // break the loop
}
cout << " sum "<< sum << endl;
START
declaration i, sum, n
n = 15 i=1
FALSE
i<=n
TRUE
loop
i = i +1
Show sum
STOP
continue - transition to the beginning of the loop
It jumps to the beginning of the loop before the current iteration has finished
------------------------------------------------------------------------------------------
for( int x = 0; x < 8; x++ ) {
cout << "x = " << x << endl;
if( x == 2 || x == 3 || x == 5 ) {
cout << "Now 'x' is " << x;
cout << " - run continue!" << endl;
continue;
}
cout << "END step x = " << x <<endl;
}
START
declaration x, n
n=8 x= 1
FALSE
x< n
loop
TRUE
x = x +1
x==2 or x==3
TRUE
Show x
STOP
TASK 13
Generating a Fibonacci sequence with a specified number of elements.
Maths model
Fibonacciego sequence
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 …
value i value a0=0 a1=1
value n=15 rules a2 = a0 + a1 ai = ai-2 + ai-1
START
declaration i, a0, a1, a2, n
n = 15
recursion start
a0 = 0 a1 = 1
values
i=1
Show
a0 =a0
1 , a1
a0 = 1 TRUE
i<=n
FALSE a0 = 1
a2 = a0 + a1
recursion rule
loop
Show a2
a0=a1
a1=a2 recursion rule
i = i +1
STOP
int a0, a1, a2, n=15;
a0=0; // recursion start value
a1=1; // recursion start value
cout<< a0 <<" "<< a1 <<" ";// showing the first elements
for(int i=0; i<=n; i++){ //**********
a2 = a0 + a1; // recursion rule
loop
cout<< a2 <<" ";
a0=a1; // recursion rule
a1=a2; // recursion rule
} //***************