Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
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 -
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.

Need help

Thank you in advance
Posted
Comments
[no name] 4-Sep-13 19:52pm    
The answer would have been obvious if you had shown the actual output. You are missing one (non) diagonal.

This looks like homework, but let's see if we can find a pattern and you can take it from there...

There are two diagonals (which make an X through the matrix), the elements of the first diagonal are (row,col):

0,0 : 1,1 : 2,2 : 3,3

That one is easy (and you already check for that one)

The second diagonal is:

3,0 : 2,1 : 1,2 : 0,3

Do you see the pattern? Do you see that you are missing a check?
 
Share this answer
 
Comments
[no name] 4-Sep-13 19:52pm    
OP should be happy - homework done.
The problem with your code is that passing length is redundant, as your accept method profile already specify 4x4 array dimension. Anyway, the main problem is not this code, but your expectation of the output.

By some reason, you expected "4 3 6 1 8 7 4 5". This is wrong. You miss 4 in first raw, 9 in second row, etc. Your code should really show non-diagonal elements correctly. Use the debugger, and you will see.

—SA
 
Share this answer
 
Comments
vishal deb 4-Sep-13 14:05pm    
5 4 3 4
6 7 9 1
8 0 3 7
2 4 5 9
Hence
4 3 6 1 8 7 4 5
Sergey Alexandrovich Kryukov 4-Sep-13 23:05pm    
No, you are missing an element from each row. Your code shows correct sequence.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900