Question

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 I am using Turbo C++ -

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

Était-ce utile?

La solution

The function skips all elements in the diagonal 5739 (i != j takes care of this), but, based on the desired output, you also wish to skip all elements in the diagonal 4902.

To also check for the other diagonal, replace

if (i != j)

with

if (i != j && i != size-j-1)

Test.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top