Please suggest alternate code for Traingular matrices.

#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;
}
{

That code looks OK (by beginner's standards, of course). What is the problem you are having with it? What kind of an alternative are you looking for? Why?

Note your line 7 could be changed to ...

int rows = 5; //, cols = 5; // cols is not used below

and you could use functions for each step ...

// allocate and initialize the array
// print the array
// free the array

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.