Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all, first I want to say thanks to this community, because it's my first post in here =)

I'm very fresh in C++/CLI and I've only a basic question:
How to simplify this loop ?

I want to set x/y coordinates for a 30*30 square in the following order:
y = 0 -> x from 0 to 29;
y = 1 -> x from 0 to 29;
...
y = 29 -> x from 0 to 29;
Since in every case (except of j==0) y = j/30, I think there is a chance to get this smaller. But I have no idea to get a better expression for x.

Thank you for any suggestions and/or hints :)

for (int j=0; j<900; j++)
{
if ( j == 0)
{
	y = 0;
	x = 0;
} else if ( j >= 1 && j <= 29 )
{
	y = j/30;
	x = j;
} else if ( j >= 30 && j <= 59 )
{
	y = j/30;
	x = j-30;
} else if ( j >= 60 && j <= 89 )
{
	y = j/30;
	x = j-60;
} else if ( j >= 90 && j <= 119 )
{
	y = j/30;
	x = j-90;
} else if ( j >= 120 && j <= 149 )
{
	y = j/30;
	x = j-120;
} else if ( j >= 150 && j <= 179 )
{
	y = j/30;
	x = j-150;
} else if ( j >= 180 && j <= 209 )
{
	y = j/30;
	x = j-180;
} else if ( j >= 210 && j <= 239 )
{
	y = j/30;
	x = j-210;
} else if ( j >= 240 && j <= 269 )
{
	y = j/30;
	x = j-240;
} else if ( j >= 270 && j <= 299 )
{
	y = j/30;
	x = j-270;
} else if ( j >= 300 && j <= 329 )
{
	y = j/30;
	x = j-300;
} else if ( j >= 330 && j <= 359 )
{
	y = j/30;
	x = j-330;
} else if ( j >= 360 && j <= 389 )
{
	y = j/30;
	x = j-360;
} else if ( j >= 390 && j <= 419 )
{
	y = j/30;
	x = j-390;
} else if ( j >= 420 && j <= 449 )
{
	y = j/30;
	x = j-420;
} else if ( j >= 450 && j <= 479 )
{
	y = j/30;
	x = j-450;
} else if ( j >= 480 && j <= 509 )
{
	y = j/30;
	x = j-480;
} else if ( j >= 510 && j <= 539 )
{
	y = j/30;
	x = j-510;
} else if ( j >= 540 && j <= 569 )
{
	y = j/30;
	x = j-540;
} else if ( j >= 570 && j <= 599 )
{
	y = j/30;
	x = j-570;
} else if ( j >= 600 && j <= 629 )
{
	y = j/30;
	x = j-600;
}else if ( j >= 630 && j <= 659 )
{
	y = j/30;
	x = j-630;
} else if ( j >= 660 && j <= 689 )
{
	y = j/30;
	x = j-660;
} else if ( j >= 690 && j <= 719 )
{
	y = j/30;
	x = j-690;
} else if ( j >= 720 && j <= 749 )
{
	y = j/30;
	x = j-720;
} else if ( j >= 750 && j <= 779 )
{
	y = j/30;
	x = j-750;
} else if ( j >= 780 && j <= 809 )
{
	y = j/30;
	x = j-780;
} else if ( j >= 810 && j <= 839 )
{
	y = j/30;
	x = j-810;
} else if ( j >= 840 && j <= 869 )
{
	y = j/30;
	x = j-840;
} else if ( j >= 870 && j <= 899 )
{
	y = j/30;
	x = j-870;
}
} 
Posted

Sounds like the classic case for nested loops:

for (y = 0; y < 30; y++)
  {
  for (x = 0; x < 30; x++)
    {
    // whatever you want to do for each (x,y)
    }
  }
 
Share this answer
 
Try hoisting everything you can out of the if statements - first off the assignment to y is the same whatever values you're using:

C++
for (int j=0; j<900; j++)
{
    y = j/30;

    if ( j == 0)
    { 
        x = 0;
    }
    else if ( j >= 1 && j <= 29 )
    {
        x = j;
    } 
    else if ( j >= 30 && j <= 59 )
    {
        x = j-30;
    }

    // ad nauseum...

}


Then either notice that you're subtracting 30 * y off j to get x:

for( int j = 0; j < 900; j++ )
{
    y = j / 30;
    x = j - y * 30;
}


or use nested loops the way Christian suggested.

Admittedly most compilers would probably notice that the loop is a very bad way of saying:

y = x = 29;


but I'll assume that x and y are just placeholders for something for now.

Cheers,

Ash
 
Share this answer
 
Use Math.Floor to get the whole number values you need to do this in a single loop.

int n = (int) Math.Floor(j/30);
x = n * 30;

Something like that.
 
Share this answer
 
Comments
Niklas L 10-Aug-10 4:16am    
The floor function is a bit superfluous. Since j is already an int, int n = j/30; should be sufficient.
Aescleal, you are right. It seems logical, but I didn't catch that connection between y and * 30.
Thank you, now the loop is damn short!
 
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