-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path02_matrix.cpp
76 lines (61 loc) · 2.07 KB
/
02_matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// // Create a program that dynamically allocates a 2D array of doubles. Allow the user to input the dimensions, populate the matrix, and then compute and display the transpose of the matrix. Use delete to free the memory.
// // Header files
#include <iostream>
#include <stdlib.h>
#include <iomanip>
// // use namespace
using namespace std;
#define MAX_ROWS 10
#define MAX_COLS 10
// // Main Function Start
int main()
{
int rows, cols;
cout << "\nEnter Order of Matrix (ROWS X COLS) (MAX_ROWS " << MAX_ROWS << ", MAX_COLS " << MAX_COLS << ") => ";
cin >> rows >> cols;
// // invalid order
if (rows < 1 || rows > MAX_ROWS || cols < 1 || cols > MAX_COLS)
{
cout << "\n!!! Invalid Order..." << endl;
exit(0);
}
// // create a 2d array of double dynamically
double **matrix = new double *[rows]; // array of pointers (outer array)
for (int i = 0; i < rows; i++)
{
matrix[i] = new double[cols]; // array of type double (inner array)
}
// // Input Elements of Matrix
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << "\nEnter Element[" << i + 1 << "][" << j + 1 << "] => ";
cin >> matrix[i][j];
}
}
// // Print Elements of Matrix
cout << "\n\n>>>>>>>> Matrix of " << rows << " X " << cols << " <<<<<<<<<\n";
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
cout << left << setw(4) << matrix[i][j];
cout << endl; // // Add New line
}
// // Transpose of Matrix
cout << "\n\n>>>>>>>> Transpose of Matrix <<<<<<<<<\n";
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < rows; j++)
cout << left << setw(4) << matrix[j][i];
cout << endl; // // Add New line
}
// // deallocate dynamically allocated memory
for (int i = 0; i < rows; i++) // To delete the inner arrays
delete[] matrix[i];
delete[] matrix; // To delete the outer array
cout << endl; // Add new line
cin.ignore();
return 0;
}
// // Main Function End