Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication22.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "iostream"
- #include "stdio.h"
- #include "stdlib.h"
- #include "time.h"
- #include "iomanip"
- using namespace std;
- int getRandomInt(int min, int max);
- void randomMatrix(int **matrix, int rows, int cols, int min, int max);
- void displayMatrix(int **m, int rows, int cols);
- void addMatrices(int **m1, int **m2, int rows, int cols);
- int main()
- {
- srand(time(NULL));
- int min = -5, max = 5;
- int rows, cols;
- cout << "Podaj wymiary macierzy : ";
- cin >> cols >> rows;
- cout << endl << "Podaj min max: ";
- cin >> min >> max;
- int *m1 = NULL;
- int *m2 = NULL;
- randomMatrix(&m1, rows, cols, min, max);
- randomMatrix(&m2, rows, cols, min, max);
- displayMatrix(&m1, rows, cols);
- displayMatrix(&m2, rows, cols);
- addMatrices(&m1, &m2, rows, cols);
- displayMatrix(&m1, rows, cols);
- return 0;
- }
- void displayMatrix(int **m, int rows, int cols)
- {
- int cellSize = 4;
- for (int i = 0; i < rows; i++)
- {
- for (int x = 0; x < cols; x++)
- {
- cout << setw(cellSize);
- cout << *(*m + i*cols +x);
- }
- cout << endl;
- }
- cout << endl;
- }
- int getRandomInt(int min, int max)
- {
- return rand() % (max - min + 1) + min;
- }
- void randomMatrix(int **matrix, int rows, int cols, int min, int max)
- {
- int *m;
- m = (int*)calloc(rows*cols, sizeof(int));
- for (int i = 0;i < rows;i++)
- {
- for (int x = 0;x < cols;x++)
- {
- *(m + i * cols + x) = getRandomInt(min, max);
- }
- }
- *matrix = (int*)realloc(m,rows*cols*sizeof(int));
- }
- void addMatrices(int **m1, int **m2,int rows,int cols)
- {
- for (int i = 0;i < rows;i++)
- {
- for (int x = 0;x < cols;x++)
- {
- *(*m1+i*cols+x) += (int)*(*m2 + i * cols + x);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement