0% found this document useful (0 votes)
22 views

Mecadineoba # 6: 1. Magalitebi Setyobinebis Gamoyenebaze: Programis Fragmenti Sedegi

1. The document discusses practical and laboratory topics in programming. Practical topics include loops, random number generation functions, and generating random vectors. Laboratory topics include the rand() and time() functions, sorting random numbers, and manipulating random number vectors. 2. Examples are provided to demonstrate for loops, the time() function, and the rand() function. Additional examples show pushing different data types like integers, strings, and doubles into vectors and manipulating the vector elements. 3. The document concludes with challenges, including writing programs to calculate the squares of the first 10 natural numbers and storing them in a vector, defining functions, sorting and printing a random number vector, and calculating the sum of numbers between 310 to 452

Uploaded by

evgeni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Mecadineoba # 6: 1. Magalitebi Setyobinebis Gamoyenebaze: Programis Fragmenti Sedegi

1. The document discusses practical and laboratory topics in programming. Practical topics include loops, random number generation functions, and generating random vectors. Laboratory topics include the rand() and time() functions, sorting random numbers, and manipulating random number vectors. 2. Examples are provided to demonstrate for loops, the time() function, and the rand() function. Additional examples show pushing different data types like integers, strings, and doubles into vectors and manipulating the vector elements. 3. The document concludes with challenges, including writing programs to calculate the squares of the first 10 natural numbers and storing them in a vector, defining functions, sorting and printing a random number vector, and calculating the sum of numbers between 310 to 452

Uploaded by

evgeni
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

mecadineoba # 6

praqtikuli mecadineobis Temebi: laboratoriuli mecadineobis Temebi:


 for Setyobineba
 rand() da time() funqciebi
 ramdenime martivi funqcia  SemTxveviTi ricxvebis Seqmna da
 SemTxveviTi ricxvebis generireba beWdva
 sxvadasxva tipis veqtorTan muSaoba
amocanebi:
amocanebi:
1: 10 pirveli naturaluri ricxvis 1: SemTxveviTi ricxvebis beWdva >>>
kvadratis veqtorSi Cawera da beWdva >>> 2: mimdinare dros dabeWdva >>>
2-3: kvadratSi ayvanis da veqtoris beWdvis
3-4: SemTxveviTi simboloebi >>>
funqciebis Seqmna >>>
4: manipulaciebi SemTxveviTi ricxvebis damoukidebeli samuSao >>>
veqtorTan >>>
damoukidebeli samuSao >>>

masala damoukideblad gacnobisaTvis:


1. magaliTebi for Setyobinebis gamoyenebaze:
# programis fragmenti Sedegi
i = 1
for(int i = 1; i < 4; i++)
1 i = 2
cout << "i = " << i << endl;
i = 3
2 for(int i = 3; i >= 2; i--) iswavle C++
puts("iswavle C++"); iswavle C++
for(char c = 'K'; c > 'D'; --c) K J I H G F E
3
cout << c << ' ';
for(int n = 5; n <= 15; n += 5) n = 5
4 cout << "n = " << n << endl; n = 10
n = 15
5 for(char p = 'A'; p < 'F'; ++p) A B C D E
cout << p << ' ';
for(int a = 5; a >= 2; a--){ a = 5
6 if(a%3 == 0) break; a = 4
cout << "a = " << a << endl;
}
for(int a = 5; a >= 2; a--){ a = 5
7 if(a%3 == 0) continue; a = 4
cout << "a = " << a << endl; a = 2
}
for(int i = 1; i < 50; i++){
7 14 21 28 35 42 49
8 if (i%7 != 0) continue;
cout << i << ' ';
}
for(int i = 7; i < 50; i += 7) 7 14 21 28 35 42 49
9
cout << i << ' ';
for(int x = 10; x > 5; x++) piroba yovelTvis
10 puts("usasrulo ganmeoreba"); WeSmaritia: procesi ar
dasruldeba
კ. გელაშვილი, ი. ხუციშვილი 44
for(int k = 5; true; k++){ k = 5
if(k%2 == 0) continue; k = 7
11 cout << "k = " << k << endl; k = 9
if(k == 9) break;
}
for(int n = 5; n < 3; n++) piroba Tavidanve mcdaria:
12 cout << "n = " << n << endl; Setyobineba ar Sesruldeba

