-
multiple for cycles
I want to copy selected elements from array stdC to total,
but instead I only get the same three numbers every time, can you help me?
thanks
Code:
#include <stdio.h>
int main()
{
int i,j,k;
float stdC[12][3]={
{-4.859,0.073,0.122},
{-3.610,-0.654,0.017},
{-2.432,0.308,0.088},
{-2.621,1.515,0.217},
{-1.213,-0.231,0.004},
{-0.016,0.584,0.061},
{0.794,0.422,-1.218},
{1.222,-0.680,-1.549},
{1.005,1.527,-1.937},
{1.760,1.497,-3.174},
{1.839,2.893,-3.777},
{1.303,3.846,-3.217},
};
float total[28][3];
for(i=0;i<28;i++)
for(j=0;j<3;j++)
total[i][j]=0;
for(i=16;i<20;i++)
for(j=0;j<3;j++)
for(k=4;k<8;k++)
total[i][j]=stdC[k][j];
for(k=0;k<28;k++)
{
{
for(j=0;j<3;j++)
printf("test %.3f\t",total[k][j]);
printf("\n");
}
}
return 0;
}
-
16 ... 17 ... 18 ... 19
4 ... 5 ... 6 ... 7
I'm not sure what you're really trying to do here, but you should be getting four items. Not three. Let's see:
i = 16
j = 0
k = 4
total[ 16 ][ 0 ] = stdC[ 4 ][ 0 ]
total[ 16 ][ 0 ] = stdC[ 5 ][ 0 ]
total[ 16 ][ 0 ] = stdC[ 6 ][ 0 ]
total[ 16 ][ 0 ] = stdC[ 7 ][ 0 ]
j = 1
All the 0 in the above assignments get set to 1. But the end result is pretty much the same. You copy four values into the same spot.
Is that what you were trying to do?
Quzah.
-
Sorry for the confusion (english is not my mother language)
The three numbers I got as output is a complete row, then I got the same row repeated four times, like this,
1.222 -0.680 -1.549
1.222 -0.680 -1.549
1.222 -0.680 -1.549
1.222 -0.680 -1.549
As you stated above, these are the equivalents for
total[ 16 ][ 0 ] = stdC[ 4 ][ 0 ]
total[ 16 ][ 0 ] = stdC[ 5 ][ 0 ]
total[ 16 ][ 0 ] = stdC[ 6 ][ 0 ]
total[ 16 ][ 0 ] = stdC[ 7 ][ 0 ]
What I need is the following
total[ 16 ][ 0 ] = stdC[ 4 ][ 0 ]
total[ 17 ][ 0 ] = stdC[ 5 ][ 0 ]
total[ 18 ][ 0 ] = stdC[ 6 ][ 0 ]
total[ 19 ][ 0 ] = stdC[ 7 ][ 0 ]
Thanks for your answer
-
You have one too many layers of looping then.
Code:
T = what 'total' slot to start at.
S = what 'stdC' slot to start at.
R = total count, how many rows you copy
C = how many columns you have
for( tstart = T, sstart = S, count = 0; count < R; count++ )
{
for( column = 0; column < C; column++ )
{
total[ tstart + count ][ column ] = stdC[ sstart + count ][ column ];
}
}
That should copy each column in one row, to another row and matching column.
Quzah.
-
This works great!
Thanks Quzah, was really helpful