Skip to content

Instantly share code, notes, and snippets.

@mindfulme
Created August 26, 2017 10:53
Show Gist options
  • Save mindfulme/6257a36d6719af2ce5619593c9af3ba1 to your computer and use it in GitHub Desktop.
Save mindfulme/6257a36d6719af2ce5619593c9af3ba1 to your computer and use it in GitHub Desktop.
C++ Triangular Array
#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