Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to perform sorting operation within 'for loop' as follows:

C++
void main()
{
 int a[5]={5,1,4,2,3},i,j,n,flag=1,temp;
 for(i=0,j=i ; (a[i]<a[j]);
    temp=a[i],
    a[i]=a[j],
    a[j]=temp,
    if(flag == 1){i++;},
    if(flag >1 && flag <6){j++;flag++;},
    if(flag >5){flag=1;}
    );

}

I have set a 'flag' to increment 'i', only when 'j' is completed incremented till 5. Now I am getting error in 'if' statement.
Please let know the correct answer.

Thanks in advance
Posted
Updated 28-Oct-12 22:02pm
v2
Comments
Zoltán Zörgő 29-Oct-12 3:42am    
And what kind of error?

Jebus flippen Christ!


Can I post this one in the Code Horrors section? :jig: Can I, can I, can I?

That has got to be about the worst for-loop abuse I have ever seen in my life. That makes blood run from my eyes;p. It may come close to winning an obfuscation contest, but come tommorrow or the next day or next week and you'll be staring sideways at it like the rest of us.

I've just realized that it's actually an empty body for the for-loop. You've tried to do all of your processing and logic in the third parameter for the for loop.

You _can_ abuse it this far and get away with it:

C++
int main()
{
 int a[5]={5,1,4,2,3},i,j,n,flag=1,temp;
 for(i=0,j=i ; (a[i]<a[j]); temp=a[i],a[i]=a[j],a[j]=temp);
 return 0;
}


The reason it won't work is that you've tried to stuff if statements into the last part of the for loop - a big no-no, and one large enough that the compiler can't or wont allow you to do such as thing - I've no idea of the internals of TurboC or anything more modern, for that matter.

Following all of this fun and hilarity, I notice that you're trying to return void from main - that's also no good, and has been an enforced no-no for years now. If you get a new compiler, you'll get (a) better executable code for the same input (b) better error messages when it's not compilable (c) help to 'get things right' rather than a compiler that looks the other way.


I _think_ you're trying to write something like this?
C++
int main()
{
    int a[5]={5,1,4,2,3},i,j,n,flag=1,temp;
    for(i=j=0; (a[i]<a[j]); )
    {
        temp=a[i];
        a[i]=a[j];
        a[j]=temp;
        if(flag == 1)
        {
            i++;
        }
        if ((flag>1)&&(flag<6))
        {
            j++;
            flag++;
        }
        else if (flag > 5)
        {
            flag=1;
        }
    }
}
 
Share this answer
 
Comments
bbirajdar 29-Oct-12 4:48am    
Absolutely.. This code is fit for our coding horrors section +5
Remove the comma:
C++
if(flag >1 && flag <6){j++;flag++;},
Becomes
C++
if(flag >1 && flag <6){j++;flag++;}
 
Share this answer
 
Comments
enhzflep 29-Oct-12 5:03am    
Certainly a good start, but still no cigar.

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