Pages

Friday, April 25, 2025

2D Dynamic Arrays in C++

#include <iostream>

using namespace std;


int main()

{

    int row, cols;

    cout << "enter rows: ";

    cin >> row;

    

    cout << "enter cols: ";

    cin >> cols;

    

    int* *matrix = new int*[row];

    

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

        matrix[i] = new int[cols];

    }

    

    // data store

    int x = 1;

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

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

            matrix[i][j] = x;

            x++;

            

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

        }

        

        cout << endl;

    }

    

    cout << matrix[2][2] << endl;

    cout << *( *(matrix+2) + 2) << endl;


    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...