PDA

View Full Version : init problem

02-14-2002, 11:35 PM
i have a problem with initialization i have a array of points[9][2] i'm doing initialization on the x axis like this
for(i=0;i<10;i++)
for(j=0;j<=2;j++)
point[i][j].x=-1+((2/3)*j);
the results are:
for j=0 point[i][0].x=-1 correct
for j=1 point[i][1].x=-1 not correct !!!
for j=2 point[i][2].x=-0.333333 again not correct except for point[9][2].x which is correct (0.33333) where is the problem ?

Bob
02-14-2002, 11:52 PM
Use 2.0/3.0 instead of 2/3. Without '.0', you get integer division.

02-15-2002, 11:04 PM
Done that but still it isn't working

02-17-2002, 04:16 AM
ok i have done some debugging and it seems that the problem is only with the last row (i mean point[i][2]) which is -1.0f when it should be 0.03333f

for(i=0to9)
for(j=0to2)
{
point[i][j].x=.....
*
}
~

when i added printf at * point everything was OK but at ~ point it has shown incorrect values(-1 it should be 0.33333) except for that point[9][2] which is ok can someone help me with that

dww
02-17-2002, 07:59 PM
When I run the following code, it gives the correct results. You should check your code for typing mistakes. For example, I have a tendency to swap i's and j's.

struct Point
{
double x, y;
};

Point point[10][3];
int i, j;
// init
for(i=0;i<10;i++)
{
for(j=0;j<=2;j++)
point[i][j].x=-1+((2.0/3.0)*j);
}
// print
for(i=0;i<10;i++)
{
for(j=0;j<=2;j++)
printf("point[%d][%d].x=%f\n", i, j,
point[i][j].x);
}

Michalek366
02-18-2002, 01:55 AM
for (j=0;j<2;j++)