2. time(x) funqciiT sargeblobisaTvis saWiroa #include <ctime> brZanebis


programaSi CarTva. time(NULL) warmoadgens sistemis mimdinare kalendarul dros,
gazomils wamebSi. drois aTvla mimdinareobs garkveuli TariRidan.
# programis fragmenti Sedegi gaSvebis dro
1 cout << time(NULL) << endl; 1319571546 11:39 PM
2 cout << time(NULL) << endl; 1319571606 11:40 PM
3 cout << time(NULL) << endl; 1319571906 11:45 PM
3. funqcia rand() axdens mTeli fsevdoSemTxveviTi ricxvis generirebas. yoveli
mimarTvisas funqcia abrunebs mTel ricxvs [0,RAND_MAX] Sualedidan. RAND_MAX,
Cveulebriv, aris SHRT_MAX –is toli.
# programis fragmenti Sedegi
cout << rand() << endl; 41
1 cout << rand() << endl; 18467
cout << rand() << endl; 6334
/* igive fragmenti Sesrulda 2 saaTis Semdeg */
cout << rand() << endl; 41
2 18467
cout << rand() << endl;
cout << rand() << endl; 6334

4. sxvadasxva tipis veqtorTan muSaobis magaliTebi:


# programis fragmenti Sedegi
1 /* veqtorSi CavweroT sami ricxvi da Semdeg
davbeWdoT veqtoris elementebi */ 23
-70
vector<int> v; 101
v.push_back(23);
v.push_back(-70);
v.push_back(101);
for(int i=0; i<v.size(); i++ )
cout << v[i] << endl;
2 /* klaviaturidan SevitanoT ori ricxvi da
CavweroT veqtorSi */ // Setana
vector<int> a; 10 -100
int number;
for(int m=1; m<=2; m++ ){ // gamosatani ekrani
cin >> number; first element is 10
a.push_back(number); second element is -100
} vector contains 2 elements
cout<<"first element is "<< a[0] <<endl;
cout<<"second element is "<< a[1]<<endl;
cout<<"vector contains "<< a.size()
<<" elements\n";
3 /* veqtorSi CavweroT sami sityva da Semdeg

კ. გელაშვილი, ი. ხუციშვილი 45
davbeWdoT veqtoris elementebi Sebrunebuli
rigiT*/ Elene
vector<string> words; Giorgi
words.push_back("Mariam"); Mariam
words.push_back("Giorgi");
words.push_back("Elene");
for(int i=words.size()-1; i>=0; i-- )
cout << words[i] << endl;
/* veqtorSi CavweroT sami namdvili ricxvi,
Semdeg daviTvaloT da davbeWdoT maTi jami */
vector<double> reals; jami = 8
reals.push_back(2.25);
4 reals.push_back(10.75);
reals.push_back(-5);
double sum =0;
for(int k=0; k<reals.size(); ++k )
sum += reals[k];
cout << "jami = " << sum << endl;
/* veqtorSi CavweroT 5 SemTxveviTi ricxi da
Semdeg davbeWdoT veqtoris elementebi*/
vector<int> vec; 41
int number; 18467
5 for(int count=1; count<=5; count++){ 6334
number =rand(); 26500
vec.push_back(number); 19169
}
for(int i=0; i<vec.size(); i++ )
cout << vec[i] << endl;

saauditorio samuSao:
<<< 1. veqtorSi CavweroT pirveli 10 naturaluri ricxvis kvadrati da Semdeg
davbeWdoT es veqtori.
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> vec;
for(int n=1; n<11; n++)
vec.push_back(n * n);
for(int i=0; i<vec.size(); i++)
cout << vec[i] << '\t';
cout<<endl;
//system("pause");
return 0;
}
programis Sesrulebis Sedegia:
1 4 9 16 25 36 49 64 81 100
Press any key to continue . . .

<<< 2. SeqmeniT funqcia, romlis prototipia int square(int k); da romelic


