0% found this document useful (0 votes)
6 views

Sample Dynamic Array With Multiple Columns and Rows

Dynamic array

Uploaded by

marcusgarxvy
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Sample Dynamic Array With Multiple Columns and Rows

Dynamic array

Uploaded by

marcusgarxvy
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>

using namespace std;

//Function to display the array


void display(int** a, int r, int c){
for(int i=0; i<r; i++){
for(int j=0; j<c; j++){
cout << a[i][j] << ' ';
}
cout << endl;
}
}

int main()
{
int** array;
int row, col, i, j;

cout << "Enter the number of rows" << endl;


cin >> row;
cout << "Enter the number of columns" << endl;
cin >> col;

//Dynamically allocating row space in heap


array = new int*[row];
//Dynamically allocating column space in heap
for(i=0; i<row; i++){
array[i] = new int[col];
}

//Taking input in the array


cout << "Enter "<< (row * col) <<" numbers \n";
for(i=0; i<row; i++){
for(j=0; j<col; j++){
cout << "Enter element at "<< i+1 << " row " << j+1 << " column"<<
endl;
cin >> array[i][j];
}
}

//Displaying array
cout << "Matrix is: \n";
display(array, row, col);

//Free space after the use of array


delete [] array;
}

You might also like