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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • balaraja
    New Member
    • Aug 2007
    • 1

    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...
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by balaraja
    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

    Comment

    • Girish Kanakagiri
      New Member
      • May 2007
      • 93

      #3
      Originally posted by balaraja
      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.

      Comment

      • Gauri attal
        New Member
        • Aug 2007
        • 3

        #4
        Originally posted by balaraja
        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.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Gauri attal
          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

          Comment

          • Sam Tan
            New Member
            • Aug 2007
            • 4

            #6
            __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

            Comment

            • emaghero
              New Member
              • Oct 2006
              • 85

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

              Code:
              int rows;
              int columns;
              
              //Assign a value to rows and columns
              
              //Also assign values to the matrix
              
              if(rows !=columns){
              	cout<<"Cannot obtain the diagonal of a non-square matrix\n";
              }
              else{
              	 for(int i=0;i<rows;i++){
              		   cout<<a[i][i]<<endl;
              	 }
              }

              Comment

              • Lestat
                New Member
                • Jan 2013
                • 1

                #8
                Just go with the program hint given by Gauri this program
                Code:
                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];
                }
                }
                }
                Last edited by Rabbit; Jan 22 '13, 04:40 PM. Reason: Please use code tags when posting code.

                Comment

                • shikha tripathi
                  New Member
                  • Jan 2014
                  • 2

                  #9
                  but what if we want to print both the digonals????

                  Comment

                  • Banfa
                    Recognized Expert Expert
                    • Feb 2006
                    • 9067

                    #10
                    There is still a relationship between the 2 indexes you just need to work it out.

                    Comment

                    • chaitanya4b3
                      New Member
                      • Sep 2015
                      • 2

                      #11
                      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]);
                      }

                      }


                      }

                      Comment

                      • Mathss
                        New Member
                        • Feb 2019
                        • 1

                        #12
                        In Java,

                        for(int i=0; i<Arr.length; i++){
                        System.out.prin tln( Arr[i][i] + " " + Arr[i][Arr.length-1-i]);
                        }

                        Comment

                        Working...