954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

this code does not work help me pls

whats wrong with this code it compiles well but gives wrong results

code is to arrange the 5 numbers in ascending order

for (a=0;a<=4;a++)
{
for (b=a+1;b<=4;b++)
{
if (num[a]>num[b])
{
temp=num[a];
num[a]=num[b];
num[b]=temp;
}
}
}


can anyone tell whats wrong??

pratigya
Newbie Poster
1 post since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

Hi,

First use [ code ] code tags [ /code ]. It' makes it alot easier to read code.

Second, show your complet code, where is num declared and how, is it declared as an array?

Third, where is temp declared, it should be declared inside the loop and be a local variable inside the loop.

JoBe
Posting Pro in Training
420 posts since Sep 2004
Reputation Points: 51
Solved Threads: 4
 

If you have 5 numbers the main cause shold be a out of bounds error. Use the below.

for (a=0;a<4;a++)
{
for (b=a+1;b<4;b++)
{


Edit:
No. Sorry. My mistake. Your code is correct. I got carried away by the <= sign. As Salem says, the error should be elsewhere.

WolfPack
Postaholic
Moderator
2,051 posts since Jun 2005
Reputation Points: 572
Solved Threads: 115
 

Well some context for the code would help.

Like how did you declare the num array?
How did you get data into it?
How did you print it out?

It looks about right "as is", so I can only assume the mistake is somewhere else.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You