Pages

Friday, April 18, 2025

My Mistakes from Matrix Pointers (2D Arrays)

 while solving a problem in coding, and i forgot one thing:- how to calculate how many times it have occured a specific value in the array in a matrix , so does it mean as my teacher said to analyze and identify our weakness did i lack "logic?, or lacked practice?, i didnt knew the concept or forgot it ?, or whatever the case is" tell me please, (my CHATGPT Prompt)



Question 1 Mistakes-

SkillStatusWhy
Logic✅ Present
You started the solution, thought in steps, and assigned variables. That's logic.

Practice🚧 Needs Improvement
You likely forgot a small technique because you haven’t repeated it enough yet. Totally normal.

Concept Knowledge✅ Basic understanding
You know enough to attempt the solution. You’re not clueless—you’re learning.

Memory/Recall❗ Momentarily weak
You likely knew it once but couldn’t remember the exact syntax or loop pattern in that moment.



πŸ” So to directly answer you:

No, you didn’t lack logic.
You didn’t completely forget the concept either.
What happened is: you’re still new, and you lacked practice recalling it on the spot.

That’s it. And that’s fixable with 15–20 mins of focused revision.


Concept revision-

#include <iostream>

using namespace std;

// matrix pointers in functions - assignment questions

// goal- to find how many times 7 occured in the array 


// best approach is used in this problem - staircase search method O(n+m)


bool search(int mat[][4], int n, int m, int key) {

    int i = 0, j = m-1; 

    

    while(i < n && j >= 0) {

        if(mat[i][j] == key) {

            cout << "found at cell (" << i << "," << j << ")\n";

            return true;

        } else if(mat[i][j] < key) {

            // down

            i++;

        } else {

            // left 

            j--;

        }

    }

    

    cout << "key not found\n";

    return false;

}


int main(){

    int matrix[4][4] = {

        {10, 20, 30, 40},

        {15, 25, 35, 45},

        {27, 29, 37, 48},

        {32, 33, 39, 50}

    };

    

    search(matrix, 4, 4, 33);


    return 0;

}




Question 2 Mistakes/Learnings:-

πŸ’‘ What I Lacked in My 2D Array Code (C++)

While writing a simple program to calculate the sum of the 2nd row in a 2D array, I initially made some mistakes. I later fixed them by improving my logic and focusing more on how 2D arrays actually work.

Here’s what I learned from my mistake:


❌ What I Did Wrong Initially

  • Used i for rows, thinking it would help me sum the 2nd row. But I was iterating over rows instead of columns.

  • Tried something like if (arr[i] == 1) to check if I’m on the 2nd row. This doesn’t work because:

    • arr[i] is a whole 1D array (a row), and comparing it to 1 doesn’t make sense.

  • My loop structure and logic were flawed due to misunderstanding how 2D arrays are accessed in C++.


✅ What I Did Right Later

  • Fixed the row index to 1 (i.e. arr[1][j]) and looped over columns (j) to sum the second row.

  • Kept the total variable and summing logic inside the nums function for cleaner design.

  • Focused on column-wise traversal within a fixed row, which is key when working with matrices.


πŸ“Œ What I Was Lacking

AreaReflection
PracticeNeeded more hands-on with nested loops and how 2D arrays work.
LogicThe core idea was there: I wanted the 2nd row. But my implementation logic was off.
ApproachI initially approached it with row iteration instead of directly targeting the row index and iterating over columns.

πŸ” Key Takeaway

To access an entire row in a 2D array:
Fix the row index, loop through columns: arr[fixed_row][j]

To access a column:
Fix the column index, loop through rows: arr[i][fixed_col]


✅ Final Code That Worked

void nums(int arr[][3], int n, int m) { int total = 0; for(int j = 0; j < m; j++) { total += arr[1][j]; } cout << "Total Sum of 2nd Row : " << total << endl; }

I’ll keep practicing more problems on 2D arrays and update my blog with what I learn, so I don't repeat the same mistakes. Small tweaks in logic and approach can make a big difference.

— Debadatta Patra


QUESTION 3:-(LEANRINGS)

❌ What You Lacked:

  1. Wrong logic – You used if(arr[i][j] == arr[j][i]) – not needed.

  2. Used int transpose like an array – but it’s just an int, not a matrix.

  3. Tried to print a matrix with cout << transpose – doesn't work like that.

  4. Tried in-place transpose on a non-square matrix – not possible, need a new matrix.


✅ What You Should Do:

  • Remove the if condition.

  • Use a new 2D array to store the transposed matrix.

  • Loop through and print the matrix properly.



⚠️ 1. Misunderstood what "transpose" actually means

You did:

cpp
if(arr[i][j] == arr[j][i])

⬇️
That’s checking if the element and its transpose are equal, which isn’t required.

✅ Fix: Transposing means swapping arr[i][j] with arr[j][i]no condition needed.


⚠️ 2. Used an undefined variable as if it were a 2D array

You wrote:

cpp
int transpose; arr[i][j] = transpose[j][i];

Issues:

  • transpose is just an int, not an array.

  • You tried to use it like a matrix → transpose[j][i] → ❌ invalid.


⚠️ 3. Wrong output method

You wrote:

cpp
cout << "transposed array : " << transpose << endl;

Problems:

  • transpose is not a matrix here — it’s just an undefined scalar.

  • Even if it were a matrix, you can't print it like that.


⚠️ 4. Tried to transpose a non-square matrix in-place

You declared a 2x3 matrix:

cpp
int arr[2][3] = { {3, 4, 5}, {6, 7, 8} };

And tried to modify arr in place, which won’t work because the transposed matrix has different dimensions: it should be 3x2.

✅ Fix: Create a new matrix to store the transpose.


✅ In Short — What You Missed:

MistakeWhy it’s wrongFix
Used if(arr[i][j] == arr[j][i])Transpose doesn’t need conditionalsJust assign directly
Used int transpose;Not a matrixDefine int transpose[m][n];
Used transpose[j][i]Used a scalar like an arrayDeclare a 2D array
Tried to do in-place transpose of 2x3 matrixCan’t do in-place for non-squareUse another matrix
Printed matrix like a variableInvalid output formatLoop over the matrix to print

Correct Code: #include <iostream>

using namespace std;




void transpose(int arr[][3], int n, int m) {

    

    // lacked this part of code

    int transposed[3][2]; // since original is 2x3, transposed will be 3x2

    

    // rows

    for(int i=0; i<n; i++) {

        // columns

        for(int j=0; j<m; j++) {

            transposed[j][i] = arr[i][j];

        }

    }

    

    cout << "transposed array : " << endl;

    

    // lacked this part of logic

    for(int i=0; i<m; i++) {

        for(int j=0; j<n; j++) {

            cout << transposed[i][j] << " ";

        }

        cout << endl; // for 3x2 positioning

    }

}


int main() {

    int arr[2][3] = {

        {3, 4, 5}, 

        {6, 7, 8}

    };


    transpose(arr, 2, 3);

    

    return 0;

}  

No comments:

Post a Comment

Stack using Linked List – Partial (University Exam Topic)

 #include <iostream> using namespace std; struct Node {     int data;     Node* next; }; class Stack {     Node* top; public:     Stac...