Piont out代码错误? [英] Piont out error in code?

查看:96
本文介绍了Piont out代码错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它给出了正确的输出直到命令= 3之后它给出了错误的答案。没有得到我错误的地方PLZ帮助我。我只是想提示



我尝试过:



it is giving correct output till order = 3 after that its giving wrong answer.iam not getting the place where i have gone wrong plz help me.i just want the hint

What I have tried:

#include<stdio.h>
#include<math.h>
int m;
int main()
{
int i,j;
printf("enter order of matrix\n");
scanf("%d",&m);
int a[m][m];
printf("enter elements of matrix\n");
for(i=0;i<m;i++)
{
    for(j=0;j<m;j++)
    {printf("a[%d][%d] = \n",i,j);
    scanf("%d",&a[i][j]);}
}
det(m,a);
printf("detrminant = %d",det(m,a));
}
int det(int x,int a[][x])
{   int k,i,j,b[m][m],sum=0;
    if(x==2)
    {
        sum=a[0][0]*a[1][1]-a[0][1]*a[1][0];
        return sum;
    }
    for(k=0;k<x;k++)
    {   int r=0,s=0;
        for(i=1;i<m;i++)
        {
            for(j=0;j<m;j++)
            {
                if(j==k)
                continue;
                b[r][s]=a[i][j];
                s++;
                if(j>m-1)
                {
                    r++;
                    s=0;
                }
                
            }
        }
        sum=sum+a[0][k]*pow(-1,k)*det(x-1,b);
    }
    
    
    return sum;
    
}

推荐答案

由于以下几个原因,我们无法指出错误:

首先,我们不知道它应该做什么!

其次,我们不知道你给它做了什么数据才能做到这一点。

第三,我们不知道给出错误答案意味着什么 - 因为前两个意味着我们不知道什么是正确答案。



第四,因为这是难以理解的代码,我至少不会费心去弄清楚它是如何工作的。

更改代码:停止使用单字母变量名,并使用适合变量执行的函数的名称。选择缩进样式 - 我不关心哪一个 - 并坚持下去。 [除此之外:

We can't "point out the error" for a couple of reasons:
Firstly, we have no idea what it is supposed to do!
Secondly, we have no idea what data you gave it in order to make it do it.
Thirdly, we have no idea what "giving the wrong answer" means - because the first two mean we don't know what a "right answer" would be.

And fourthly, because that is impenetrable code that I at least cannot be bothered to work out how it works.
Change your code: stop using one-letter variable names and use names that are appropriate for the function the variable performs. Pick an indentation style - I don't care which one - and stick with it. [Except this:
for(j=0;j<m;j++)
{printf("a[%d][%d] = \n",i,j);
scanf("%d",&a[i][j]);}

这是一种无用的风格]

然后开始用调试器查看你的代码并弄清楚它在做什么。



在函数的第一行放置一个断点,然后通过调试器运行代码。然后查看您的代码,并查看您的数据并找出手动应该发生的事情。然后单步执行每一行检查您预期发生的情况正是如此。如果不是,那就是当你遇到问题时,你可以回溯(或者再次运行并仔细观察)以找出原因。




之前你已经被告知了这一切,但是如果你不打算听你有问题吗?

That's a useless style]
Then start looking at your code with the debugger and working out what it is doing.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.


You have been told all this before, but if you aren't going to listen is there any point in you asking the questions?


你的代码有麻烦处理子矩阵。

尝试:

Your code has troubles handling the sub-matrices.
Try:
#include <stdio.h>

int det(int x,int a[][x]);

int main()
{
  int i,j,m;
  printf("enter order of matrix\n");
  scanf("%d",&m);
  int a[m][m];
  printf("enter elements of matrix\n");
  for(i=0;i<m;i++)
  {
    for(j=0;j<m;j++)
    {
      printf("a[%d][%d] = \n",i,j);
      scanf("%d",&a[i][j]);
    }
  }
  printf("determinant = %d\n",det(m,a));
}

int det(int x,int  a[][x])
{
  int k,i,j,b[x-1][x-1],sum=0;
  if(x==2)
  {
    sum=a[0][0]*a[1][1]-a[0][1]*a[1][0];
    return sum;
  }
  for(k=0;k<x;k++)
  {
    int r=0,s=0;
    for(i=1;i<x;i++)
    {
      for(j=0;j<x;j++)
      {
        if(j==k)
          continue;
        b[r][s]=a[i][j];
        s++;
        if( s == x-1)
        {
          r++;
          s=0;
        }
      }
    }
    int sign = k & 1 ? -1 : 1;
    sum=sum+a[0][k]*sign*det(x-1,b);
  }
  return sum;
}


这篇关于Piont out代码错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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