#include "pch.h" #include #include #include using namespace std; void utworz(double **tab,int m,int n) { tab = new double*[m]; for (int i = 0; i < m; i++) { tab[i] = new double[n]; } for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { tab[i][j] = 0.0; } } } void zapisz(double **tab, int m, int n) { cout << m << "/n" << n; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cout << tab[i][j] << "/t"; } cout << endl; } } void usun(double **tab, int m) { for (int i = 0; i < m; i++) { delete tab[i]; } delete[]tab; } bool czytaj(double **tab, int m, int n, double **tab1, int p, int q) { cout << "(funkcja czytaj) Podaj rozmiary macierzy"; cin >> m >> n >> p >> q; tab = new double*[m]; tab1 = new double*[p]; for (int i = 0; i < m; i++) { tab[i] = new double[n]; } for (int i = 0; i < p; i++) { tab[i] = new double[q]; } cout << " wczytaj elementy macierzy A" << endl; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) cin >> tab[i][j]; cout << " wczytaj elementy macierzy B" << endl; for (int i = 0; i < p; i++) for (int j = 0; j < q; j++) cin >> tab1[i][j]; return true; } bool czytaj_losowo(double **tab, int m, int n, double **tab1, int p, int q) { cout << "(funkcja czytaj) Podaj rozmiary macierzy"; cin >> m >> n >> p >> q; tab = new double*[m]; tab1 = new double*[p]; for (int i = 0; i < m; i++) { tab[i] = new double[n]; } for (int i = 0; i < p; i++) { tab[i] = new double[q]; } cout << " wczytaj elementy macierzy A" << endl; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) tab[i][j] = rand() % 100; cout << " wczytaj elementy macierzy B" << endl; for (int i = 0; i < p; i++) for (int j = 0; j < q; j++) tab[i][j] = rand() % 100; return true; } bool suma(double **A, int m, int n, double **B, int p, int q, double **C) { if (m != p || n != q) return false; C = new double *[m]; for (int i = 0; i < m; i++) C[i] = new double[n]; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) C[i][j] = A[i][j] + B[i][j]; return true; } bool roznica(double **A, int m, int n, double **B, int p, int q, double **C) { if (m != p || n != q) return false; C = new double *[m]; for (int i = 0; i < m; i++) C[i] = new double[n]; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) C[i][j] = A[i][j] - B[i][j]; return true; } bool transponowanie(double **A, int m, int n, double **C) { C = new double*[n]; for (int i = 0; i < n; i++) { C[i] = new double[m]; } for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) C[j][i] = A[i][j]; return true; } int main() { srand(time(NULL)); double **A = 0, **B = 0, **C =0; unsigned int n = 2, m = 3, p = 3, q = 2; if (czytaj_losowo(A, m, n, B, p, q)) { zapisz(A, m, n); zapisz(B, p, q); } if (czytaj(A, m, n, B, p, q)) { zapisz(A, m, n); zapisz(B, p, q); if (!suma(A, m, n, B, p, q, C)) { cerr << "Macierze maja nieprawidlowe wymiary - suma niemozliwa" << endl; } else { cout << "suma macierzy" << endl; zapisz(C, m, n); } if (!roznica(A, m, n, B, p, q, C)) { cerr << "Macierze maja nieprawidlowe wymiary - roznica niemozliwa" << endl; } else { cout << "roznica macierzy" << endl; zapisz(C, m, n); } /*if (!iloczyn(A, m, n, B, p, q, C)) { cerr << "Macierze maja nieprawidlowe wymiary - iloczyn niemozliwy" << endl; } else { cout << "iloczyn macierzy" << endl; zapisz(C, m, q); }*/ cout << "macierz przed transponowaniem" << endl; zapisz(B, p, q); cout << "macierz transponowana" << endl; transponowanie(B, p, q, C); zapisz(C, q, p); } }