该程序可以计算矩阵A和B的总和,但对该程序不满意,可以帮助我更正它. [英] This a program that calculate the sum of matrices A and B but am not satisfied with the program , can some one help me to correct it .

查看:65
本文介绍了该程序可以计算矩阵A和B的总和,但对该程序不满意,可以帮助我更正它.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
using namespace std;
int main()
{
    int A[10][10], m,n,x,y,
        ,sum=0;
    cout << "Enter number of rows and colums in matrix A: \n";
    cin >>n>>m;
    cout << "Enter element of Matrix A: \n";
    for (x=1; x<n+1; ++x)
        for(y=1; y<m+1;++y)
        cin >>A[x][y];
    for (x=1; x<n+1;++x)
    {
        A[x][m+1]=0;
        for (y=1;y<m+1;++y)
            A[x][m+1] = A[x][m+1]+A[x][y];
        for (y=1; y<m+2; ++y)
            cout <<A[x][y] <<" ";
        cout <<"\n";
    }
    return 0;
}

推荐答案

您的代码的第一个大问题是其与要求的强烈矛盾(程序中的矩阵B在哪里? ).然后是一些小问题(例如,您没有检查数组是否溢出).
The first big problem of your code is its strong contraddiction with the requirements (where''s the matrix B in the program?). Then there are the minor problems (for instance you didn''t check for array overflow).


正如SA所说的,存在样式问题-如果您需要解决的话想进步.不仅如此,还需要首先解决一些基本问题.

1)使用数组.您是否意识到大多数语言(包括C和C ++)中的数组索引都从零开始?那么为什么要使用一个基于索引的索引呢?
As SA has said, there are style problems - which you will need to attend to if you want to progress. But more than that, there are fundamental problems you need to look at first.

1) Use of arrays. You do realize that array indices in most languages (C and C++ included) start from zero? So why are you using a one based index?
for (x=1; x<n+1;>        for(y=1; y<m+1;++y)>
    cin >>A[x][y];


2)在讨论上面的代码时,有两个要点:
2A)正确缩进您的代码.随意看一下代码意味着cin是作为外部循环而不是内部循环的一部分执行的.
2B)作为初学者,(并且我仍然会这样做)始终将您的陈述包含在大括号对中,即使它是一条陈述也是如此.它变得更加明显,并且更容易犯错误并假设代码在语句块中执行(或未执行):


2) While we are talking about the code above, two important points:
2A) Indent your code properly. A casual look at your code implies that the cinis performed as part of the outer loop, not the inner.
2B) As a beginner, (and I still do this) always contain your statements in brace pairs, even if it is a single statement. It makes it more obvious, and it is harder to make a mistake and assume that code is (or isn''t) executed within a statement block:

for (x=1; x<n+1;>        {
    for (y=1; y<m+1;++y)>
        {
        cin >>A[x][y];
        }
    }

相信我:这将节省您的头部抓伤负担.
3)评论:使用它们.即使在像这样的小程序中,也不清楚所有操作的含义.假设您六个星期都不会看它,请评论您所做的每件事.
4)变量:使用专有名称.单个字符更容易键入,是的.但是专有名称使它更易于阅读-从而理解和调试.您为-sum赋予了一个明智的名称的唯一变量-您甚至都没有使用它!
5)输出:给穷人草皮一些机会!使用标题,文本,换行符.不仅仅是数字的单行.使其易于阅读和使用.
6)检查:如果我输入12 x 14的尺寸,您的程序会怎样?繁荣!是什么.检查这些东西.然后再次检查它们.为用户提供一个体面的错误消息要好得多,因为该程序执行了非法操作,将被终止".
7)幻数:为什么要编码

Trust me: this will save you loads of head scratching.
3) Comments: Use them. Even in a small program like this, it isn''t obvious what everything is meant to do. Comment every thing you do, assuming you won''t look at it for six weeks.
4) Variables: Use proper names. Single characters are easier to type, yes. But proper names make it easier to read - and thus understand, and thus debug. The only variable you have given a sensible name to - sum - you don''t even use!
5) Output: Give the poor sod some chance! use headings, text, newlines. Not just one monolithic line of numbers. Make it easy to read and work with.
6) Checking: What happens to your program if I enter sizes of 12 x 14? BOOM! is what. Check these things. then check them again. Giving the user a decent error message is much better that "this program has performed an illegal operation and will be terminated".
7) Magic numbers: Why the heck are you coding

