Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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
*\
}


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.
Posted
Updated 30-Sep-16 10:26am
v3
Comments
[no name] 30-Sep-16 11:03am    
As you have already been told, yes you have at least one error in your code. Without actually seeing your code, we can't tell you what it is. Posting just "#include" doesn't help us help you. And, posting and deleting your questions over and over is annoying.
Member 12769131 30-Sep-16 11:06am    
sorry but i tried to improve my question and the whole code doesn't seem to get posted. Sorry for making you feel annoyed. I am so sorry
[no name] 30-Sep-16 11:10am    
Sorry, I am not annoyed. I simply ignore people that do this kind of thing. I do not see what is so difficult with copy/paste but people seem to have a great deal of difficulty simply pasting their code into a textbox.
David_Wimbley 30-Sep-16 11:11am    
You need to post your question between <pre lang='c++'>..your code here..</pre> tags to get your code to show up.

Plenty of people do it on this website daily...i believe in you.
Member 12769131 30-Sep-16 11:13am    
thank you it worked. I am sorry I am a newbie here.

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!)
 
Share this answer
 
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.
 
Share this answer
 
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
C++
int num[6],i,j,temp; // Change
for(i=0;i<6;i++)     // insert
    num[i]= 5;   // insert


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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900