0% found this document useful (0 votes)
14 views40 pages

Lecture3 Loops

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 40

Mathematical calculations – functions

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

abs(x) Returns the absolute value of x

acos(x) Returns the arccosine of x

asin(x) Returns the arcsine of x

atan(x) Returns the arctangent of x

cbrt(x) Returns the cube root of x

ceil(x) Returns the value of x rounded up to its nearest integer

cos(x) Returns the cosine of x

cosh(x) Returns the hyperbolic cosine of x

exp(x) Returns the value of Ex

expm1(x) Returns ex -1

fabs(x) Returns the absolute value of a floating x

fdim(x, y) Returns the positive difference between x and y

floor(x) Returns the value of x rounded down to its nearest integer

hypot(x, y) Returns sqrt(x2 +y2) without intermediate overflow or underflow

fma(x, y, z) Returns x*y+z without losing precision


fmax(x, y) Returns the highest value of a floating x and y

fmin(x, y) Returns the lowest value of a floating x and y

fmod(x, y) Returns the floating point remainder of x/y

pow(x, y) Returns the value of x to the power of y

sin(x) Returns the sine of x (x is in radians)

sinh(x) Returns the hyperbolic sine of a double value

tan(x) Returns the tangent of an angle

tanh(x) Returns the hyperbolic tangent of a double value


#include <iostream>
#include <cmath> // library of mathematical functions
using namespace std;

int main()

{ // start of the program

// all commands of the program

} // end of the program


𝟓
𝒚 = 𝐬𝐢𝐧 𝒙 + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙
#include <iostream>
#include <cmath> // library of mathematical functions
using namespace std;
int main()
{
double x,y;
cout<< " x= "; cin>>x; // two commands
// in one line
y = sin(x) + sqrt(x) + log10(x) + pow(x,0.2);
cout<< " y " << y <<endl;
Changing the value of a variable
x=x+5. x+=5.

The program takes the value of the variable x, adds 5 to


this value and stores the result in the variable x

Name Value Name Value


x 0 x 5

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

x 5.2 loop 2 x= x+5.2 5.2+5.2

x 10.4 loop 3 x= x+5.2 10.4+5.2

x 15.6 loop 4 x= x+5.2 15.6+5.2

x 20.8 loop 5 x= x+5.2 20.8+5.2

x 26.0 loop 6 x= x+5.2 26.0+5.2

the old value of x is overwritten by the new value of x


Loop while
while (condition) // there is no semicolon
A block of commands that is executed if condition is true
------------------------------------------------------------------------------------------------
double x = 0, xmax= 50;//declaration and assigning
// value
while (x<=xmax) { // start of the loop ----
cout<< x <<endl;
x=x+5.2;
} // end of the loop --------------------
Loops can execute a block of code as long as a specified condition is reached.

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.4 2.848866249 Continuous graph


1.6 3.075998141

1.8 3.286301878

2 3.482311716

2.2 3.665817131

2.4 3.838118569

2.6 4.000183446

2.8 4.152746027

3 4.296373987 Discrete graph


START

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

the old value of x is overwritten by the new value of x


#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x(0.2),xmax(50),y;//declaration and assigning
// value
while (x<=xmax) { // start of the loop ------
y=sin(x/3)+sqrt(x) + log10(x)+ pow(x,0.2);
cout<< x <<" "<< y<<endl;
x=x+0.2;
} // end of the loop --------------------
TASK 10
Generating consecutive integers - an arithmetic sequence with a
specified number of elements

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

the old value of k is overwritten by the new value of k


Loop for
performed only once at executed as the last commands at
the beginning of the the end of the loop
loop
for ( start commands ; condition ; end commands)
A block of commands that is executed if condition is true
--------------------------------------------------------------------------------------------
int k,n(19);
for (k=0; k <= n; k++){ // start of the loop ------

cout<< " k= "<< k<<endl;

} // end of the loop --------------------


When you know exactly how many times you
want to loop through a block of code, use
the for loop instead of a while loop:

for (statement 1; statement 2; statement 3) {


// code block to be executed
}

Statement 1 is executed (one time) before the execution of the code block.

Statement 2 defines the condition for executing 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

// all commands of the program

} // end of the program


#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{ int i, k, n(15);
for(i=1;i<=n;i++){ //start of the loop
k=rand();
cout<<i <<" "<<k<<endl;
} // end of the loop --------------------
TASK 12
Generating consecutive numbers of a sequence with a specified number
of elements.
Summing the elements of a sequence.

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.

The loop will always be executed at least


once, even if the condition is false, because
the code block is executed before the
condition is tested.

Do not forget to increase the variable used in


the condition, otherwise the loop will never
end!
do while
START

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

sum = sum +i2


break
TRUE
sum>500

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
} //***************

You might also like