Thread: multiple for cycles

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    12

    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;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for(i=16;i<20;i++)
    16 ... 17 ... 18 ... 19
    Code:
    for(k=4;k<8;k++)
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    12
    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

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Last edited by quzah; 06-17-2009 at 04:54 PM. Reason: magenta .. forgot my column
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    12
    This works great!
    Thanks Quzah, was really helpful

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  2. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 06:31 AM
  3. Phantom redefinition
    By CodeMonkey in forum C++ Programming
    Replies: 6
    Last Post: 06-12-2005, 05:42 PM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM