Lecture4 Algorythms
Lecture4 Algorythms
TASK 14
Draw a set of n integers from the interval <-30, 30>
rand() function that generates an integer random number from the range < 0 - RAND_MAX >
RAND_MAX = 36767
rand()%11 random integers: 0 1 2 3 4 5 6 7 8 9 10
rand()%11 -10 random integers: -10 -9 -8 … 0
rand()%21 -10 random integers: -10 -9 -8 … 0 +1 … +10
Input n, p1, p2
i=1
FALSE
i<=n
TRUE
k = rand()%(p2-p1+1)+p1
loop
Show k
i = i +1
STOP
#include <iostream>
#include <cstdlib> // function rand()
using namespace std;
int main() { // ------------------------------
int k,n,p1,p2;
cout<<" number of draws n="; cin>>n;
cout<< "the smallest integer drawn number ="; cin>>p1;
cout<< "the highest integer drawn number ="; cin>>p2;
return 0;
}// ---------------------------
TASK 15
Draw a set of n real numbers from the interval <-50, 50>
rand() function that generates an integer from the range < 0 - RAND_MAX >
RAND_MAX = 36767
double integer
Declaration integer i, n
Input n, p1, p2
i=1
FALSE
i<=n
TRUE
k = 1.*rand()/RAND_MAX *(p2-p1)+p1
loop
Show k
i = i +1
STOP
#include <iostream>
#include <cstdlib> // function rand()
using namespace std;
int main() { // ------------------------------
int n;
double k,p1,p2;
cout<<" number of draws n="; cin>>n;
cout<< " the smallest real drawn number ="; cin>>p1;
cout<< " the highest real drawn number ="; cin>>p2;
TRUE
d1<=k<=d2 Show k
FALSE
i = i +1
STOP
int n;
double k,p1,p2, d1,d2;
cout<<" number of draws n="; cin>>n;
cout<< " the smallest real drawn number ="; cin>>p1;
cout<< " the highest real drawn number ="; cin>>p2;
cout<< " the smallest shown number ="; cin>>d1;
cout<< " the highest shown number ="; cin>>d2;
Declaration x, xmax=50, y, y1
Input y1
x = 1.5
FALSE
x<=xmax
TRUE
y = f(x)
TRUE
loop
y>y1 Show x, y
FALSE
x = x +1.5
STOP
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double x(1.5), xmax=50, y, y1; // declaration
cout<< "y1 "; cin>>y1;
Maths model
value x value xmax arithmetic series with difference 0.1
x=0.1 0.2 0.3 … xmax value y = f(x)
𝟓
𝒇(𝒙) = 𝐬𝐢𝐧(𝒙 /𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙
value ymax, xm
START
Declaration x, xmax=50, y, ymax, xm
x = 0.1
ymax = f(x) xm=0.1
FALSE
x<=xmax
TRUE
y = f(x)
TRUE
loop
y>ymax ymax =y
FALSE xm = x
x = x +0.1
STOP
double x(0.1),xmax=50, y, xm, ymax; // declaration
ymax=sin(x/3)+sqrt(x) + log10(x)+ pow(x,0.2);
xm=x;
while (x<=xmax) { // start of the loop --------------
y=sin(x/3)+sqrt(x) + log10(x)+ pow(x,0.2);
if (y>ymax) {
ymax=y;
xm=x;
}
x=x+0.1;
} // end of the loop ----------------------------------
cout<<"maximum xm "<<xm<<" ymax "<< ymax<<endl;
TASK 19
Generating numbers of an arithmetic series x
up to the specified maximum value and
computing the value of the function y= f(x)
for these values according to the formula:
𝟓
𝒚 = 𝐬𝐢𝐧(𝒙/𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙 − 𝟖
Find value x0 , where f(x) is value 0
Maths model
value x value xmax arithmetic series with difference 0.1
x=0.1 0.2 0.3 … xmax value y = f(x)
𝟓
𝒇(𝒙) = 𝐬𝐢𝐧(𝒙 /𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙 − 𝟖
value x0
𝟓 𝟓
𝒇(𝒙) = 𝐬𝐢𝐧(𝒙 /𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙 − 𝟖 𝒇(𝒙) = |𝐬𝐢𝐧(𝒙 /𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙 − 𝟖|
START
Declaration x, xmax=50, y, x0, ymin
x = 0.1
ymin = |f(x)| x0=0.1
FALSE
x<=xmax
TRUE
y = |f(x)|
TRUE
loop
y<ymin ymin =y
FALSE x x0 = x
x = x +0.1
STOP
double x(0.1), xmax=50, y,x0, ymin; // declaration
ymin=abs(sin(x/3)+sqrt(x) + log10(x)+ pow(x,0.2)-8);
x0=x;
while (x<=xmax) { // start of the loop --------------
y=abs(sin(x/3)+sqrt(x) + log10(x)+ pow(x,0.2)-8);
if (y<ymin) {
ymin=y;
x0=x;
}
x=x+0.1;
} // end of the loop ----------------------------------
cout<<"x0 "<<x0<<" ymin "<< ymin<<endl;
TASK 20
Generating numbers of an arithmetic series x
up to the specified maximum value and
computing the value of the function y= f(x)
for these values according to the formula:
𝟓
𝒚 = 𝐬𝐢𝐧(𝒙/𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙
Find the value of the definite integral of the
function f(x) range < 0, xmax >
Maths model
value x value xmax arithmetic series with difference dx=0.1
x=0.1 0.2 0.3 … xmax value y = f(x)
𝟓
𝒇(𝒙) = 𝐬𝐢𝐧(𝒙 /𝟑) + √𝒙 + 𝐥𝐨𝐠 𝟏𝟎 𝒙 + √𝒙
value dx (integral step) value: integral
START
Declaration x, xmax=50, y, dx=0.1, integral
x = 0.1
integral =0
FALSE
x<=xmax
TRUE
y = |f(x)|
loop
x = x +dx
Show integral
STOP
double x, xmax, y, dx=0.1, integral;
cout<<" integral 0.1-xmax xmax="; cin>>xmax;
for(integral=0, x=0.1; x<=xmax; x=x+dx) {// start
y=abs(sin(x/3)+sqrt(x) + log10(x)+ pow(x,0.2));
integral = integral + y*dx;
} // end ---------------------------------
cout<<" integral "<< integral <<endl;
Symulations Algorithms
TASK 21
Calculate the position and speed of the vehicle moving at speed v1 and acceleration a.
After the distance d1 has been traversed, the vehicle travels at a constant speed until the
distance d2 has been traversed.
Maths model
t=0 dt=0.1s
v1=0, a=0.5 m/s2
v2 a=0 v3=0, a=0
d1=1000m d2=2000m
Show d, v
t = t +dt
STOP
double t=0, dt=1, v, a=0.5, d1=1000, d2=2000, d;
d=0;
while(d<=d2){ // ***************
if (d<=d1){
d = a*t*t/2;
v = a*t;
}
else{
d = d + v*dt;
}
cout<< " d " << d << " v " << v <<endl;
t=t + dt;
}// ***************
TASK 22
Calculate the position of vehicle A under acceleration aA.
Calculate the position of the vehicle B moving at constant speed vB in the opposite direction
Set a time limit for these vehicles to meet. The dAB distance between A B is 2000m.
Maths model
t=0 dt=0.1s
vA=0, aA=0.2 m/s2 vB= 10 m/s aB=0
d1=a1*t2/2
v1=a1*t
d2 = v2*t
delta=dab-(d1+d2)
Show t
STOP
double t=0, dt=0.1, v1, v2=10, a1=0.3, d1, d2, dab=2000, delta;
do {
d1=a1*t*t/2;
v1=a1*t;
d2=v2*t;
delta=dab-d1-d2;
cout<< " d1 "<< d1<< " v1 "<<v1
<< " d2 "<< d2<< " v2 "<<v2
<< " delta "<< delta <<endl;
t=t+dt;
} while(delta>=0);
cout<< " meeting time "<< t <<endl;
TASK 23
Calculate the position of the end of the line with uniformly accelerated rotary motion with
angular acceleration ε = 0.1 rad / s2 around the point (x0, y0) for the time tk = 20s.
Maths model
x0=0
x,y y0=0
x=x0+rcos( φ)
y=y0+rsin( φ)
φ
x0,y0
φ=ε*t2/2
t = t + dt
START
x=r*cos(fi)
y=r*sin(fi)
Show t, x, y,
t = t +dt
STOP
#include <iostream>
#include <cmath>
using namespace std;
int main(){// ----------------------------------------
double t=0, dt=0.1, fi, eps=0.1, tk=20,x,y, r=2;
for(t=0; t<=tk; t=t+dt) { //*************
fi=eps*t*t/2;
x=r*cos(fi);
y=r*sin(fi);
cout<< " t "<< t << " x "<< x << " y "<< y <<endl;
}//*************
return 0;
} // ----------------------------------------
TASK 24
Calculate the position (x, y) of the body in projectile
motion when the initial velocity of the thrown body is
v0 = 100m / s and the projection angle is alpha = 30o
Maths model
x = v0 * cos (alfa) * t
t = t + dt
y = v0 * sin (alfa) * t – g * t2 / 2
dt = 0.1s
v0=100m/s
g=10 m/s2
alfa=30o
START
x = v0*cos(alfa*PI/180)*t
y= v0*sin(alfa*PI/180)*t-g*t2/2
Show t, x, y,
t = t +dt
STOP
#include <iostream>
#include <cmath>
using namespace std;
int main(){ // ----------------------------------------
double t, dt=0.5, fi, g=10, x, y=0, alfa, v0=100;
cout<< " alfa="; cin>> alfa;
for(t=0; y>=0; t = t + dt) {//*************
x=v0*cos(alfa*M_PI/180)*t;
y=v0*sin(alfa*M_PI/180)*t-g*t*t/2;
cout<< " t " << t << " x "<< x << " y " << y <<endl;
}//*************
return 0;
}// ----------------------------------------