469,315 Members | 1,795 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

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..

Thanks in advance
Bala...
Aug 10 '07 #1
11 64583
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..

Thanks in advance
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..

Thanks in advance
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..

Thanks in advance
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
Lestat
1
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,064 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
Mathss
1
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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

31 posts views Thread by arun | last post: by
2 posts views Thread by serave | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.