非对角阵列元素不显示 [英] Non Diagonal Array Elements not displaying

查看:81
本文介绍了非对角阵列元素不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everybody



我成功显示了Diagonal Array元素,但未能显示非对角线阵列元素我尝试了很多但不成功。以下是我尝试的代码 -

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();
}





示例:如果数组内容是

5 4 3 4

6 7 9 1

8 0 3 7

2 4 5 9

通过功能输出应该be:

4 3 6 1 8 7 4 5



输出显示一些对角线元素。



需要帮助



提前谢谢你



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

推荐答案

这个看起来像是家庭作业,但让我们看看我们是否能找到一个模式,你可以从那里拿走它...



有两个对角线(通过矩阵),第一个对角线的元素是(row,col):



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


那个很容易(你已经检查过那个)



第二个对角线是:



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



你看到了这种模式吗?你知道你错过了支票吗?
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?


代码的问题是传递长度是多余的,因为你的接受方法配置文件已指定4x4数组维度。无论如何,主要的问题不是这个代码,而是你对输出的期望。



由于某种原因,你期望4 3 6 1 8 7 4 5。这是错的。你在第一个原始中错过4,在第二行中错过9,等等。你的代码应该正确显示非对角线元素。使用调试器,你会看到。



-SA
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


这篇关于非对角阵列元素不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