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

Mecadineoba # 4: Praqtikuli Mecadineobis Temebi: Laboratoriuli Mecadineobis Temebi

The document discusses practical topics in programming such as: - Using mathematical functions like abs(), sqrt(), and pow() - The sizeof operator and standard data type sizes and value ranges - Examples of using switch statements with breaks and without breaks It also provides examples of code snippets with explanations of output for each topic. At the end it provides a problem to calculate the area of a triangle using Heron's formula and requests input of side lengths from the user.

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)
32 views

Mecadineoba # 4: Praqtikuli Mecadineobis Temebi: Laboratoriuli Mecadineobis Temebi

The document discusses practical topics in programming such as: - Using mathematical functions like abs(), sqrt(), and pow() - The sizeof operator and standard data type sizes and value ranges - Examples of using switch statements with breaks and without breaks It also provides examples of code snippets with explanations of output for each topic. At the end it provides a problem to calculate the area of a triangle using Heron's formula and requests input of side lengths from the user.

Uploaded by

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

mecadineoba # 4

praqtikuli mecadineobis Temebi: laboratoriuli mecadineobis Temebi:


 maTematikuri funqciebis gamoyeneba  Cadgmuli if - else
 operatori sizeof  switch operatoris gamoyeneba
 switch da break Setyobinebebi  return –is specifika funqciaSi
amocanebi: amocanebi:
1. samkuTxedis farTobis gamoTvla >>>
1. kvadratuli gantolebis amoxsna >>>
2-3. rTuli gamosaxulebis gamoTvla
switch-iT >>> 2. xmovania Tu Tanxmovani klaviaturidan
Setanili simbolo-aso? >>>
4. simbolo-cifris luw-kentovnebis
garkveva >>> damoukidebeli samuSao >>>
5. studentis Sefasebis (A,B,…,E) Sesabamisi
qulebis diapazonis dabeWdva >>>
damoukidebeli samuSao >>>

masala damoukideblad gacnobisaTvis:


1. zogierTi maTematikuri funqcia:
1) abs(x) funqciaa, romelic gamoiTvlis mTeli x ricxvis moduls - mTel
ricxvs. fabs(x) funqcia gamoiTvlis namdvili x ricxvis moduls - namdvil
ricxvs. am funqciebiT sargeblobisaTvis saWiroa #include<cmath> direqtivis
CarTva.
# programis fragmenti Sedegi
1 cout << abs(-21) << endl; 21
2 cout << abs(345) << endl; 345
3 cout << abs(0) << endl; 0
4 cout << fabs(-32.25) << endl; 32.25
5 cout << fabs(98.00457) << endl; 98.0046
6 cout << fabs(-1) << endl; 1

2) funqcia sqrt(x) gamoiTvlis arauaryofiTi namdvili x ricxvidan kvadratul


fesvs - namdvil ricxvs. funqcia aRwerilia cmath failSi.
# programis fragmenti Sedegi
1 cout << sqrt(2.25) << endl; 1.5
2 cout << sqrt(-2.25) << endl; -1.#IND
3 cout << sqrt(36.) << endl; 6
4 cout << sqrt(-16.0) << endl; -1.#IND
5 cout << sqrt(0.) << endl; 0

3) funqcia pow(x,y) gamoiTvlis xy mniSvnelobas - namdvil ricxvs, sadac x da y


