# include # include # include # include void main ( ) { srand ( time (0)); int a[ 3 ][ 3 ], b[ 3 ][ 3 ], c[ 3 ][ 3 ], i, j, k; for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) a[ i ][ j ] = rand ( ); for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) b[ i ][ j ] = rand ( ); // davomi keyingi varaqda for (i=0; i<3; i++) { for (j=0; j<3; j++) { c [ i ][ j ] = 0; for (k=0; k < 3; k++) c[ i ][ j ] = c[ i ][ j ] + a[ i ] [ k ] * b[ k ][ j ]; cout <<”c=” << c[ i ][ j ] << ”\t”; } cout << endl; } getch ( ); } 5-masala. A matrisani B vektorga ko’paytirish dasturini tuzing. Izoh: matrisaning satrlari soni vektorning satrlariga teng bo’lishi kerak. (xatosini toping!) # include # include # include void main ( ) { int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}}, b[3] = {1,3,6}, i, j; for ( i=0; i < 3; i++) { for ( j=0; j < =3; j++) c[ i ] = c[ j ] + a[ j ][ i ] * b[ j ]; cout <<”c=”<< c[ j ] << endl; } getch ( ); } # include # include //# include void main ( ) { int a[3][3] = {{1,2,3},{4,5,6},{7,8,9}}, b[3] = {1,3,6}, c[3], i, j; for ( i=0; i < 3; i++) { c[i] = 0; for ( j=0; j < 3; j++) c[ i ] = c[ i ] + a[ i ][ j ] * b[ j ]; cout <<”c=”<< c[ i ] << endl; } getch ( ); } Saralash masalasi. Saralash deb massiv elementlarini o’sish yoki kamayish tartibida joylashtirishga aytiladi. Saralashning bir nechta usullari mavjud. 1. Tanlash usuli. Bu usulda massiv elementlari ichidan kichigiga (kattasiga) nisbatan solishtirish orqali amalga oshiriladi. Izoh: dasturini avval bir o’lchovli massiv uchun, keyin 2 o’lchovli massiv uchun ko’rib chiqamiz. # include # include void main() { int a[5]={4,7,10,-20,-6}, i, j, min, b; for (i = 0; i < 5; i++) {min = i; for (j = i+1; j < 5; j++) if ( a[j] > a[min] ) min = j; b = a[i]; a[i] = a[min]; a[min] = b;} for(i = 0; i < 5; i++) cout<<"a="<< a[i] << endl; getch(); } …………. void main() { int a[3][3]={{40,-700,10},{-20,-6,7},{1,2,3}}, i, j, k, min, b; for (i=0; i < 2 ; i++) for (j=0; j < 3; j++) { min = i; for ( k = i+1; k < 3; k++) if (a[k][j] < a[min][j] ) min=k; b = a[i][j]; a[i][j]=a[min][j]; a[min][j] = b;} for (i=0; i < 3; i++) { for (j = 0; j < 3; j++) cout<<"a="< Dastur natijasi:
a=-20
|
a=-700
|
a=3
|
a=1
|
a=-6
|
a=7
|
a=40
|
a=2
|
a=10
| Izoh: 1.Saralash ustun bo’yicha o’sish tartibida amalga oshirildi. Agar kamayish tartibi kerak bo’lsa if ichida > belgisi bo’ladi. 2. Agar saralash qator bo’yicha kerak bo’lsa, j indeks o’zgartiriladi. 2. “Puffakchalar” usuli, ya’ni masssiv elementlaridan eng engili oldinga o’tib, og’irlari keyinga o’tadi. Bu usulda ham saralash ustun yoki qator bo’yicha olib borilishi mumkin. Bizga bitta bo’sh joy kerak bo’ladi (o’rin almashtirish uchun). Masalan: 2ta qator va 4ta ustunga ega massiv berilgan. Uning elementlarini ustun bo’yicha kamayish tartibida saralash dasturini tuzing. void main () { int a[2][4] = {{22, 222, 31, 1}, {5, 56, 9, -1}}, i, j, k, b; for (k=0; k<8; k++) for (i=0; i<1; i++) for (j=0; j<4; 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;} } for (i=0; i<2; i++) { for (j=0; j<4; j++) cout<<"a["< Dastur natijasi:
a[0][0]=22
|
a[0][1]=222
|
a[0][2]=31
|
a[0][3]=1
|
a[1][0]=5
|
a[1][1]=56
|
a[1][2]=9
|
a[1][3]=-1
|
Do'stlaringiz bilan baham: |