📅 Date: Jan 14, 2026
🔥 Topic: 2D Arrays Input & Matrix Addition
🔄 The Nested Loop
To travel through a 2D Array, we need two pointers:
i = Controls the Row.
j = Controls the Column.
💻 Day 14 Code: Adding Two Matrices
Today I built a program that takes two 2x2 matrices and adds them together.
#include <iostream>
using namespace std;
int main() {
int A[2][2] = {{1, 2}, {3, 4}};
int B[2][2] = {{5, 6}, {7, 8}};
int Sum[2][2];
cout << "Sum Matrix:" << endl;
// Outer Loop (Rows)
for(int i=0; i<2; i++) {
// Inner Loop (Cols)
for(int j=0; j<2; j++) {
Sum[i][j] = A[i][j] + B[i][j];
cout << Sum[i][j] << " ";
}
cout << endl; // New line after every row
}
return 0;
}
💠Thoughts
Nested loops can be confusing at first. The inner loop runs completely for every single step of the outer loop. This is crucial for image processing and game grids.
No comments:
Post a Comment