471,854 Members | 1,922 Online

# Printing the diagonal elements of an array using for loop..

Hi
how to print the diagonal elements of a two dimensional array using for loop
Can any one help me...Plzzzzzzzz..

Bala...
Aug 10 '07 #1
11 66040
JosAH
11,448 Expert 8TB
Hi
how to print the diagonal elements of a two dimensional array using for loop
Can any one help me...Plzzzzzzzz..

Bala...
Just think a bit: what is characteristic about the index values of the elements
on the diagonal of a matrix? And what is 'Plzzzzzz'? Keyboard got stuck?

kind regards,

Jos
Aug 10 '07 #2
Hi
how to print the diagonal elements of a two dimensional array using for loop
Can any one help me...Plzzzzzzzz..

Bala...
develop one for loop for row traversing in the array and
inside this nest another for loop for column traversing within
the row.

In side the inner for loop just check whether row value and
column value are equal; if yes then print the array element
with row and column subscripts.

Do you feel you got the solution or still struggling ?

Regards,
Girish.
Aug 10 '07 #3
Hi
how to print the diagonal elements of a two dimensional array using for loop
Can any one help me...Plzzzzzzzz..

Bala...

Hi,

Try out this ....

for(int i=0;i<3;i++)
{
for (int j=0;j<=i;j++)
{
if(i==j)
{
cout<<"the diagonal elements are"<<a[i][j];
}
}
}

U too could have even tried...anyways...try the above

Gauri.
Aug 10 '07 #4
JosAH
11,448 Expert 8TB
Hi,

Try out this ....

<code snipped>
Better not; a nested loop is not necessary to iterate over the diagonal of a matrix.

kind regards,

Jos
Aug 10 '07 #5
__0 1 2 3
0| 1 0 0 1 |
1| 0 1 0 1 |
2| 1 0 0 0 |
3| 1 0 1 1 |

coordinates of diagonal elements
(0,0)
(1,1)
(2,2)
(3,3)

don't you see the pattern :) ?

Sam
Aug 11 '07 #6
I aggree with Josah, a single loop will do the job.

Also remember that the diagonal of a matrix (array) only makes sense if the matrix is square, ie number of rows equals number of columns.

Expand|Select|Wrap|Line Numbers
1. int rows;
2. int columns;
3.
4. //Assign a value to rows and columns
5.
6. //Also assign values to the matrix
7.
8. if(rows !=columns){
9.     cout<<"Cannot obtain the diagonal of a non-square matrix\n";
10. }
11. else{
12.      for(int i=0;i<rows;i++){
13.            cout<<a[i][i]<<endl;
14.      }
15. }
16.
Aug 11 '07 #7
Just go with the program hint given by Gauri this program
Expand|Select|Wrap|Line Numbers
1. for(int i=0;i<3;i++)
2. {
3. for (int j=0;j<=i;j++)
4. {
5. if(i==j)
6. {
7. cout<<"the diagonal elements are"<<a[i][j];
8. }
9. }
10. }
Jan 22 '13 #8
but what if we want to print both the digonals????
Jan 31 '14 #9
Banfa
9,065 Expert Mod 8TB
There is still a relationship between the 2 indexes you just need to work it out.
Jan 31 '14 #10
I think this should work
#include<stdio.h>
int main()
{
int a[3][3],i,j,n;
scanf("%d",&n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{

scanf("%d",&a[i][j]);

}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(i==j)
{
printf("%d",a[i][j]);
}

}

}
Sep 25 '15 #11
In Java,

for(int i=0; i<Arr.length; i++){
System.out.println( Arr[i][i] + " " + Arr[i][Arr.length-1-i]);
}
Feb 5 '19 #12