for (y=1; y<m+2; ++y)

为什么m+2? +2有什么作用?有必要吗?如果我更改它会怎样?
8)约定:这只是一个很小的问题,对代码完全没有影响,但是循环的约定是对增量使用后缀表示法:index++而不是++index

[edit]<和>浮肿-固定. OriginalGriff [/edit]

Why m+2? What does the +2 do? Is it necessary? What happens if I change it?
8) Conventions: It''s only a tiny point, and it makes no difference to the code at all, but the convention for loops is to use postfix notation for increments: index++ instead of ++index

[edit]< and > swollowed - fixed. OriginalGriff[/edit]


除了OriginalGriff在解决方案2中已经解决的问题之外,还有两个实际问题:

1.如果让用户输入所需的矩阵a大小,则必须随后以适当的大小分配此矩阵.考虑用户的观点:例如,如果您告诉Word将3x5列的表插入文档中,您是否希望看到10x10的表?只是没有意义.而且,正如OriginalGriff已经指出的那样,如果数字表示更大的Matrix,您的程序将崩溃.

2.从命令行读取矩阵系数很繁琐且容易出错.让程序从文件中读取其输入!这样,您可以使用自己选择的编辑器来输入数据,并且以后可以再次使用相同的数据重新运行该程序,而不必重复繁琐的输入过程.

3. cin有一个讨厌的习惯,在输入流中保留换行符.除非您将所有数字真正放在一行上,否则这将破坏您的输入循环.使用 getline 函数,然后适当地解释输入行更安全

4.显然,您将两个矩阵的系数存储到一个矩阵A中,而不是使用两个不同且独立的对象.另外,您将覆盖A的一部分来存储和.

这太邪恶了,我不得不写一个新的段落!

您不仅混淆了A的哪些部分实际上属于B,而且还引入了一些内部索引算法,从而不必要地使算法复杂化(并且您弄错了!)

同样,提示您输入A的值是错误的,因为实际上要求您同时输入A和B的值.甚至更多,您必须输入矩阵宽度,该宽度是矩阵A和B的宽度之和,并且必须断断续续地输入两个矩阵的系数:A的第一行,B的第1行,然后是2行A,然后是B的第2行,依此类推!几乎没有什么可混淆的了!

编写一个函数以读取一个矩阵,并且只读取一个矩阵,然后只需调用该函数两次!
Apart from the issues that OriginalGriff already addressed in solution 2, there are a couple of actual problems:

1. If you let the user input the required size of Matrix a, then you have to allocate this matrix at the appropriate size afterward. Think of the users point of view: if for example you tell Word to insert a table of 3x5 columns into a document, would you expect to see a 10x10 table? It just doesn''t make sense. And, as OriginalGriff already stated, if the numbers indicate a bigger Matrix, your program will crash.

2. Reading matrix coefficients from command line is tedious and error prone. Make the program read its inputs from file instead! This way you can use the editor of your choice to enter the data, and you can later rerun the program with the same data again without having to repeat the tedious input process.

3. cin has a nasty habit of leaving linebreaks in the input stream. This will break your input loops, unless you really put all numbers onto a single line. It''s safer to use the function getline and then interpret the input lines appropriately

4. Apparently you''re storing the coefficients for both matrices into the one matrix A, rather than using two different and independent objects. Also you''re overwriting one part of A to store the sum.

This is so evil I had to make a new paragraph!

Not only are you obfuscating what parts of A actually belong to B, also you''re needlessly complicating the algorithm by introducing some internal index arithmetic (and you got it all wrong!)

Also the prompt to enter the values for A is wrong, as you are actually required to enter the values for A and B at the same time. Even more, you have to enter a matrix width that is the sum of the width of the matrices A and B, and you have to intermittently enter coefficients for both matrices: first row 1 of A, then row 1 of B, then row 2 of A, then row 2 of B, and so on! It can hardly get any more confusing!

Write a function to read one matrix, and one matrix only, and then just call that function twice!


这篇关于该程序可以计算矩阵A和B的总和,但对该程序不满意,可以帮助我更正它.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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