|
Matritsadagi har bir satrning eng kattasini topish
|
bet | 4/6 | Sana | 26.01.2024 | Hajmi | 0,98 Mb. | | #146468 |
Bog'liq Matritsalarni tashkil etish Matritsadagi har bir satrning eng kattasini topish
#include #include void matrix_print(int a[10][10], int m, int n)
{
// matritsani jadval shaklida chiqarish for (int i = 0; i < m; i++)
{ for (int j = 0; j < n; j++)
{ cout << a[i][j] << "\t";
} cout << "\n";
} } int satr_max(int a[], int n)
{
// massivning eng katta elementini aniqlash int max = a[0]; for (int i = 1; i < n; i++) if (max < a[i]) max = a[i]; return max; } int main() { int m, n, a[10][10]; cout << "Satrlar sonini kiriting \nm="; cin >> m; cout << "Ustunlar sonini kiriting \nn="; cin >> n; cout <<"Massiv elementlarini kiriting \n"; for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) cin >> a[i][j]; cout << "Kiritilgan matritsa\n";
//funksiyaga matritsa, satrlar va ustunlar soni jo'natiladi matrix_print(a, m, n); for (int i = 0; i < m; i++)
{
// funksiyaga i-satrning 0-elementi adresini
// va elementlar sonini jo'natamiz cout << i << "-satrning eng kattasi=" << satr_max(&a[i][0], n); cout << endl;
} return 0; system ("pause");
} a(m, n) matritsani b(n, p) matritsaga ko'paytirishdan hosil bo'lgan
c(m, p) matritsani chiqaruvchi programma tuzilsin.
#include #include void matrix_print(int **a, int m, int n)
{
// matritsani jadval shaklida chiqarish for (int i = 0; i < m; i++)
{ for (int j = 0; j < n; j++)
{ cout << a[i][j] << "\t";
} cout << "\n";
}
}
void matrix_input (int **a, int m, int n)
{ for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) cin >> a[i][j];
} void matrix_delete (int **a, int m)
{
// Dinamik massivdan foydalanib bo'lgandan so'ng
// uni xotiradan o'chirish lozim
// oldin massiv satrlari o'chiriladi (xotira bo'shatiladi) for (int i = 0; i < m; i++) delete []a[i];
// endi massivning o'zini o'chirish mumkin delete []a;
}
// create_matrix - ko'rsatkichga ko'rsatkich qaytaradi int ** create_matrix(int m, int n)
{ int **ptr;
// matritsa satrlari uchun xotira ajratish ptr = new int *[m]; for (int i = 0; i < m; i++)
// matritsa ustunlari uchun xotira ajratish ptr[i] = new int [n]; return ptr; } int main() { int m, n, p, **a, **b, **c; cout << "A matritsa satrlar sonini kiriting \nm="; cin >> m; cout << "A matritsa ustunlar sonini kiriting \nn="; cin >> n; cout << "B matritsa ustunlar sonini kiriting \np="; cin >> p;
// matritsalarni hosil qilish a = create_matrix(m, n); b = create_matrix(n, p); c = create_matrix(m, p); cout << "A massiv elementlarini kiriting \n"; matrix_input(a, m, n); cout << "B massiv elementlarini kiriting \n"; matrix_input(b, n, p); cout << "Kiritilgan A matritsa\n"; matrix_print(a, m, n); cout << "Kiritilgan B matritsa\n"; matrix_print(b, n, p);
// c matritsa elementlarini 0 qilish for (int i = 0; i < m; i++) for (int j = 0; j < p; j++) c[i][j] = 0;
// A matritsani B matritsaga ko'paytirish for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) for (int k = 0; k < p; k++)
{ c[i][k] += a[i][j] * b[j][k];
} cout << "Natija C matritsa\n"; matrix_print(c, m, p); matrix_delete(a, m); // a matritsani o'chirish matrix_delete(b, n); // b matritsani o'chirish matrix_delete(c, m); // c matritsani o'chirish return 0; system ("pause");
}
|
| |