您好,请帮我这个程序,我不明白为什么它不起作用。 [英] Hello, please help me with this program, I dont get it why it does not work.

查看:79
本文介绍了您好,请帮我这个程序,我不明白为什么它不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  //  任务:给出方形二维数组:查找最大的负面因素及其阵列地址。  

#include < iostream >
#include < < span class =code-attribute> iomanip >
#include < string >
使用 命名空间标准;
int main()
{
int a [ 5 ] [ 5 ];
int did,n,m,i,j;

cout<< 类型数组测量(最多5个): << ENDL;
cin>> m>> n;
cout<< 类型数组元素<< endl;

for (i = 0 ; i< m; i ++)
for (j = 0 ; j< n; ++ j)
cin> >一种[i] [j];

did = a [ 0 ] [ 0 ];
for (i = 0 ; i< m; ++ i)
{
for (j = 0 ; j< n; ++ j)
{
如果(a [i] [j]>做&& a [i] [j]< 0
did = a [i] [j];
i = m;
j = n;
}
}

cout<< 最大的负数元素是<<做了<< ENDL;
cout<< 其数组地址:<< m< ;<< n<< endl;
system( pause);
return 0 ;
}

解决方案

您需要对代码进行一些更改。

首先拿两个变量来保存索引。



  int  nI,nJ; 
如果(a [i] [j] < && a [ i] [j] < 0 ){ // 检查值是否小于0和0。
did = a [i] [j];
nI = i; // 保存i的索引。
nJ = j; // 保存j的索引。
}







您之前做过:

i = m; //这里无论条件是什么。

j = n; //它总是每次都分配相同的m和n值。

由于m和n的值在整个程序中没有变化所以你总是得到相同的值。

为m和n是数组的大小,您将它们分别分配给i和j。它会到达循环结束。所以它来自for循环。


你的代码没有你想象的那样,你不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行你的代码。

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



此解决方案的缺点:

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

这个解决方案的优点:

- 你看到你的代码行为,你把它与你的期望相匹配。



次要影响

- 你会为此感到骄傲自己发现错误。

- 你的技能会提高。


你应该很快找到错误。



建议:

- 使用牙套 {} 在任何地方它都应该是可选的。

- 使用缩进,它使你的程序更容易阅读并给出你打算做什么的提示。

//Task : There is given square two-dimensional array: Find the biggest negative element and its array adress. 

#include < iostream>        
#include < iomanip>
#include < string>
using namespace std;
int main()
{
    int a[5][5];
    int did,n,m,i,j;

    cout<<"Type array measurements(max 5):" <<endl;
    cin>>m>>n;
    cout<<"Type array elements" <<endl;

    for(i=0;i<m;i++)
        for(j=0;j<n;++j)
            cin>>a[i][j];
    
    did = a[0][0];
    for (i=0; i < m; ++i)
    {
        for (j=0; j < n; ++j)
        {
            if (a[i][j] > did && a[i][j] < 0)
                did = a[i][j];
            i = m;
            j = n;
        }
    }

    cout <<"Biggest negative element is " << did << endl;
    cout <<"Its array adress: " << m <<";" << n <<endl;
    system("pause");
    return 0;
}

解决方案

You need to make a little change in your code.
First take two variable to save the Index.

int nI,nJ;
if (a[i][j] < did && a[i][j] < 0){ //check if value less than did and 0.
      did = a[i][j];
      nI = i;        // save index of i.
      nJ = j;        //save index of j.
}




Earlier you were doing :
i = m; // here no matter what the condition is.
j = n; // it will always assign the same value of m and n every time.
As value of m and n are not changing in whole program so you will get the same value always.
as m and n are the size of array and you are assigning it to i and j respectively. it will reach end of the loop. so it comes out of the for loop.


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

There is an almost universal solution: Run your code on debugger step by step.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

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:
- You see your code behaviour, you match it against your expectations.

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

You should find pretty quickly what is wrong.

Advices:
- Use braces {} everywhere it should be even if optional.
- use indentation, it makes your program easier to read and gives hints on what you intend to do.


这篇关于您好,请帮我这个程序,我不明白为什么它不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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