gamoiTvlis naturaluri ricxvis kvadrats. gamoiyeneT funqcia wina amocanis
amoxsnisTvis.
კ. გელაშვილი, ი. ხუციშვილი 46
#include <iostream>
#include <vector>
using namespace std;
int square (int );
int main(){
vector<int> vec;
for(int n=1; n<11; n++)
vec.push_back( square (n) );
for(int i=0; i< vec.size(); i++)
cout << vec[i] << '\t';
cout<<endl;
//system("pause");
return 0;
}
int square (int k){
return k * k;
}

3. SeqmeniT kidev erTi funqcia prototipiT void printVector(vector<int> );


romelic dabeWdavs veqtoris elementebs. Semdeg gamoiyeneT es funqcia wina
amocanaSi.
#include <iostream>
#include <vector>
using namespace std;
int square (int );
void printVector(vector<int> );
int main(){
vector<int> vec;
for(int n=1; n<11; n++)
vec.push_back( square (n) );
printVector( vec );
//system("pause");
return 0;
}
int square (int k){
return k * k;
}
void printVector(vector<int> x){
for(int i=0; i< x.size(); i++)
cout << x[i] << '\t';
cout << endl;
}

<<< 4. CawereT veqtorSi N SemTxveviTi mTeli ricxvi da dabeWdeT veqtori. Semdeg


daalageT veqtoris elementebi zrdadobiT da isev dabeWdeT veqtori. programaSi
gamoiyeneT adre Seqmnili funqcia printVector.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printVector(vector<int> );
int main(){
vector<int> vec;
int N;
cout<<"ShemoitaneT ricxvebis raodenoba : ";
cin >> N;
კ. გელაშვილი, ი. ხუციშვილი 47
for(int count=1; count<=N; count++)
vec.push_back( rand() );
printVector( vec );
sort( vec.begin(), vec.end() );
printVector( vec );
//system("pause");
return 0;
}
void printVector(vector<int> x){
for(int i=0; i<x.size(); i++)
cout << x[i] << " ";
cout << endl;
}
programis Sesrulebis Sedegia:
ShemoitaneT ricxvebis raodenoba : 5
41 18467 6334 26500 19169
41 6334 18467 19169 26500
Press any key to continue . . .

<<< damoukidebeli samuSao:


1. gaarCieT programa, romelic iTvlis 310 –idan 452 –mde 7-is jeradi ricxvebis
jams.
#include <iostream>
using namespace std;
int main(){
int a; // mTeli ricxvi
int s =0; // 7-is jeradi ricxvebis jami
//ricxvebisTvis 310-dan 452-is CaTvliT, bijiT 1, sruldeba:
for(a = 310; a <= 452; a++)
if( a%7 == 0 ) s += a; //Tu ricxvi 7-is jeradia davamatoT igi jams
cout << "s = " << s << endl;
//system("pause");
return 0;
}
programis Sesrulebis Sedegia:
s = 7630
Press any key to continue . . .

2. amocanis pirobaa: SekribeT SemTxveviTi ricxvebi, vidre jami ar gaxdeba 10–is


jeradi, oRond jamSi ar gaiTvaliswinoT 13-ze damTavrebuli ricxvebi. gaarCieT
Sesabamisi C++ –is programa.
#include <iostream>
using namespace std;
int main (){
int a; // SemTxveviTi ricxvi
int s = 0; // ricxvebis jami
while ( true ){
a = rand();
if(a%100 == 13)
continue;
s += a;
if(s%10 == 0)
break;

კ. გელაშვილი, ი. ხუციშვილი 48
}
cout << "s = " << s << '\n';
//system("pause");
return 0;
}
P programis Sesrulebis Sedegia:
s = 554810
Press any key to continue . . .

3. CawereT saTanado veqtorSi klaviaturidan Semotanili 5 sityva da Semdeg dabeWdeT


veqtoris elementebi. veqtoris elementebis dasabeWdad SeqmeniT da gamoiyeneT
funqcia prototipiT void printWords(vector<string> );
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void printWords(vector<string> );
int main(){
vector<string> w;
string word;
for(int count=1; count<=5; count++){
cin >> word;
w.push_back(word);
}
printWords(w);
//system("pause");
return 0;
}
void printWords(vector<string> a){
for(int i=0; i<a.size(); i++ )
cout<<a[i]<<endl;
}
programis Sesrulebis Sedegia:
Tbilisi Batumi KuTaisi Gori Telavi
Tbilisi
Batumi
KuTaisi
Gori
Telavi
Press any key to continue . . .

4. ras dabeWdavs Semdegi fragmenti? axseniT da Semdeg SeamowmeT kompiuterze:


# programis fragmenti
int k; char p;
1 for(k=1, p='A'; k<9; ++k)
cout << p++ << endl;
int x;
2 for(x=10; -10 || 0; x++)
puts("ar dasruldeba");
int x;
3 for(x=10; x>3 && 5-5; x++)
puts("ar shesruldeba");
int s =30;
4 for(int n=7; true; --n){
s -= n;
კ. გელაშვილი, ი. ხუციშვილი 49
if(n == 4) break;
}
cout << "s = " << s << endl;
int n = 5, p = 1;
for( ; n <= 11; n++){
if( n%2 == 0) continue;
5
p *= n;
}
cout << "p = " << p << endl;
5. klaviaturidan SevitanoT mTeli ricxvebi, CavweroT saTanado tipis veqtorSi da
Semdeg davbeWdoT maTi jamis meoTxedi.
6. klaviaturidan SevitanoT mTeli ricxvebi, CavweroT saTanado tipis veqtorSi da
Semdeg davbeWdoT maTi saSualo ariTmetikuli.
7. klaviaturidan SeitaneT simboloTa mimdevroba, CawereT saTanado tipis veqtorSi da
Semdeg dabeWdeT Setanili simboloebis raodenoba.
8. ipoveT da dabeWdeT 7-dan 32-mde ricxvebis jami.
9. ipoveT 14 –idan 102 –mde kenti ricxvebis jami.
10. ipoveT 4 –idan 123 –mde luwi ricxvebis jami.
11. ipoveT 5 –idan 103 –mde 5-is jeradi ricxvebis jamis meoTxedi.
12. ipoveT m-idan n-mde naturalur ricxvTa jami. m da n SemoitaneT klaviaturidan.
13. a da b – mTeli ricxvebia. c cvlads mianiWeT a-dan b-mde ricxvebis saSualo
ariTmetikuli, da Semdeg dabeWdeT. a da b SeitaneT klaviaturidan.
14. klaviaturidan SevitanoT 12 mTeli ricxvi da davbeWdoT maTi saSualo
ariTmetikuli.
15. ipoveT SemTxveviTi ricxvebis, garda luwi ricxvebisa, saSualo ariTmetikuli
vidre maTi jami ar gaxdeba 21 –is jeradi.
16. ipoveT SemTxveviTi ricxvebis, garda 5-is jeradi ricxvebisa, saSualo
ariTmetikuli vidre maTi jami ar gaxdeba 19 –is jeradi.
17. ramdenjerme dabeWdeT cout << (1.0*rand())/RAND_MAX << endl; ra tipis
SemTveviT ricxvebs qmnis (1.0*rand())/RAND_MAX ? romeli diapazonidan?
18*. wina savarjiSos safuZvelze, rogor SevqmnaT SemTxveviTi namdvili ricxvebi
diapazonidan [a;b]?

კ. გელაშვილი, ი. ხუციშვილი 50
laboratoriuli samuSao:
<<< amocana 1. SeqmeniT programa, romelic dabeWdavs sam striqons. pirvel striqonsi
daibeWdeba sami SemTxveviTi mTeli ricxvi, meoreSi - sami SemTxveviTi mTeli ricxvi
[0;100] Sualedidan, xolo mesameSi – sami SemTxveviTi namdvili ricxvi, aRebuli
[0.,1.0] Sualedidan.
Sesabamis programas aqvs saxe:
////////////////////////////////////////////
// ავტორი:
// პროგრამა: სამი შემთხვევითი რიცხვების ბეჭდვა
////////////////////////////////////////////
#include <iostream>
using namespace std;
int main(){
cout<<"Random numbers:\n";
cout << rand() << '\t' <<rand() << '\t' << rand() << endl;
cout<<"Random numbers from [0,100]:\n";
cout << rand()%101 << '\t' <<rand()%101 << '\t' << rand()%101
<< endl;
cout<<"Random real numbers from [0.0,1.0]:\n";
cout << (double) rand()/RAND_MAX << '\t'
<< (double) rand()/RAND_MAX << '\t'
<< (double) rand()/RAND_MAX << endl << endl;
//system("pause");
return 0;
}
davaleba: gadawereT programa ise, rom SesaZlebeli gaxdes [0;100] Sualedidan
aRebuli N cali SemTxveviTi ricxvis dabeWdva. N–is mniSvneloba SevitanoT
klaviaturidan.
<<< amocana 2. dawereT programa, romelic beWdavs mimdinare dros wamebSi da wuTebSi.
Sesabamis programas aqvs saxe:
///////////////////////////////////////
// ავტორი:
// პროგრამა: მიმდინარე დროის ბეჭდვა
///////////////////////////////////////
#include <iostream>
#include <ctime>
using namespace std;
int main(){
cout << "mimdinare dro (second): " << time(NULL)<<endl;
cout << "mimdinare dro (minute): " << time(NULL)/60<<endl;
//system("pause");
return 0;
}
davaleba: daamateT programaSi Setyobineba, romelic dabeWdavs mimdinare dros
saaTebSi.

<<< amocana 3. SeqmeniT 10 SemTxveviTi mTeli ricxvi [65,90] diapazonidan – didi


inglisuri asoebis kodebi. dabeWdeT am kodebis Sesabamisi simboloebi. dasabeWdad
gamoiyeneT Tqven mier gansazRvruli funqcia. Semdeg dabeWdeT simboloebs Soris
umciresi da misi kodi.

კ. გელაშვილი, ი. ხუციშვილი 51
Sesabamis programas aqvs saxe:
///////////////////////////////////////////////////////////////////////////
// ავტორი: //
// პროგრამა: 10 შემთხვევითი რიცხვის შექმნა, მათი შესაბამისი სიმბოლოების ბეჭდვა //
///////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void printChar(vector<int> );
int main(){
vector<int> vec;
int code;
for(int count=1; count<=10; count++){
code = rand()%(91 -65) + 65;
vec.push_back(code);
}
printChar(vec);
sort( vec.begin(), vec.end() );
printChar(vec);
cout<<"umciresia " <<(char)vec[0]
<< " kodiT " << vec[0] << endl;
//system("pause");
return 0;
}
void printChar(vector<int> m){
for(int i=0; i<m.size(); i++ )
cout << (char)m[i]<<' ';
cout << endl;
}
davaleba: SecvaleT programa ise, rom man dabeWdos SemTxveviTi kodebis mqone
simboloebi dalagebuli klebadobiT.
miTiTeba: sort(vec.begin(), vec.end()); algoriTmi gamoiyeneT sxva saxiT:
sort( vec.rbegin(), vec.rend() );
Sesabamisad SecvaleT umcirebi simbolos da misi kodis beWdvis Setyobinebac.

amocana 4. dawereT programa, romelic Seqmnis 25 SemTxveviT patara asos (['a','z']


Sualedidan) da Semdeg dabeWdavs udidesis kods. miTiTeba: isargebleT formuliT:
rand()%('z '+1- 'a') + 'a';

<<< damoukidebeli samuSao:


1. mocemuli faili Seicavs 3 simbolos. simboloTa Soris umciresi da udidesi maT
kodebTan erTad dabeWdeT sxva failSi. amocana gaakeTeT ori xerxiT: 1) veqtoris
da dalagebis algoriTmis gamoyenebis gareSe; 2) saTanado tipis veqtoris da
dalagebis algoriTmis gamoyenebiT.
2. [-15,80] diapazonidan aRebuli 75 SemTxveviTi ricxvi dabeWdeT klebadobiT.
3. 77 cali [0.,1.0] Sualedidan aRebuli SemTxveviTi namdvili ricxvi dabeWdeT
zrdadobiT.
4. klaviaturidan SemoitaneT N raodenobis namdvili ricxvi. dabeWdeT dadebiT
ricxvebs Soris umciresi da misi rigiTi nomeri.

კ. გელაშვილი, ი. ხუციშვილი 52

You might also like