这个程序有什么错误。 [英] What is the mistake in this program.

查看:55
本文介绍了这个程序有什么错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <stdlib.h>
#define N 3

int main()
{
    int i,j,k,n=N;float b[N][N],a[N][N],c[N][2*N],det,l;
    printf("Enter the matrix :\n\n");
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            printf("a[%d][%d]= ",i+1,j+1);
           scanf("%f",&b[i][j]);
        }
    }
    printf("ENTERED MATRIX:\n\n");
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)printf("%10.4f",b[i][j]);printf("\n");
    }
    /*Identity Matrix*/
    for(i=0;i<N;i++)for(j=0;j<N;j++)a[i][j]=(i==j)?1:0;

    /*Echelon Form*/
    for(i=0;i<N;i++)
    {
        for(j=0;j<(2*N);j++)
        {
            if(j<N)c[i][j]=b[i][j];
            else c[i][j]=a[i][j-n];
        }
    }

    printf("\nRow Echelon Form:\n\n");
    for(i=0;i<N;i++)
    {
        for(j=0;j<(2*N);j++)printf("%10.4f",c[i][j]);printf("\n");
    }

    printf("\n");
    /*Operation for inverse of matrix begins*/
    for(k=0;k<N-1;k++)
    {
        if(c[k][k]==0)
        {
            for(i=k+1;i<N;i++)if(c[i][k]!=0)break;
            for(j=0;j<(2*N);j++){l=c[i][j];c[i][j]=c[k][j];c[k][j]=l;}
        }
        for(i=k+1;i<N;i++)
        {
            for(j=k;j<N;j++)
                {c[i][j]=c[i][j]-(c[i][k]/c[k][k])*c[k][j];
                 c[i][j+n]=c[i][j+n]-(c[i][k]/c[k][k])*c[k][j+n];}
        }
    }

    for(i=0,det=1;i<N;i++)det*=c[i][i];
    if(det==0.0){printf("\nInverse of Matrix can't be found.\n");exit(0);}
    /*Determinant operation ends*/

    for(k=N-1;k<0;k--)
    {
        if(c[k][k]==0)
        {
            for(i=k-1;i<=0;i--)if(c[i][k]!=0)break;
            for(j=0;j<(2*N);j++){l=c[i][j];c[i][j]=c[k][j];c[k][j]=l;}
        }
        for(i=k-1;i<=0;i--)
        {
            for(j=k;j<=0;j--)
                {c[i][j]=c[i][j]-(c[i][k]/c[k][k])*c[k][j];
                c[i][j+n]=c[i][j+n]-(c[i][k]/c[k][k])*c[k][j+n];}
        }
    }
    for(i=0;i<N;i++){c[i][i]=c[i][i]/c[i][i];c[i][i+n]=c[i][i+n]/c[i][i];}/*corner elements*/

    printf("\nRow Echelon Form:\n\n");
    for(i=0;i<N;i++)
    {
        for(j=0;j<(2*N);j++)printf("%10.4f",c[i][j]);printf("\n");
    }


    printf("\nDeterminant of Matrix is %f",det);

    printf("\n\nINVERSE OF MATRIX:\n\n");

    /*printing operation for inverse of matrix begins*/
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            printf("%10.4f",c[i][j+n]);
        }
        printf("\n");
    }
    return 0;
}





我的尝试:



这是一个矩阵逆的程序,它没有显示任何错误但也没有给出正确的输出。



What I have tried:

It's a program for inverse of a matrix it isn't showing any errors but also not giving the proper output.

推荐答案

引用:

这个程序有什么错误。



第一个错误就是你的编码风格,它非常好使你的代码不可读。下一步是混淆,它使代码完全不被人类读取,但编译器仍然可以处理它。

建议:永远不要尝试在源代码中保存几行,它不会使代码更快,只会让它难以阅读。



您的代码没有按照您的预期行事,或者您不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码在做什么和你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它不知道你的cpde应该做什么,它没有找到bug,它只是帮助你向你展示发生了什么。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行并在执行时检查变量。



此解决方案的缺点:

- 这是一个DIY,你是跟踪问题并找到根源的那个,这导致了解决方案。

这个解决方案的优点:

- 它也是一个很好的学习工具,因为它告诉你现实,你可以看到哪种期望与现实相符。



次要效果

- 你会为自己找到虫子感到自豪。

- 你的学习技巧会提高。



你应该很快就会发现什么是错的。



调试器 - 维基百科,免费的百科全书 [ ^ ]

1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]

调试器仅显示您的代码正在执行的操作,并且您的任务是与应该执行的操作进行比较。


The first mistake is your coding style, it is very good at making your code unreadable. The next step is obfuscation, it makes code completely unreadable by humans, but the compiler can still handle it.
Advice: Never try to save a few lines in source code, it don't make the code faster, it only make it difficult to read.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]
1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于这个程序有什么错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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