Thread: A loop solution

  1. #1
    ---
    Join Date
    May 2004
    Posts
    1,379

    A loop solution

    I can't figure out how I can put this in a loop. It is probably really simple but I cant think straight right now.
    I tried some things with the % operator but my logic is off

    Code:
      map_x[0] = tmp[0];
      map_y[0] = tmp[1];
      map_x[1] = tmp[2];
      map_y[1] = tmp[3];
      map_x[2] = tmp[4];
      map_y[2] = tmp[5];
      map_x[3] = tmp[6];
      map_y[3] = tmp[7];
      map_x[4] = tmp[8];
      map_y[4] = tmp[9];
    the sizes of map_x, map_y and tmp are not know until runtime.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There are multiple ways of doing this.

    You could use one counter/variable:
    1) Transform the number into a counter.
    2) If the counter is odd, place the value in the "y array", in the slot where the counter is divided in half. Otherwise put it in the "x array" using the same method.
    3) Repeat until done.

    Or you could use multiple variables:
    1) Have a loop counter, an "x counter" and a "y counter".
    2) Each time through the loop, put the value of the big counter into the x counter. Then increment the big counter, and do the same for the y counter. Now increment the big counter once more.
    3) Repeat until done.

    There are probably a few other fun ways of doing it, but try both of those and see what one you like better. But definately try both of them as a learning experience.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    If the size of map_x is equal to the size of map_y, and the size of tmp is equal to 2 * the size of map_x, then you can do something like this:
    1) Use a single counter incremented by one on each iteration of the loop, up to the size of map_x
    2) The index of map_x and map_y is equal to the counter
    3) The index of tmp into map_x is equal to the counter * 2
    4) The index of tmp into map_y is equal to the counter * 2 +1

    You should be able to assign a value to both map_x and map_y on each iteration through the loop.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    here is my solution
    Code:
      int i,j=0;
      for(i=0;i<sizeof(tmp);i++){ //line 37
        if(i%2){
          map_x[j] = tmp[i];
        }  
        else{ 
          map_y[j] = tmp[i]; 
          j++;
        }
      }
    This gives me the right output but i get a warning.
    Code:
     Line 37: [Warning] comparison between signed and unsigned integer  expressions
    And the program crashes on exit :s

    nm Im going to go back and redesign this function. I'm having more problems with it now. It's time to find a different approach.
    Last edited by sand_man; 03-02-2005 at 12:52 AM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you passing an array (tmp) to this function? If so, you can't use sizeof there. sizeof( arrayname ) on an array passed to a function gives you the size of the pointer, not the array. You can only use that if the array is declared directly in scope.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM