Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- void alloc_mem(int **a, int n)
- {
- a = new int *[n];
- for (int i = 0; i < n; i++)
- a[i] = new int[n];
- }
- void del_mem(int **a, int n)
- {
- for (int i = 0; i < n; i++)
- delete[] a[i];
- delete a;
- a = nullptr;
- }
- void init_a(int **a, int n) //A initialization
- {
- cout << "Enter matrix A:\n";
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- cin >> a[i][j];
- }
- void gen_b(int **a, int **b, int n) //B elem generation
- {
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- if (a[i][j] >= 0)
- b[i][j] = a[i][j];
- else
- b[i][j] = 1;
- }
- void sum_c(int** a, int** b, int ** c, int n)//init C
- {
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- c[i][j] = a[i][j] + b[i][j];
- }
- void disp_m(int** m, int n)
- {
- for (int i = 0; i < n; i++){
- for (int j = 0; j < n; j++)
- cout << setw(4) << m[i][j];
- cout << endl;
- }
- }
- void display(int** a, int** b, int** c, int n)
- {
- char ch = 'A';
- int** m_list[3] = {a, b, c};
- for (int k = 0; k < 3; k++){
- cout << "Matrix " << char(ch+k)<< ":\n";
- for(int i = 0; i < n; i++)
- for(int j = 0; j < n; j++)
- disp_m(m_list[k]);
- }
- }
- int main()
- {
- cout << "Enter n: ";
- int n;
- cin >> n;
- int** a, **b, **c;
- alloc_mem(a, n);
- alloc_mem(b, n);
- alloc_mem(c, n);
- init_a(a, n);
- gen_b(a, b, n);
- sum_c(a, b, c, n);
- display(a, b, c, n);
- del_mem(a, n);
- del_mem(b, n);
- del_mem(c, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement