-
-
Save mindfulme/6257a36d6719af2ce5619593c9af3ba1 to your computer and use it in GitHub Desktop.
C++ Triangular Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
int main(void) | |
{ | |
int rows = 5, cols = 5; | |
int **arr; | |
// allocate and initialize the array | |
arr = new int * [rows]; | |
for (int r = 0; r < rows; r++) { | |
arr[r] = new int[r + 1]; | |
for(int c = 0; c <= r; c++) | |
arr[r][c] = (r + 1) * 10 + c + 1; | |
} | |
// print the array | |
for(int r = 0; r < rows; r++) { | |
for(int c = 0; c <= r; c++) | |
cout << arr[r][c] << " "; | |
cout << endl; | |
} | |
// free the array | |
for(int r = 0; r < rows; r++) | |
delete [] arr[r]; | |
delete [] arr; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment