├── .gitattributes
├── images
├── r1.png
└── r2.png
├── .github
└── FUNDING.yml
├── LICENSE
├── README.md
└── Matrix.cpp
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
--------------------------------------------------------------------------------
/images/r1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harismuneer/Matrix-Operations-Using-Pointer-Arithmetic/HEAD/images/r1.png
--------------------------------------------------------------------------------
/images/r2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/harismuneer/Matrix-Operations-Using-Pointer-Arithmetic/HEAD/images/r2.png
--------------------------------------------------------------------------------
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
4 | patreon: # Replace with a single Patreon username
5 | open_collective: # Replace with a single Open Collective username
6 | ko_fi: # Replace with a single Ko-fi username
7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
9 | liberapay: # Replace with a single Liberapay username
10 | issuehunt: # Replace with a single IssueHunt username
11 | otechie: # Replace with a single Otechie username
12 | custom: ['https://www.cyfylabs.com']
13 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 harismuneer
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 💾 Matrix-Operations-Using-Pointer-Arithmetic
2 |
3 |
4 |
5 | [](#)
6 | [](https://www.github.com/harismuneer/Matrix-Operations-Using-Pointer-Arithmetic/fork)
7 | [](https://www.github.com/harismuneer/Matrix-Operations-Using-Pointer-Arithmetic/issues)
8 | [](#)
9 |
10 |
11 | A program that takes two matrices from user and performs following operations:
12 | - Matrix Addition
13 | - Transpose of a matrix
14 | - Checks if a matrix is symmetric or not
15 | - Interchange rows of a matrix
16 |
17 | ## Functionality
18 |
19 | 
20 | 
21 |
22 |
23 |
24 |
25 |
26 | ## Author
27 | You can get in touch with me on my LinkedIn Profile: [](https://www.linkedin.com/in/harismuneer)
28 |
29 | You can also follow my GitHub Profile to stay updated about my latest projects: [](https://github.com/harismuneer)
30 |
31 | If you liked the repo then kindly support it by giving it a star ⭐ and share in your circles so more people can benefit from the effort.
32 |
33 |
34 | ## Contributions Welcome
35 | [](https://www.github.com/harismuneer/Matrix-Operations-Using-Pointer-Arithmetic/issues)
36 |
37 | If you find any bugs, have suggestions, or face issues:
38 |
39 | - Open an Issue in the Issues Tab to discuss them.
40 | - Submit a Pull Request to propose fixes or improvements.
41 | - Review Pull Requests from other contributors to help maintain the project's quality and progress.
42 |
43 | This project thrives on community collaboration! Members are encouraged to take the initiative, support one another, and actively engage in all aspects of the project. Whether it’s debugging, fixing issues, or brainstorming new ideas, your contributions are what keep this project moving forward.
44 |
45 | With modern AI tools like ChatGPT, solving challenges and contributing effectively is easier than ever. Let’s work together to make this project the best it can be! 🚀
46 |
47 | ## License
48 | [](../master/LICENSE)
49 |
50 | Copyright (c) 2018-present, harismuneer
51 |
52 |
53 |
54 |
55 |
56 |
57 | Hey there, I'm Haris Muneer 👨🏻💻
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 | - 🛠️ Product Builder: Agile Product Manager with 5+ years of hands-on experience delivering SaaS solutions across sales, recruiting, AI, social media, and public sector domains. Background in Computer Science, with a proven track record of scaling products from inception to $XXM+ ARR, launching 3 top-ranking tools on Product Hunt, and developing solutions adopted by 250+ B2B clients in 40+ countries.
67 |
68 | - 🌟 Open Source Advocate: Passionate about making technology accessible, I’ve developed and open-sourced several software projects for web, mobile, desktop, and AI on my GitHub profile. These projects have been used by thousands of learners worldwide to enhance their skills and knowledge.
69 |
70 | - 📫 How to Reach Me: To learn more about my skills and work, visit my LinkedIn profile. For collaboration or inquiries, feel free to reach out via email.
71 |
72 |
73 |
74 | 🤝 Follow my journey
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
--------------------------------------------------------------------------------
/Matrix.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | using namespace std;
3 |
4 | int** AllocateMatrix (int ROWS, int COLS);
5 | int** InputMatrix ( int& ROWS, int& COLS);
6 | void OutputMatrix (int** matrix, const int ROWS, const int COLS);
7 | int** AddMatrix(int** matrixA, int** matrixB, const int ROWS, const int COLS);
8 | int** TransposeMatrix(int** matrix, const int ROWS, const int COLS);
9 | bool IsSymmetric(int** matrix, const int ROWS, const int COLS);
10 | void InterchangeRows(int** matrix, const int ROWS, const int COLS );
11 | void InterchangeRows( int* &row1, int* &row2 );
12 | void deAllocateMatrix(int** &matrix, const int ROWS);
13 |
14 |
15 |
16 | int** AllocateMatrix (int ROWS, int COLS)
17 | {
18 | //Code to Allocate a 2D Matrix
19 | int** matrix = new int* [ROWS];
20 |
21 | for ( int i = 0 ; i < ROWS ; i++)
22 | matrix[i] = new int [COLS];
23 |
24 | return matrix;
25 | }
26 |
27 |
28 | int** InputMatrix ( int& ROWS, int& COLS)
29 | {
30 | cout << "Total Number of Rows : ";
31 | cin >> ROWS ;
32 |
33 | while ( ROWS <=0 )
34 | {
35 | cout <<"You have entered an invalid number . Enter the Rows again : " << endl;
36 | cin >> ROWS;
37 | }
38 |
39 | cout << "Total Number of Columns : ";
40 | cin >> COLS ;
41 |
42 | while ( COLS<=0 )
43 | {
44 | cout <<"You have entered an invalid number . Enter the Columns again : " << endl;
45 | cin >> COLS;
46 | }
47 |
48 |
49 | int** matrix = NULL;
50 |
51 | matrix = AllocateMatrix (ROWS , COLS);
52 |
53 |
54 | //Code to input numbers in the 2D Matrix
55 | cout <> *colPtr;
65 | }
66 |
67 | return matrix;
68 | }
69 |
70 |
71 | void OutputMatrix (int** matrix, const int ROWS, const int COLS)
72 | {
73 | int** endRowPtr = matrix + ROWS;
74 |
75 | for(int** rowPtr = matrix ; rowPtr < endRowPtr ; rowPtr++ )
76 | {
77 | int* endColPtr = *rowPtr + COLS;
78 |
79 | cout << endl;
80 |
81 | for(int* colPtr = *rowPtr ; colPtr < endColPtr ; colPtr++)
82 | cout << *colPtr << " ";
83 |
84 | }
85 | cout << endl;
86 | }
87 |
88 |
89 | int** AddMatrix(int** matrixA, int** matrixB, const int ROWS, const int COLS)
90 | {
91 | //Code to allocate the third Matrix i.e. Matrix C
92 | int** matrixC = NULL;
93 | matrixC = AllocateMatrix (ROWS , COLS);
94 |
95 | int* colPtrA = NULL ; int* colPtrB = NULL ; int* colPtrC = NULL;
96 | int* endColPtrA = NULL ; int* endColPtrB = NULL ; int* endColPtrC = NULL;
97 |
98 | int** rowPtrA = matrixA;
99 | int** rowPtrB = matrixB;
100 | int** rowPtrC = matrixC;
101 | int** endRowPtrA = matrixA + ROWS;
102 | int** endRowPtrB = matrixB + ROWS;
103 | int** endRowPtrC = matrixC + ROWS;
104 |
105 | while( (rowPtrA < endRowPtrA) && (rowPtrB < endRowPtrB) && (rowPtrC < endRowPtrC) )
106 | {
107 | endColPtrA = *rowPtrA + COLS;
108 | endColPtrB = *rowPtrB + COLS;
109 | endColPtrC = *rowPtrC + COLS;
110 |
111 | colPtrA = *rowPtrA;
112 | colPtrB = *rowPtrB;
113 | colPtrC = *rowPtrC;
114 |
115 | while( (colPtrA < endColPtrA) && (colPtrB < endColPtrB) && (colPtrC < endColPtrC) )
116 | {
117 | *colPtrC = *colPtrA + *colPtrB;
118 |
119 | colPtrA++;
120 | colPtrB++;
121 | colPtrC++;
122 | }
123 |
124 | rowPtrA++;
125 | rowPtrB++;
126 | rowPtrC++;
127 | }
128 |
129 | return matrixC;
130 | }
131 |
132 |
133 | int** TransposeMatrix(int** matrix, const int ROWS, const int COLS)
134 | {
135 | //Allocate the memory of the Transpose Matrix dynamically on Heap
136 | int** matrixD = NULL;
137 | matrixD = AllocateMatrix (COLS , ROWS);
138 |
139 | int** rowPtr = matrix;
140 | int* colPtr = *rowPtr ;
141 | int** endRowPtr = matrix + ROWS;
142 | int* endColPtr = *rowPtr + COLS;
143 | int i = 0;
144 | int** rowPtrD = matrixD;
145 | int* colPtrD = *rowPtrD;
146 | int** endRowPtrD = matrixD + COLS;
147 | int* endColPtrD = *rowPtrD + ROWS;
148 |
149 | while ( rowPtrD < endRowPtrD )
150 | {
151 | rowPtr = matrix;
152 | colPtrD = *rowPtrD;
153 |
154 | while ( rowPtr < endRowPtr )
155 | {
156 | colPtr = *rowPtr + i;
157 | *colPtrD = *colPtr;
158 |
159 | colPtrD++;
160 | rowPtr++;
161 | }
162 |
163 | i++;
164 | rowPtrD++;
165 | }
166 |
167 | return matrixD;
168 | }
169 |
170 |
171 | bool IsSymmetric(int** matrix, const int ROWS, const int COLS)
172 | {
173 | bool isSymmetric = true;
174 |
175 | for (int i = 0 ; i < ROWS && isSymmetric ; i++)
176 | {
177 | for (int j = 0 ; j < COLS && isSymmetric ; j++)
178 | {
179 | if ( matrix[i][j] != matrix[j][i] )
180 | isSymmetric = false;
181 | }
182 | }
183 |
184 | return isSymmetric;
185 | }
186 |
187 |
188 | void InterchangeRows(int** matrix, const int ROWS, const int COLS )
189 | {
190 | int rowOne , rowTwo = 0;
191 |
192 | cout << endl << "Enter row1 : ";
193 | cin >> rowOne;
194 |
195 | while( rowOne <= 0 || rowOne > ROWS)
196 | {
197 | cout << "You have entered an invalid Row Number . Enter a valid Row1 :"<< endl;
198 | cin >> rowOne;
199 | }
200 |
201 | cout << "Enter row2 : ";
202 | cin >> rowTwo;
203 |
204 | while( rowTwo <= 0 || rowTwo > ROWS)
205 | {
206 | cout << "You have entered an invalid Row Number . Enter a valid Row2 :"<< endl;
207 | cin >> rowTwo;
208 | }
209 |
210 | int* row1 = *( matrix + (rowOne - 1) );
211 | int* row2 = *( matrix + (rowTwo - 1) );
212 |
213 | InterchangeRows (row1 , row2);
214 |
215 | *( matrix + (rowOne - 1) ) = row1;
216 | *( matrix + (rowTwo - 1) ) = row2;
217 |
218 | }
219 |
220 |
221 | void InterchangeRows( int* &row1, int* &row2 )
222 | {
223 | int* temp = NULL;
224 |
225 | temp = row1;
226 | row1 = row2;
227 | row2 = temp;
228 | }
229 |
230 |
231 | void deAllocateMatrix(int** &matrix, const int ROWS)
232 | {
233 | if(matrix)
234 | {
235 | for ( int i = 0 ; i < ROWS ; i++)
236 | delete[] matrix[i];
237 |
238 | delete[] matrix;
239 | }
240 |
241 | matrix = NULL;
242 | }
243 |
244 |
245 |
246 | int main()
247 | {
248 | int rowsA, colsA, rowsB, colsB = 0 ;
249 | int** matrixA = NULL;
250 | int** matrixB = NULL;
251 | int** matrixC = NULL; //Matrix after addition of Matrix A and Matrix B
252 | int** matrixAT = NULL; //Transpose of Matrix A
253 | int** matrixBT = NULL; //Transpose of Matrix B
254 |
255 | //Code to call Input And Output Functions to get Matrices from user and display them
256 | cout << "Enter Matrix A : " << endl;
257 | matrixA = InputMatrix (rowsA , colsA);
258 |
259 | cout << endl <<"Enter Matrix B : " << endl;
260 | matrixB = InputMatrix (rowsB, colsB);
261 |
262 | cout << endl << "Matix A = ";
263 | OutputMatrix (matrixA , rowsA , colsA);
264 |
265 | cout << endl << "Matrix B = ";
266 | OutputMatrix (matrixB , rowsB , colsB);
267 |
268 |
269 | /////Code to call the AddMatrix Function and Display the resultant Matrix i.e MatrixC
270 | if ( (rowsA == rowsB) && (colsA == colsB) )
271 | {
272 | matrixC = AddMatrix (matrixA, matrixB ,rowsA ,colsA); // becuase rowsA and rowsB are same and colsA and colsB are same.
273 | cout << endl << "A + B = " ;
274 | OutputMatrix (matrixC , rowsA , colsA); //becuase matrix C will have the same rows and columns as that of Matrix A and Matrix B
275 | }
276 | else
277 | cout <