- namdvili ricxvebia. funqcia aRwerilia cmath failSi.
# programis fragmenti Sedegi
1 cout << pow(1.44, 0.5) << endl; 1.2
2 cout << pow(2.0, -3) << endl; 0.125
3 cout << pow(3.0, 3) << endl; 27
4 cout << pow(-0.7, 2) << endl; 0.49
5 cout << pow(-5, 4.0) << endl; 625
6 cout << pow(-0.2, -3) << endl; -125
7 cout << pow(-16,0.5) << endl; -1.#IND
კ. გელაშვილი, ი. ხუციშვილი 25
2. operatori sizeof:
mocemuli kompiuteris mexsierebaSi mocemuli tipis obieqtis zomis garkveva
SeiZleba sizeof operatoris saSualebiT. programaSi SegviZlia visargebloT int
tipis minimaluri INT_MIN da maqsimaluri INT_MAX mudmivebis mniSvnelobebiT,
unsigned int (uniSno mTeli) tipis maqsimaluri UINT_MAX mudmivis mniSvnelobiT,
char tipis minimaluri CHAR_MIN da maqsimaluri CHAR_MAX mudmivebis
mniSvnelobebiT. am mudmivebis mniSvnelobebiT sargeblobisTvis unda CavrToT
#include<climits> direqtiva.
double tipis mudmivebis mniSvnelobebia - minimaluri DBL_MIN da maqsimaluri
DBL_MAX. am mudmivebis mniSvnelobebiT sargeblobisTvis unda CavrToT
#include<cfloat> direqtiva.
# programis fragmenti Sedegi
// int tipis zoma
cout << sizeof(int) << " baiti\n"; 4 baiti
1 // int tipis ricxvebis diapazoni
cout << INT_MIN<<" : " << INT_MAX -2147483648 : 2147483647
<< endl;
// unsigned int tipis zoma
cout << sizeof(unsigned int)
4 baiti
2 << " baiti\n";
// unsigned int tipis ricxvebis diapazoni 0 : 4294967295
cout << 0 << " : " <<UINT_MAX << endl;
// char tipis zoma
cout << sizeof(char) << " baiti\n";
1 baiti
3 // char tipis cvlilebis diapazoni
cout << CHAR_MIN << " : " << CHAR_MAX -128 : 127
<< endl;
// unsigned char tipis zoma
cout<<sizeof(unsigned char)<<" baiti\n"; 1 baiti
4 // char tipis cvlilebis diapazoni
cout << 0 << " : " << UCHAR_MAX << endl; 0 : 255

// float tipis zoma


cout << sizeof(float) << " baiti\n";
5 // float tipis ricxvebis diapazoni 4 baiti
cout << FLT_MIN << " : " << FLT_MAX
1.17549e-038 : 3.40282e+038
<< endl;
// double tipis zoma
cout << sizeof(double) << " baiti\n";
6 // double tipis ricxvebis diapazoni 8 baiti
cout << DBL_MIN << " : "<< DBL_MAX
2.22507e-308 : 1.79769e+308
<< endl;

3. magaliTebi switch Setyobinebis gamoyenebaze


# programis fragmenti Sedegi
int a = 5; /* daibeWdeba */
switch(a){
case 3: cout << "a aris 3\n"; break;
1 a aris 5
case 5: cout << "a aris 5\n"; break;
case 8: cout << "a aris 8\n"; break;
}
კ. გელაშვილი, ი. ხუციშვილი 26
int x = 7; /* daibeWdeba */
switch(x){
case 1: cout << "x = 1\n"; break;
2 x != 1, x != 3
case 3: cout << "x = 2\n"; break;
default: cout << "x != 1, x != 3\n"; break;
}
int n = 3; /* daibeWdeba */
switch(n){
case 1: cout << "Anna\n"; break;
3 Irakli
case 3: cout << "Irakli\n"; break;
default: cout << "Giorgi\n"; break;
}
int n = 3; /* daibeWdeba */
switch(n){
case 1: cout << "Anna\n"; break;
4 Irakli
case 3: cout << "Irakli\n";
default: cout << "Giorgi\n"; break; Giorgi
}
int n = 1, m; /* daibeWdeba */
switch(n){
case 1: m =n+1; cout << "m = "<<m<<endl;
5 m = 2
case 2: m =3*n; cout << "m = "<<m; break;
default: m =n; cout << "m = "<<m; break; m = 3
}
int n = 1, m; /* daibeWdeba */
switch(n){ m = 2
case 1: m = n+1; cout << "m = "<< m << endl;
6 m = 3
case 2: m = 3*n; cout << "m = "<< m << endl;
default: m = n; cout << "m = "<< m << endl; m = 1
}
char c = 'A'; /* daibeWdeba */
switch(c){
case 'A': cout << "Aleko\n"; break;
Aleko
7 case 'K': cout << "Kote\n"; break;
case 'M': cout << "Mari\n"; break;
default: cout << "Gio\n";
}
char c = 'K'; /* daibeWdeba */
switch(c){
case 'A': cout << "Aleko\n"; break;
Kote
8 case 'K': cout << "Kote\n";
case 'M': cout << "Mari\n"; Mari
default: cout << "Gio\n"; Gio
}
char p;
cin >> p; /* Setana */
switch(p){
case 'A': case 'O': B
9
case 'I': case 'E':
/* gamotana */
case 'U': cout << "Xmovani asoa\n"; break;
default: cout << "Ar aris xmovani aso\n";
} Ar aris xmovani aso
int a, b = 2, c;
cin >> a; //Setana //gamotana
switch( b + a ){ 4 c = 3
case 6: c = 3; break;
10
case 12: c = 2*a - 1; break;
10 c = 19
default: c = a - b;
}
cout << "c = " << c << '\n'; 120 c = 118

