Hello Everybody

I am successful in displaying Diagonal Array elements, but failed to display Non Diagonal array elements I tried a lot but unsuccessful. Here is the code what I am try with -

#include<conio.h>
#include<iostream.h>
void accept(int a[4][4],int size)
{
    cout<<"Diagonal One:";
    for (int i=0;i<size;i++)
           for(int j=0;j<size;j++)
    if (i!=j)
         cout<<"\n"<<i <<"  "<<j<<"  "<<a[i][j];
}
void main()
{
      int a[4][4]={{5,4,3,4},{6,7,9,1},{8,0,3,7},{2,4,5,9}};
      clrscr();
      accept(a,4);
      getch();
}

Example : if the array content is
5 4 3 4
6 7 9 1
8 0 3 7
2 4 5 9
Output through the function should be :
4 3 6 1 8 7 4 5

Output is displaying some of the diagonal elements also.

Need help

Thank you in advance

Recommended Answers

All 2 Replies

What is the code you are using to display the diagonals? You basiclly need to invert the display logic to have it display the non diagonals. As an FYI this is how I would display the diagonals

// this assumes the array is sqaure and size is the size of the array
// the array is named numbers
int step = 1;

for (int i = 0; i < size; i++, step++)
{
    for (int j = 0; j < size; j++)
    {
        if(j + 1 == step || step + j == size)
            cout << numbers[i][j] << " ";
    }
}

@NathanOliver
For displaying Diagonal elements of array I am using this code-

#include<conio.h>
#include<iostream.h>
void accept(int a[4][4],int size)
{
    cout<<"Diagonal One:";
    for (int i=0;i<size;i++)
        for(int j=0;j<size;j++)
            if (i==j)
                cout<<a[i][j]<<" ";

    cout<<"\n Diagonal Two:";
    for (i=0;i<size;i++)
        for(j=0;j<size;j++)
            if((i+j)==(size-1))
                cout<<a[i][j]<<" ";
}
void main()
{
      int a[4][4]={{5,4,3,0},{6,7,8,0},{1,2,9,0},{4,6,8,9}};
      clrscr();
      accept(a,4);
      getch();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.