Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

10-lekciya

.pdf
Скачиваний:
1
Добавлен:
27.11.2023
Размер:
502.72 Кб
Скачать

10-Lekciya. Ulıwma tákirarlanıw algoritmleri hám ishpe-ish tákirarlanıwlar Lekciya jobası:

10.1.C++ tilinde tákirarlanıwshı processlerdi programmalastırıw

10.2.Continue operatorı

Gilt sózler: delete, aralıq keltiriw, delete[], new, indeks, this, [] bos yad, void, konteyner, dizim, manzil, nolinshi kórsetkish, túyin, adres alıw &, bosatıw, kórsetkish, virtual destruktor, yad, yad shıǵıwı, destruktor, tipti ózlestiriw, resurslar shıǵıwı, aǵza destruktorı.

10.1. C++tilinde tákirarlanıwshı processlerdi programmalastırıw

Eger programma orınlanıw processinde operator yamasa operatorlar toparı bir neshe márte qayta-qayta orınlansa, bunday processlerdi tákirarlanıwshı (ciklli) process delinedi. C++ tilinde cikldi 3 túrli kóriniste shólkemlestiriw múmkin.

1. Shárti aldınnan tekseriletuǵın tákirarlanıw (aldshártli):

while(shárt) operator(lar);

Bul jerde operatorlar while da kórsetilgen shárt jalǵan bolǵansha tákirarlanadı. Tákirarlanıw denesi quramalı bolsa, yaǵnıy 1 den artıq operatorlar qatnassa, olardı óz aldına {} (figuralı qawıs) ishine alıw kerek boladı.

Máselen: b = 2*(a+5); a € [1, 10]; h=1;

#include <iostream> #include <math.h> using namespace std; int main( )

{

int a = 1, b; while(a <= 10)

{

b = 2 * (a + 5); cout << “b = ” << b;

cout << “\ta = ” << a << endl; a++;

}

return 0;

}

Ekranda 10 dana a hám b lardıń mánisleri payda boladı. Ekinshi mısal:

#include <iostream> using namespace std; int main( )

{

int i = 10; while(i++ <= 15)

cout << “Sálem!!!” << endl; return 0;

}

Ekranda 6 márte “Sálem!!!” jazıwı payda boladı.

2. Shárti keyin tekseriletuǵın tákirarlanıw (sońshártli cikl):

do

operator(lar) while(shárt);

Tákirarlanıw while da kórsetilgen shárt jalǵan bolǵansha dawam etedi.

Máselen: y = sinx; x € [1, 2]; h=0.1

#include <iostream> #include <math.h> using namespace std; int main( )

{

float x = 1, y; do

{

y = sin(x);

cout << “x = ” << x << ”y = ” << y << endl; x += 0.1;

}

while(x <= 2); getch();

}

Máseleniń algoritmi tómendegi kóriniske iye boladı:

Baslanıw

x, h,

Y= sin x

x, y

x=x+h

jalǵan

ras

 

 

x <= 2

end

Jáne bir mısal: Bul programma klaviaturadan 20 sanı kiritilemen degenshe dawam etedi.

#include <iostream> using namespace std; int main( )

{

int n; do

{

cout << “Sandı kiritiń:” << endl; cout << “n = ”; cin >> n;

}

while(n != 20); return 0;

}

3. Parametrli tákirarlanıw (cikl): Ulıwma kórinisi

for(dáslepki mánis; shárt; ózgeriw qádemi) operator(lar);

Operatorlar 1 danadan artıq bolsa olar óz aldına qawıslar -{} ishine alınadı.

Máselen: Y = cos x; x € [2, 3]; h=0.2;

#include <iostream> #include <math.h> using namespace std; int main( )

{

float x, y;

for(x = 2; x <= 3; x += 0.2)

{

y = cos(x);

cout << “x = ” << x << “\t\ty = ” << y << endl;

}

return 0;

}

Keyingi mısal: 100 ge shekem bolǵan jup sanlardı ekranǵa shıǵarıw programması.

#include <iostream> using namespace std; int main( )

{

int i = 2; while(i <= 100)

{

cout << “i = ” << i << endl; i += 2;

}

return 0;

}

Joqarıdaǵı programmanıń basqa usılları:

for operatorı arqalı:

do-while operatorı arqalı:

 

 

 

do

for(int i = 2; i<=100; i +=2)

cout << “i=”<< i << endl;

cout << “i = ”<< i << endl;

i += 2;

 

while (i <= 100);

 

 

Máselen: 1 den 100 ge shekem bolǵan 3 cifri menen juwmaqlanatuǵın sanlardı ekranǵa shıǵarıw programmasın dúziń (2 túrli usılda).

while operatorı arqalı:

for operatorı arqalı:

 

 

int i=3;

 

while (i <= 100)

for(i=3; i<=100; i+=10)

{

cout << “i=”<< i << endl;

cout << “i = ”<< i <<endl;

 

i += 10;

 

}

 

 

 

Keyingi mısal: Eski másele. Bir adam 100 sum menen bazarǵa bardı. Bazarda 1 sıyır 10 sum, 1 qoy 3 sum, 1 eshki 5 sum hám satıp alınǵan qaramallardıń ulıwma sanı 100 dana bolsa, neshe sıyır, qoy hám eshki satıp alindı?

