循环时,我的数组输出出错 [英] Output of my array goes wrong while looping

查看:93
本文介绍了循环时,我的数组输出出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

main()
{
	clrscr();
	int num[5],i,j,temp;

	printf("Enter 5 numbers: \n");
	for(i=0;i<5;i++)
		scanf("%d",&num[i]);

	for(i=0;i<5;i++) {
		for(j=1;j<=5;j++) {
			if(num[i]>num[j]) {
				temp=num[i];
				num[i]=num[j];
				num[j]=temp;
			}
		}
	}

	printf("Sorted Order: ");
	for(i=5;i>0;i--)
		printf("%d ",num[i]);

	getch();
	return 0;
\*Output:
Enter 5 numbers:
1
3
4
2
5
Sorted Order: 2 1 3 4 5
*\
}





我试图对值进行排序在num []里面。正如你在我的评论中看到的那样(在Sorted Order部分),获得输入的第一个数字是2而不是1.我现在问自己。我的代码有任何错误吗?



我尝试过:



重新启动Turbo c ++应用程序。谷歌我的问题。重新阅读我的代码。什么都行不了。



I was trying to sort the value inside the num[]. As you can see on my comment(on the Sorted Order part) the first number to get input was 2 instead of 1. I am now asking myself. Do i have any errors on my code?

What I have tried:

Restart the Turbo c++ app. Google my problem. Re-read my code. Nothing works.

推荐答案

开发代码不仅仅是编写代码并让它编译的情况 - 在这之后有趣的是:让它工作! br />
幸运的是,你有一个工具可以帮助你做到这一点 - 它被称为调试器。

我不记得如何使用Turbo C ++(它现在是古老的历史)所以你我可能需要谷歌的具体细节,但首先在函数的开头放置一个断点并在调试器中运行你的代码。当它到达断点时,它将停止并让你接管。

单步执行代码,(谷歌搜索单步Turbo C ++)并查看变量。在每一行执行您期望发生的事情之前解决,然后检查是否发生了什么。如果它是相同的,继续前进。如果没有,为什么不呢?发生了什么事你没想到,或者没有发生过你做过的事情?这应该开始让你知道你做错了什么,所以你可以改变它并再试一次。



这是一项技能 - 可以预见的称为调试 - 和像所有技能一样,你只需要使用它来开发它。所以现在用这样一个简单的app来学习它并习惯调试器 - 你会经常使用它(我们都这样做!)
Developing code isn't just a case of writing the code and getting it to compile - after that comes the fun bit: getting it working!
Fortunately, you have a tool to help you do that - it's called the debugger.
I can't remember how to use Turbo C++ (it's ancient history now) so you'l probably have to Google for the specifics, but start by putting a breakpoint at the start of the function and running your code in the debugger. When it reaches the breakpoint, it will stop and let you take over.
Step through the code, (google for "Single step Turbo C++") and look at the variables. Work out before each line executes what you expect to happen and then check that against what did happen. If it's the same, move on. If not, why not? What happened that you didn't expect, or didn't happen that you did? This should start to give you a good idea what you have done wrong, so you can change that and try again.

This is a skill - predictably called debugging - and like all skills you only develop it by using it. So learn it now with a simple app like this and get used to the debugger - you will be using it a lot (we all do!)


显然,你的索引在某个地方是错的。由于数组大小为5,因此只有索引0到4包含有效。



因此,在排序代码的第二级循环中,您将一个项目停止为远(索引5而不是索引4)。这会导致缓冲区溢出,这通常会影响该数组附近的其他变量并导致未定义的行为。



显示结果时遇到同样的问题。你开始和结束一个项目到高。所以你打印索引5,4,3,2和1而不是4,3,2,1和0的项目。



有人建议,使用调试器这将是一个好主意...



但是,如果您想根据需要编写任何严格的代码,您应该能够找出读取代码的问题在编写C代码时理解循环索引...所以也许,你应该再次阅读文档。



如果你正在学习语言,那么显示一些跟踪信息可能会很有用因为你可以看到你愿意展示的信息。在这种情况下,在每个步骤显示所有变量可能会让您清楚地了解计算机语言的工作。
Obviously, your index are wrong at some location. Since the array size is 5, then only index 0 to 4 inclusively are valid.

Thus, in your second level loop of the sorting code, you stop one item to far (index 5 instead of index 4). This cause buffer overflowing which might typically affect other variables near that array and cause undefined behavior.

You have the same problem when you display the result. You start and end one item to high. So you print items at index 5, 4, 3, 2 and 1 instead of 4, 3, 2, 1 and 0.

As someone suggested, using a debugger would be a good idea...

However, you should be able to figure out such problems reading your code if you want to write any serious code as you need to understand loop indexing when writing C code... So maybe, you should read the documentation again.

Also displaying some trace information might be useful if you are learning the language as you could see as much information as you are willing to display. In that case displaying all variables at each step might give you a clear idea on computer languages work.


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

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

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



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你接近一个bug。



建议:给一张纸做故事并尝试手工操作,你的程序应该使用相同的程序。 />
将代码的开头更改为

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Advice: tale a sheet of paper and try to do it by hand, your program should use the same procedure.
Change the begining of your code to
int num[6],i,j,temp; // Change
for(i=0;i<6;i++)     // insert
    num[i]= 5;   // insert





和输入值:1 3 6 2 7

如果得到5的结果,你就会知道你在数组之外排序。



And input values: 1 3 6 2 7
If you get a 5 in result, you will know that you sort outside of your array.


这篇关于循环时,我的数组输出出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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