კ. გელაშვილი, ი. ხუციშვილი 27
saauditorio samuSao:
<<< 1. heronis formulis meSveobiT gamoTvaleT samkuTxedis farTobi, Tu mocemulia
samkuTxedis a,b da c gverdebi. heronis formulis Tanaxmad,

S  p  p  a p  b  p  c  , sadac p  a  b  c  2 .

#include <iostream>
#include <cmath>
using namespace std;
int main(){
double a, b, c, p, s;
cout << "SemoitaneT a, b, c gverdebis sidideebi :\n";
cin >> a >> b >> c;
//Tu es gverdebi qmnian samkuTxeds, maSin visargebloT heronis formuliT
if( a < b + c && b < a + c && c < a + b )
{
p =(a + b + c)/2;
s =sqrt(p*(p - a)*(p - b)*(p - c));
cout << "partobi = " << s << endl;
}
else
cout << "aseTi gverdebis mqone samkuTxedi ar arsebobs\n";
//system("pause");
return 0;
}
programis Sesrulebis Sedegia:
SemoitaneT a, b, c gverdebis sidideebi :
3 4 5
partobi = 6
Press any key to continue . . .

<<< 2. ipoveT da dabeWdeT u cvladis mniSvneloba, Tu x da y – MmTeli ricxvebia da:


 x  y, Tu x  y  3;

 2( x  y ), Tu x  y  6;
u
 y  3x, Tu x  y  7;
 10, winaaRmdeg SemTxvevaSi

#include <iostream>
using namespace std;
int main(){
int u, x, y;
cout << "x = ";
cin >> x;
cout << "y = ";
cin >> y;
if(abs(x - y) == 3) u = x + y;
else if(abs(x - y) == 6) u = 2*(x + y);
else if(abs(x - y) == 7) u = y - 3*x;
else u = 10;
cout << "u = " << u << endl;
return 0;
}
programis Sesrulebis Sedegia:
კ. გელაშვილი, ი. ხუციშვილი 28
x = 3
y = 5
u = 10
Press any key to continue . . .

3. igive amocana gavakeToT switch Setyobinebis gamoyenebiT:


