Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Still haven't gotten to the part to do with increasing and decreasing at will yet so have fun figuring that out. :)
- //Dynamically created arrays
- struct Matrix
- {
- int rows;
- int cols;
- double ** Body;
- int ** IntABody;
- bool ** BoolABody;
- void ** VoidABody;
- };
- //Matrix function prototypes
- Matrix BuildMatrix(int rows, int cols); //this must be used with a decaired structure
- void FreeMatrix(Matrix & M);
- Matrix InputMatrix(); //this must be used with a decaired structure
- void PrintMatrix(const Matrix & M);
- Matrix MatrixAddition(const Matrix & M1, const Matrix & M2);
- Matrix MatrixSubtraction(const Matrix & M1, const Matrix & M2);
- Matrix ScalarMultiplication(const Matrix & M, double scalar);
- //Functions defined
- Matrix BuildMatrix(int rows, int cols) //this must be used with a decaired structure
- {
- Matrix M;
- double **f;
- int i = 0, j = 0;
- f = new double * [rows];
- //memory is allocated for the first cell of each row
- if(f == NULL)
- {
- printf("Failed to allocate memory. \n");
- }
- for(; i < rows; ++i)
- {
- //memory is allocated for each column
- *(f + i) = new double[cols];
- if((f + i) == NULL)
- {
- printf("Failed to allocate memory. \n");
- }
- }
- for(i = 0; i < rows; i++)
- {
- for(j = 0; j < cols; j++)
- {
- f[i][j] = 0.0;
- }
- }
- M.rows = rows;
- M.cols = cols;
- M.Body = f;
- return M;
- }
- void FreeMatrix(Matrix & M)
- {
- int i;
- for(i = 0; i < M.rows; i++)
- {
- delete [] M.Body[i];
- }
- delete [] M.Body;
- }
- Matrix InputMatrix() //this must be used with a decaired structure
- {
- int i, j, rows, cols;
- Matrix M;
- printf("Enter the number of rows: ");
- scanf("%d", &rows);
- printf("Enter the number of columns: ");
- scanf("%d", &cols);
- M = BuildMatrix(rows, cols);
- for(i = 0; i < M.rows; i++)
- {
- for(j = 0; j < M.cols; j++)
- {
- printf("Enter M[%d][%d]: ", i, j);
- scanf("%lf",&M.Body[i][j]);
- }
- }
- return M;
- }
- void PrintMatrix(const Matrix & M)
- {
- int i, j;
- for(i = 0; i < M.rows; i++)
- {
- for(j = 0; j < M.cols; j++)
- {
- printf("%lf ", M.Body[i][j]);
- }
- printf("\n");
- }
- }
- Matrix MatrixAddition(const Matrix & M1, const Matrix & M2)
- {
- Matrix M;
- int i, j;
- M = BuildMatrix(M1.rows, M1.cols);
- for(i = 0; i < M.rows; i++)
- {
- for(j = 0; j < M.cols; j++)
- {
- M.Body[i][j] = M1.Body[i][j] + M2.Body[i][j];
- }
- }
- return M;
- }
- Matrix MatrixSubtraction(const Matrix & M1, const Matrix & M2)
- {
- Matrix M;
- int i, j;
- M = BuildMatrix(M1.rows, M1.cols);
- for(i = 0; i < M.rows; i++)
- {
- for(j = 0; j < M.cols; j++)
- {
- M.Body[i][j] = M1.Body[i][j] - M2.Body[i][j];
- }
- }
- return M;
- }
- Matrix ScalarMultiplication(const Matrix & M, double scalar)
- {
- Matrix N;
- int i, j;
- N = BuildMatrix(M.rows, M.cols);
- for(i = 0; i < M.rows; i++)
- {
- for(j = 0; j < M.cols; j++)
- {
- N.Body[i][j] = scalar * M.Body[i][j];
- }
- }
- return N;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement