Professional Documents
Culture Documents
Programming Lec 10
Programming Lec 10
-10
: . //
.
.
www.emust.edu.mn
.
.
#include <iostream>
using namespace std;
int main()
{
int age[4];
for (int i = 0; i < 4; ++i)
{ cout << "Nasaa oruulj ogno uu:"; cin >> age[i]; }
for (int i = 0; i < 4; ++i)
{ cout <<endl<< "Tanii oruulsan ni:"<<age[i]; }
return 0;
}
, .
, .
: 44
: 16
: 23
: 68
: 44
: 16
: 23
: 68
.
.
.
. 4
.
.
.
. ,
.
(4 * 2 ) 8
. 0 .
(n-1) (n !).
0-3 4 .
2 .
.
j
. j
.
. 6
, .
#include <iostream>
using namespace std;
const int SIZE=6;
int main()
{ float sales[SIZE];
cout<<endl<<"6 odriin orlogiig daraaluulan oruulna uu"<<endl;
for (int i = 0; i < SIZE; ++i)
cin>>sales[i];
float total=0;
for (int i = 0; i < SIZE; ++i)
total+=sales[i];
float average=total/SIZE;
cout<<"Dundaj orlogo: "<<average; }
.
6
352.64
867.70
781.32
867.35
746.21
189.45
=634.11
, .
.
.
.
.
. .
#include <iostream>
using namespace std;
int main()
{
int sar, odor, niit_odor, sariin_odruud[12]={31,28,31,30,31,30,31,31,30,31,30,31};
cout<<endl<<"Sariing oruulna uu (1 to 12):";
cin >>sar;
cout <<"Odriig oruulna uu (1 to 31):";
cin>>odor;
niit_odor=odor;
for (int j=0; j<sar-1; j++)
niit_odor+=sariin_odruud[j];
cout<<"Jiliin ehnees ongorson odriin too : "<<niit_odor; }
(
.). :
,
, total_days .
days_per_month . : 5
4 (31, 28, 31, 30) .
.
.
, .
0 .
.
. ,
.
.
#include <iostream>
using namespace std;
const int CITY = 2;
const int WEEK = 7;
int main()
{
int temperature[CITY][WEEK];
cout <<"Hoer hotiin agaariin temperaturiig oruul"<<endl;
// temperature massivd utgiig oruulj baina
for (int i = 0; i < CITY; ++i)
{
for(int j = 0; j < WEEK; ++j)
{
cout << "Hot " << i + 1 << ", Odor" << j + 1 << " : ";
cin >> temperature[i][j];
}
cout<<endl<<endl<<"Utgiig haruulj baina:"<<endl;
return 0;
}
temperature 2
.
Hoer hotiin agaariin temperaturiig oruul Utgiig haruulj baina:
Hot 1, Odor1 : 33 Hot1, Odor 1 = 33
Hot 1, Odor2 : 30 Hot1, Odor 2 = 30
Hot 1, Odor3 : 29 Hot1, Odor 3 = 29
Hot 1, Odor4 : 28 Hot1, Odor 4 = 28
Hot 1, Odor5 : 35 Hot1, Odor 5 = 35
Hot 1, Odor6 : 29 Hot1, Odor 6 = 29
Hot 1, Odor7 : 37 Hot1, Odor 7 = 37
Hot 2, Odor1 : -20 Hot2, Odor 1 = -20
Hot 2, Odor2 : -15 Hot2, Odor 2 = -15
Hot 2, Odor3 : -19 Hot2, Odor 3 = -19
Hot 2, Odor4 : -25 Hot2, Odor 4 = -25
Hot 2, Odor5 : -30 Hot2, Odor 5 = -30
Hot 2, Odor6 : -35 Hot2, Odor 6 = -35
Hot 2, Odor7 : -33 Hot2, Odor 7 = -33
.
int temperature[CITY][WEEK];
.
, temperature city week
. 3
.
2 .
temperature[i][j]
.
temperature[i][j] .
.
. saleinit .
#include <iostream>
using namespace std;
int main() {
// Ene massiv 12 hurtelh element hadgalna (2x3x2)
int test[2][3][2];
cout << "12 utgiig garaas oruulna uu:"<<endl;
// test massivd utgiig oruulj bn
// 3 davtaltiig ashiglah shaardalagatai
for(int i = 0; i < 2; ++i)
{
for (int j = 0; j < 3; ++j)
{
for(int k = 0; k < 2; ++k )
{
cin >> test[i][j][k];
}
}
}
cout<<endl<<"Displaying Value stored:"<<endl;
return 0;
}
12 utgiig garaas oruulna uu: Displaying Value stored:
1 test[0][0][0] = 1
2 test[0][0][1] = 2
3 test[0][1][0] = 3
4 test[0][1][1] = 4
5 test[0][2][0] = 5
6 test[0][2][1] = 6
7 test[1][0][0] = 7
8 test[1][0][1] = 8
9 test[1][1][0] = 9
10 test[1][1][1] = 10
11 test[1][2][0] = 11
12 test[1][2][1] = 12
--------------------------------
Process exited after 7.738 seconds with return value 0
2 .
. ,
, .
.
#include <iostream>
using namespace std;
void display(int marks[5]);
int main()
{
int marks[5] = {88, 76, 90, 61, 69};
display(marks);
return 0;
}
void display(int m[5])
{
cout << "Delgetsend temdeglegdsen "<< endl;
Delgetsend temdeglegdsen
Oyutnuud 1: 88
Oyutnuud 2: 76
Oyutnuud 3: 90
Oyutnuud 4: 61
Oyutnuud 5: 69
--------------------------------
Process exited after 0.01827 seconds with return value 0
Press any key to continue . . .
#include <iostream>
void display(int n[3][2])
using namespace std;
void display(int n[3][2]); {
int main()
{ cout << "Utguudiig haruullaa:" << endl;
int num[3][2] = { for(int i = 0; i < 3; ++i)
{3, 4}, {
{9, 5}, for(int j = 0; j < 2; ++j)
{7, 1} {
}; cout << n[i][j] << " ";
display(num); }
}
return 0;
}
}
:
Utguudiig haruullaa:
349571
--------------------------------
Process exited after 0.01901 seconds with return value 0
Press any key to continue . . .
do .
max 100 . 100-
?
.
.
,
. ,
.
.
do
.
If (n>=max)
{
cout << \n !!!;
break;
}
.
. C++- .
, .
randomize() .
stdlib.h, time.h 2 .
random()
.
. 0- 51-
random (52) .
,
.
#include <iostream>
using namespace std;
int main()
{
int i, n;
float arr[100];
Toog oruul 1 : 1
Toog oruul 2 : 2
Toog oruul 3 : 9
Toog oruul 4 : 66
Toog oruul 5 : 99
Toog oruul 6 : 100
Toog oruul 7 : 999
Toog oruul 8 : 5
Toog oruul 9 : 6666
Toog oruul 10 : 4897
Toog oruul 11 : 18886
Toog oruul 12 : 95
Toog oruul 13 : 1
Toog oruul 14 : -8
Toog oruul 15 : 5
Hamgiin ih element = 18886
1. ,
2. , 6- , , ,
,
3. U.CS101
4. he2must.blogspot.com
5. https://www.programiz.com/cpp-programming/multidimensional-arrays
6. https://www.programiz.com/cpp-programming/passing-arrays-function
7. https://www.programiz.com/cpp-programming/examples/array-largest-element