#include <iostream>
using namespace std;
int main(){
int u, x, y;
cout << "x = ";
cin >> x;
cout << "y = ";
cin >> y;
switch( abs(x - y) ){
case 3: u = x + y; break;
case 6: u = 2*(x + y); break;
case 7: u = y - 3*x; break;
default: u = 10;
}
cout << "u = " << u << endl;
return 0;
} x = -7
programis Sesrulebis Sedegia: y = -4
u = -11
Press any key to continue . . .
<<< 4. dawereT programa, romelic klaviaturidan Setanili simbolo-cifrisTvis
gaarkvevs da dabeWdavs warmoadgens igi kent cifrs, Tu luws.
#include <iostream>
using namespace std;
int main(){
char cifri;
cout << "shemoitaneT simbolo - cifri\n";
cin >> cifri;
switch(cifri){
case '0': case '2':
case '4': case '6':
case '8': cout << cifri << " aris luwi\n"; break;
case '1': case '3':
case '5': case '7':
case '9': cout << cifri << " aris kenti\n"; break;
default: cout << cifri << " ar aris cifri.\n"
"Scade Tavidan!\n";
}
return 0;
} shemoitaneT simbolo - cifri
programis Sesrulebis Sedegia: 7
7 aris kenti
Press any key to continue . . .
<<< 5. klaviaturidan SeitaneT studentis Sefaseba, romelic man miiRo gamocdaze (A,
B, C, D, E). dabeWdeT qulebis Sesabamisi diapazoni.
#include <iostream>
using namespace std;
int main(){
კ. გელაშვილი, ი. ხუციშვილი 29
char shefaseba;
cin >> shefaseba;
switch(shefaseba)
{ case 'A': cout << "91 - 100\n"; break;
case 'B': cout << "81 - 90\n"; break;
case 'C': cout << "71 - 80\n"; break;
case 'D': cout << "61 - 70\n"; break;
case 'E': cout << "51 - 60\n"; break;
default: cout << "ver chaabareT\n";
}
return 0;
}
programa Sesrulebis Sedegia:
A F
91 - 100 ver chaabareT
Press any key to continue . . . Press any key to continue . . .

<<< damoukidebeli samuSao:


1. SeadgineT programa, romelic gamoiTvlis da dabeWdavs manZils 0X RerZis or A da
B wertils Soris.
2. SeadgineT programa, romelic gamoiTvlis da dabeWdavs manZils sibrtyis or
A(-1.5,2.0) da B(1,-2.5) wertils Soris. A(x1,y1) da B(x2,y2) wertilebs
Soris manZili ganisazRvreba formuliT
d x2  x1    y 2  y1 
2 2
.

3. SeadgineT programa, romelic gamoiTvlis da dabeWdavs manZils sivrcis or _


A(-1.5,2.0,1.2) da B(1,-2.5,0.5) wertils Soris.
d x2  x1    y 2  y1   z2  z1 
2 2 2
.

4*. heronis formulis meSveobiT gamoTvaleT samkuTxedis farTobi, Tu mocemulia


