424,294 Members | 1,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,294 IT Pros & Developers. It's quick & easy.

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

P: 1
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
Share this Question
Share on Google+
11 Replies


Expert 10K+
P: 11,448
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

P: 93
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

P: 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

Expert 10K+
P: 11,448
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

P: 4
__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

emaghero
P: 85
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

P: 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

P: 2
but what if we want to print both the digonals????
Jan 31 '14 #9

Banfa
Expert Mod 5K+
P: 8,916
There is still a relationship between the 2 indexes you just need to work it out.
Jan 31 '14 #10

P: 2
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

P: 1
In Java,

for(int i=0; i<Arr.length; i++){
System.out.println( Arr[i][i] + " " + Arr[i][Arr.length-1-i]);
}
2 Weeks Ago #12

Post your reply

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