# include 
# include - Reja: Massiv. Massiv tushunchasi

|
Reja: Massiv. Massiv tushunchasi
|
bet | 6/6 | Sana | 04.12.2023 | Hajmi | 27,81 Kb. | | #111121 |
Bog'liq 931-22 gruh talabasi Sadullayev Danyor | # include
# include
# include
# include
using namespace std;
int main ()
{ srand ( time (0));
int a[5], b[5], i;
for (i = 0; i < 5; i++) a[i] = rand ( );
for (i = 0; i < 5; i++)
{ b[i] = a[i] + 64;
cout << "b="<
getch ( );
return 0;
}
Izox: tasodifiy sonlar ichida manfiy sonlarning ham qatnashishini ixtiyor etsak,
a[i] = 1050 - rand ( ); yoki a[i] = rand ( )-1000; deb yozish ham mumkin.
6-misol. 2 ta matritsa berilgan. Ularni o’zaro ko’paytirib yangi matritsa hosil qiling. Bu erda 1-matritsaning ustunlar soni 2-matritsaning satrlar soniga teng bo’lishi kerak.
Izox: i– 1-martitsaning ustunlari soni; j– 2-matritsaning satrlari soni;
k– kupaytirishlar soni
# include
# include
# include
# include
using namespace std;
int main ( )
{
{ srand ( time (0));
int a[3][3], b[3][3], c[3][3], i, j, k;
for (i=0; i
a[i][j] = rand ( );
for (i=0; i
b[i][j] = rand ( );
cout<<"\n"; for (i=0; i
{ for (j=0; j
for (k=0; k
c[i][j] = c[i][j] + a[i][k]*b[k][j];
cout<<"C["<cout<
}
cout << endl; }
getch ( );
} }
7-misol. A matritsani B vektorga ko’paytirish algoritmi. S i =
Izoh: matritsaning satrlari soni vektorning satrlariga teng bo’lishi kerak.
M asalan: 1 2 3 1
A = 4 5 6 B = 3
7 8 9 6
C1= 1*1+2*3+3*6 = 25
C2 = 4*1+5*3+6*6 = 55
C3 = 7*1+8*3+9*6 = 85
# include
# include
using namespace std;
int main ( )
{
int a[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, b[3]= {1,3,6}, c[3], i, j;
cout<
for (i=0; i
{ c[i] = 0;
for (j=0; j
c[i] = c[i] + a[i][j] * b[j];
cout <<" C["<
getch ( );
}
8-misol. Matritsani transponirlash algoritmini tuzing. Matritsani transponirlash deb, ustun va satr elementlarini o’zaro o’rin almashtirishga aytiladi, ya`ni Aij = Bji
#
Berilgan matritsa:
1 2 3
4 5 6
7 8 9
Hosil bo’lgan matritsa:
1 4 7
2 5 8
3 6 9
include
# include
using namespace std;
int main ( )
{
int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}}, b[3][3], i, j;
cout<
for ( i=0; i
{ for ( j=0; j
{ b[i][j] = a[j][i];
cout <<" b["<
cout << endl; }
getch ( );
}
9-misol. 3 ta qator va 4 ta ustunga ega A matritsa berilgan. Undagi eng katta elementni va uning indeksini topish, hamda o’sha qatorni massiv shaklida elementlarini ekranga chiqarish dasturini to’zish.
// programma max_indks
# include
# include
using namespace std;
int main ( )
{
int a[3][4]={{1,2,3,4},{4,5,6,7},{7,8,9,10}}, i, j, k, h, max;
int b[4];
max= a[0][0]; cout<
for (i=0; i
{ if ( a[i][j] > max) { max = a[i][j]; k = i; h = j; } }
cout <<" Eng katta element max = "<
cout <<" "<
cout<
cout<<" Hamda o’sha qator elementlari";
for ( j=0; j
{ b[j] = a[k][j]; cout <<" b["<
getch ( );
}
Eng katta element max = 10; 3 - qator va 4 - ustundagi element ekan
Hamda o’sha qarot elementlari b[1]=7 b[2]=8 b[3]=9 b[4]=10
MAVZU: STRUKTURALAR MASSIVI. STRUKTURALARNI FUNKSIYALARGA UZATISH
Reja
Massiv elementlarini saralash masalalari
Struktura massivi. Strukturalarni funksiyalarga uzatish.
Struktura elementlarini uzatish. Butun strukturani uzatish.
10-misol. Saralash masalasi. Massiv elementlarini o’sib borish tartibida saralash algoritmi va dasturini tuzish. (puzirkoviy metod)
Avval 1 ulchovli massiv elementlarini saralashni ko’rib o’tamiz.
# include
# include
using namespace std;
int main( )
{
float a[10], b; int i, j;
for (i = 0; i
{cout<<" a["<>a[i];}
for( j = 0; j< 10; j++)
for ( i = 0; i< 10; i++)
{ if (a[i] > a[i+1])
{ b = a[i];
a[i] = a[i+1];
a[i+1] = b; }
}
cout. precision(3);
cout<<"\n";
for (i = 0; i < 10; i++)
cout <<" b["<
getch ( );
}
MAVZU: STRUKTURALARGA KO’RSATGICH. STRUKTURALARGA KO’RSATGICHNI TAVSIVLASH.
Reja
Ikki o’lchovli massiv elementlarini saralash.
Strukturaga ko’rsatgich.
Strukturaga ko’rsatgichlarni tavsivlash.
Saralash masalasini 0 - 32767 oraliqdagi ixtiyoriy tasodifiy sonlar uchun (rand( ) funktsiyasidan foydalanib ) dasturini tuzamiz
// Saralash masalasi. Massiv elementlarini o’sib borish tartibida
# include
# include
# include
# include
using namespace std;
int main( )
{
srand (time (0));
float a[10], b; int i, j;
for (i = 0; i
a[i] = rand( ) /33.;
for( j = 0; j< 10; j++)
for ( i = 0; i< 10; i++)
{
if (a[i] > a[i+1])
{ b = a[i];
a[i] = a[i+1];
a[i+1] = b; }
}
cout. precision(4);
cout<<"\n";
for (i = 0; i < 10; i++)
cout <<" " <
getch ( );
}
Endi 2 ulchovli massiv elementlarini saralashni ko’ramiz:
Ikki o’lchovli massivlarni saralashda, agar saralashni o’sib borish tartibida qilish kerak bo’lsa, if operatoridagi solishtirish belgisi > bo’lishi kerak, agar kamayish tartibida saralash kerak bo’lsa, solishtirish belgisi < ko’rinishida bo’lishi kerak.
Misol
# include
# include
using namespace std;
int main( )
{
float a[3][3]={{9.1, 8.3, 6.8},{4.8, 5.7, 1.6},{4.7, 3.8, 9.5}};
float b;
int i, j, k;
for ( k=0; k
for ( i=0; i
for ( j=0; j
{ if (a[i][j] > a[i][j+1] )
{ b = a[i][j]; a[i][j] = a[i][j+1]; a[i][j+1] = b; } }
cout<<"\n";
for ( i=0; i
{ for ( j=0; j
cout <<" a="<
getch ( );
}
a=6.8 a=8.3 a=9.1
a=1.6 a=4.8 a=5.7
a=3.8 a=4.7 a=9.5
Bu dastur saralashni qator bo’yicha olib borish uchun ishlatiladi.
Agar saralashni ustun bo’yicha qilish kerak bo’lsa, quyidagicha yozish kerak bo’ladi:
for ( i=0; i< 2; i++)
for ( j=0; j
{ if ( a[i][j] > a[i+1][ j] ) { b= a[i][j]; a[i][j]= a[i+1][ j]; a[i+1][ j]= b; }
11-misol. Matritsaning izini hisoblash dasturini tuzish. Matritsaning izi deb bosh diagonal elementlarining yig’indisiga aytiladi. Shu dasturda teskari (qo’shimcha) diagonal elementlarining yig’indisini ham hisoblashni ko’rib o’tamiz.
// Matritsa (bosh va teskari) diagnal elementlari
// yig'indisini hisoblash dasturi.
# include
# include
using namespace std;
int main ( )
{
float a[3][3] = { {9.1, 8.3, 6.8},
{4.8, 5.7, 1.6},
{4.7, 3.8, 9.5} };
float ds=0, tds=0; int i, j;
for ( i=0; i
ds = ds + a[i][i];
for ( i=0; i
for ( j=0; j
if ( i+j == 2) tds = tds + a[i][j];
cout <<"\n Bosh diagnal elementlari summasi ds= "<
cout <<"\n Teskari diagnal elementlari summasi tds= "< |
getch ( );
return 0;
}
Bosh diagnal elementlari summasi ds= 24.3
Teskari diagnal elementlari summasi tds= 17.2
12-misol. Xar bir hadi an = formulasi orqali hisoblanadigan satr yig’indisini 0,0001 aniqlikda hisoblash dasturini to’zish.
// Har bir hadi An =2n!/(2n+1)! formulasi orqali hisoblanadigan
// satr yig'indisini 0,0001 aniqlikda hisoblash dasturini tuzish.
# include
# include
using namespace std;
int main ( )
{
int n =1; float s1 = 0, s2 = 0;
float p1 =1, p2 = 1;
while (s2 < 0.0001)
{ p1 = p1 * 2*n*(2*n-1); // p1*= 2*n*(2*n-1);
p2 = p2 * (2*n+1)*((2*n+1)-1); // p2*= (2*n+1)*(2*n);
s2 = p1 / p2;
s1 = s1 + s2; // s1+ = s2;
n=n+1; } // n++;
cout. precision (5);
cout<
cout << " s1="<<<"; n=" << n << endl;
getch ( );
}
s1=0.33333; n=2
|
| |