Upper half of 2D Array

In a 5*5 matrix i want to display the primary diagonals upper half so i developed the logic as follows:-

cout<<"Enter elements in a 5*5 matrix"<<endl;
int a[5][5],i,j,k=0;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
cin>>a[i][j];
}
}
cout<<"The upper half of the primary diagonal is"<<endl;
for(i=0;i<5;i++)
{
for(j=k;j<5;j++)
{
cout<<a[i][j]<<" ";
}
cout<<"\n";
k=k+1;
}

Instead of the upper half the lower half is being displayed . WHY ?
No it's not?

Enter elements in a 5*5 matrix
5 6 7 8 9
2 3 4 5 6
0 1 2 3 4
9 10 11 12 13
21 22 23 24 25

The upper half of the primary diagonal is
5 6 7 8 9
3 4 5 6
2 3 4
12 13
25

(Changed the input to represent a table, but I changed nothing in your code.)

Maybe you're confused because the of the layout. Add this line:
for(j = 0; j < k; ++j) cout << "\t";
and in the output cout<<a[i][j]<<" ";, change the space " " to "\t" (tab).

That changes the output to:
The upper half of the primary diagonal is
5       6       7       8       9
        3       4       5       6
                2       3       4
                        12      13
                                25

Clearly showing the upper diagonal matrix.
What all did you change , now the layout is lower half.

Thanks
Topic archived. No new replies allowed.