samkuTxedis wveroebis koordinatebi.
5. gamoTvaleT 2x5+3x3-7-is mniSvneloba, Tu x = 2.1.
6. dawereT programa, romelic Seamowmebs, aris Tu ara mocemuli n weli nakiani.
weliwads ewodeba nakiani, Tu misi gamomsaxveli ricxvi 4-is jeradia, magram 100-is
jeradi wlebidan nakiania mxolod 400-is jeradebi. magaliTad, 1700, 1800 da 1900
– araa nakiani wlebi, xolo 2000 – nakiani welia. n weliwadis nakianobas
SevamowmebT Semdegi pirobiT: n%4 == 0 && n%100 != 0 || n%400 = = 0
7. ras dabeWdavs programis fragmenti,
int n, m;
cin >> n;
switch(n){
case 1: m = n + 1; cout << "m = " << m; break;
case 2: m = 3*n; cout << "m = " << m; break;
default: m = n; cout << "m = " << m; break;
}
Tu SevitanT n -is Semdeg mniSvnelobebs: a) 1; b) 2; g) 3.
8*. dawereT programa, romelic daiTvlis da dabedWavs koreqtul Sedegs, Tu SevitanT
or mTel ricxvs da Semdeg ariTmetikuli moqmedebis binarul operators ('+', '-
', '*' an '/').

კ. გელაშვილი, ი. ხუციშვილი 30
laboratoriuli samuSao:
<<< amocana 1. kvadratuli gantolebis amoxsna.
/////////////////////////////////////////
// პროგრამა: კვადრატული განტოლების
// ამოხსნა
/////////////////////////////////////////
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double a, b, c, d, x1, x2;
cout << "SeitaneT gantolebis a, b da c koeficientebi\n";
cin >> a >> b >> c;
d = b*b - 4*a*c;
if(d < 0)
cout << "\ngantolebas namdvili amonaxsni ar gaaCnia\n";
else if(d == 0)
{
x1 = -b/(2*a);
cout << "\ngantolebis amonaxsnebia: "
"x1 = x2 = " << x1 << '\n';
}
else
{
x1 =( -b - sqrt(d))/(2*a);
x2 =( -b + sqrt(d))/(2*a);
cout<<"\ngantolebis amonaxsnebia: "
"x1 = " << x1 << '\t' << "x2 = " << x2 << '\n';
}
//system("pause");
return 0;
}
davaleba:
a) SeasruleT programa Semdegi monacemebisTvis: 1) a = 1, b = 4, c = 4;
2) a = 1, b = 5, c = 6; 3) a = 1, b = 2, c = 9.
b) SeiZleba Tu ara am amocanaSi gamoviyenoT switch operatori?
g) programa imuSavebs sworad, Tu SeitanT nulisagan gansxvavebul a –s. SeasruleT
programa monacemebisTvis a = 0, b = 2, c = 3; da axseniT miRebuli Sedegi.
rogor gadavweroT programa, rom man koreqtulad imuSavos a = 0 SemTxvevaSic?
d) gaarCieT Semdegi programa. ZiriTadi gansxvaveba imaSi mdgomareobs, rom main()
funqcia, nebismieri sxva funqciis msgavsad, dauyovnebliv mTavrdeba rogorc ki
marTva gadaecema return Setyobinebas.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double a, b, c, d, x1, x2;
cout << "SeitaneT gantolebis a, b da c koeficientebi\n";
cin >> a >> b >> c;
if( a == 0 )
{
კ. გელაშვილი, ი. ხუციშვილი 31
cout << "\na=0, kvadratuli gantoleba ar gvaqvs. "
"sheitanet koreqtuli monacembi\n";
return 0;
}
d = b*b - 4*a*c;
if( d < 0 )
{
cout << "\ngantolebas namdvili amonaxsni ar gaaCnia\n";
return 0;
}
if(d == 0)
{
x1 = -b/(2*a);
cout << "\ngantolebis amonaxsnebia: x1 = x2 = " << x1 << '\n';
return 0;
}
x1 = ( -b - sqrt(d))/(2*a);
x2 = ( -b + sqrt(d))/(2*a);
cout<<"\ngantolebis amonaxsnebia: "
"x1 = " << x1 << '\t' << "x2 = " << x2 << '\n';
return 0;
}
<<< amocana 2. dawereT programa, romelic klaviaturidan Setanil patara asosTvis
gaarkvevs xmovania igi Tu Tanxmovani da dabeWdavs Sesabamis daskvnas.
///////////////////////////////////
// ავტორი:
// პროგრამა: ხმოვანი თუ თანხმოვანი?
///////////////////////////////////
#include <iostream>
using namespace std;
int main(){
char aso;
cout << "shemoitaneT patara aso\n";
cin >> aso;
switch(aso){
case 'a': case 'o':
case 'i': case 'e':
case 'u': cout << aso << " aris xmovani\n"; break;
default: cout << aso << " aris Tanxmovani\n";
}
//system("pause");
return 0;
}
davaleba:
a) SeasruleT programa sxvadasxva patara asoebisTvis da darwmundiT mis sisworeSi
b) ra moxdeba, Tu klaviaturidan patara asos nacvlad SevitanT sxva simbolos?
rogor gavasworoT programis kodi, rom am SemTxvevaSi programam dagvibeWdos
saTanado gzavnili? miTiTeba: programis teqstSi switch operatoramde CaamateT
Semdegi fragmenti
if(aso < 'a' || aso > 'z')
cout << aso <<" ar aris patara aso.\n"
"Try again!\n";
else

კ. გელაშვილი, ი. ხუციშვილი 32
kvlav SeasruleT programa nebismieri simbolosTvis da darwmundiT, rom igi
yovelTvis imuSavebs koreqtulad.