Sıyırlar sanı: x, qoylar sanı y, eshkiler sanı z dep alınsa:

#include <iostream> using namespace std; int main()

{

int x, y, z;

for(x = 1; x <= 100; x++) for(y = 1; y <= 100; y++) for(z = 1; z <= 100; z++)

if(x + y + z == 100 && 10 * x + 3 * y + 0.5 * z == 100)

{

cout << "x = " << x; cout << "\ny = " << y;

cout << "\nz = " << z << endl;

}

return 0;

}

Bul programmanıń nátiyjesi: x = 5, y = 1, z = 94.

10.2. Continue operatorı

Bul operator járdeminde cikl parametriniń qandayda bir mánisinde esaplawdı toqtatıp, keyingi mánisinde esaplawdı dawam ettiriw múmkin. Máselen: y = 2x

funkciyasın x € [1, 18] aralıqta h=1 mánis penen esaplaw kerek, biraq x=6 yamasa x=13 mánislerinde esaplawdı orınlamaw kerek.

#include <iostream> using namespace std; int main()

{

int x, y;

for(x = 1; x <= 18; x++)

{

if(( x == 6) || (x == 13)) continue;

y = 2 * x;

cout << “x = ”<< x << “\ty =” << y << endl;

}

}

Keyingi mısal. 10 dana izbe-iz kiritiletuǵın pútin oń sanlar qosındısın esaplaw programmasın dúziń. Eger san teris bolsa, qosındıǵa qospaw kerek.

#include <iostream> using namespace std; int main()

{

int x, i, s=0;

for(i = 1; i <= 10; i++)

{

cin >> x; if(x < 0)

continue; s += x;

}

cout << “s = ”<< s << endl; return 0;

}

Keyingi mısal. Y = xn funkciyasın rekurrent formula arqalı esaplaw programmasın dúziń. bul jerde n - pútin san, x – qálegen haqıyqıy san.

#include <iostream> #include <conio.h> using namespace std; int main()

{

float x = 2.56, y = 1; for(int n = 1; n <= 10; n++) y *= x;

cout << ”y = ” << y << endl; getch();

return 0;

}

8

x

2

 

 

n!

Keyingi mısal: Y=

n 1

 

 

x - qálegen haqıyqıy san.

= x2 +

x

2

 

1* 2

+

x

2

 

x

2

 

...

 

1* 2 * 3

1* 2 * 3 * 4 * 5 * 6 * 7 * 8

 

#include <iostream> #include <conio.h> using namespace std; int main()

{

float x = 3.75, y = 0; long p = 1;

for(int n = 1; n <= 8; n++)

{

p *= n;

y += x * x / p;

}

cout << “y = ” << y << endl; getch();

return 0;

}

Nátiyje: x = 3.75 te y = 24.1633;

 

cos 2x

 

cos3x

Keyingi mısal: S=cosx +

2

3

 

 

 

 

n=10

....

 

cos nx n

; bul jerde

 

x

9

5

5

 

#include <iostream> #include <conio.h> #include <math.h> using namespace std; int main()

{

float a, b, h, x, s, pi = 3.14;

a = pi / 5; b = 9 * pi / 5; h = (b - a) / 10; x = a;

cout. precision (3); //útirden keyin 3 san shıǵadı while(x <= b)

{

s = 0;

for(int n = 1; n <= 10; n++) s += cos(n * x) / n;

cout << ”s = ” << s << endl; x = x + h;

}

getch (); return 0;

}

Keyingi mısal: Azamat penen Ernazar básekilesti. Azamat Ernazarǵa hár kúni (30 kún) 100000 sum beretuǵın bolıptı. Azamat bolsa Ernazarǵa 1-kún 1 tiyin, 2- kún 2 tiyin, 3-kún 4 tiyin, 4-kun 8 tiyin hám t.b. pul beretuǵın bolıptı. Básekide kim utadı? Programmasın dúziń.

Azamat → 30*100000 = 3000000 sum

Ernazar →

30

 

2

i

 

i 0

 

sum → 10737418 sum

#include <iostream> #include <conio.h> #include <math.h> using namespace std; int main()

{

int s, s1 = 1, k = 0;

s = 30 * 100000; // Azamattan Ernazarǵa for(int i = 1; i <= 30; i++)

{

k += s1; s1 *= 2;

}

k /= 100; //tiyindi 100 ge bólsek sum shıǵadı cout <<”Azamattan Ernazarǵa:” << s << endl; cout <<”Ernazardan Azamatqa:” << k << endl; getch ();

}

Baqlaw sorawları:

1.Shárti aldınnan tekseriletuǵın tákirarlanıw.

2.Shárti keyin tekseriletuǵın tákirarlanıw.

3.Parametrli tákirarlanıw.

4.Programmada tákirarlanıwlardı shólkemlestiriw.

5.Tákirarlanıwshı programma ne?

6.Quramalı tákirarlanıwlar.

7.continue operatorı.

8.return operatorı.

Соседние файлы в предмете Программирование на C++