<<< damoukidebeli samuSao:


1. gaarCieT valutis gacvlis programa, romelsac larebSi Semotanili Tanxis sidide
gadahyavs moTxovnis Sesabamisad dolarSi (D), evroSi (E) an inglisur funtSi (P).
#include <iostream>
using namespace std;
int main(){
double amount, convert;
char letter;
cout << "Enter amount in GEL : ";
cin >> amount;
cout << "Enter the first letter of currency\n"
"you wish to convert to (D, E or P): ";
cin >> letter;
switch(letter){
case 'D': convert = amount*0.588408;
cout << amount << " GEL == "<<convert << " USD\n";
break;
case 'E': convert = amount*0.413449;
cout << amount << " GEL == " << convert << " EUR\n";
break;
case 'P': convert = amount*0.365252;
cout << amount << " GEL == " << convert << " GBP\n";
break;
}
//system("pause");
return 0;
}

2. gaarCieT programa, romelsac moTxovnis Sesabamisad gadahyavs santimetrebi (c)


diumebSi (i) an piriqiT.
#include <iostream>
using namespace std;
int main()
{
// number of centimeters in an inch
const double cm_per_inch = 2.54;
int length; // length in inches or centimeters
char unit;
cout << "Please enter a length followed by a letter (c or i):\n";
cin >> length >> unit;
switch (unit) {
case 'i' :
cout << length << " in == "<< cm_per_inch*length << " cm\n";
break;
case 'c' :
cout << length << " cm == " << length/cm_per_inch << " in\n";
break;
default:
cout << "Sorry, I don't know a unit called '" << unit<< "'\n";
break;
}
//system("pause");
return 0;
}
3. gaarCieT programa, romelic “xsnis” gadasasvlelze qcevis wesebs SuqniSnis
CarTuli feris mixedviT (SemoitaneT Yellow, Green an Red).
კ. გელაშვილი, ი. ხუციშვილი 33
#include <iostream>
#include <string>
using namespace std;
int main()
{
string Traffic_light_color;
cout << "What color of a traffic light is included? ";
cin >> Traffic_light_color;
switch (Traffic_light_color[0]){
case 'Y': cout << "Be ready !\n"; break;
case 'G': cout << "Go !\n"; break;
case 'R': cout << "Stop !\n";
}
//system("pause");
return 0;
}
4. gaarCieT programa, romelic Tvis nomris mixedviT beWdavs Tu weliwadis romel
dros ekuTvnis mocemuli Tve.
#include <iostream>
using namespace std;
int main()
{
int month_number;
cout << "Enter month's number ";
cin >> month_number;
switch(month_number){
case 12:
case 1:
case 2: cout << "Winter\n"; break;
case 3:
case 4:
case 5: cout << "Spring\n"; break;
case 6:
case 7:
case 8: cout << "Summer\n"; break;
default: cout << "Autumn\n";
}
//system("pause");
return 0;
}

5. switch Setyobinebis gamoyenebiT gamoTvaleT mTeli d cvladis mniSvneloba, Tu


 a %b  10, a  c  3;

d   a  b  b / 4, a  c  6;
a  b  2c, a  c  9.

a, b da c mTeli ricxvebi SeitaneT klaviaturidan. SeamowmeT Tqveni programis
muSaoba Semdegi sawyisi monacemebisaTvis:
1). a =2, b =3, c =5; 2). a =2, b =2, c =3; 3). a =2, b =1, c =4.
6. switch Setyobinebis gamoyenebiT gamoTvaleT u cvladis mniSvneloba, Tu
 3x  y, x  2 y  1;

u  y  x, x  2 y  2;
 x  y, x  2 y  3.

Tu ki x  2y ar udris arc 1-s, arc 2-s da arc 3-s, maSin u-s mivaniWeT x  y -is
mniSvneloba. x da y ricxvebi SeitaneT klaviaturidan.
კ. გელაშვილი, ი. ხუციშვილი 34

You might also like