Thread: Wrap around in array

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    44

    Wrap around in array

    ppl,

    I want to wrap around in a array. I now use this code but I would like
    to start at position 3 and end at position 2. It needs to be something with modulo but I can't figure out how. Keep in mind that pos is a variable that
    gets changed just like the board, this is only a simplified version of a function I use.

    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int board[] = {9, 8, 6, 3, 4, 1, 3, 5, 2};
      int pos = 3;
    
      int i;
        for ( i = 0; i < 10; i++ )
            if (pos == i)
                continue;
            else if (board[i] == board[pos])
                printf ( "%d same wrong  \n", board[i] );
                //return true to use in other part of the program  
    return 0;
    }
    I've seen something like this with a for loop around it
    printf ( "%d ", a[i % 9] );
    But i would like to use it in a for loop so it don't start at zero but at pos & wraps around.

  2. #2
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    There are 9 elements in your array, indexed 0 -8, your for loop overruns your array. Your question is a little strange, could you explain what you mean by wrap around your array? If you don't want to start at 0, don't start at 0. for(int i=pos;i<9;i++)....
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So put "printf("%d", a[i%9]);" in a for loop. What's stopping you?
    Code:
    for (i = 4; i < 35; i++) {
        printf("%d", a[i%9]);
    }

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    Quote Originally Posted by tabstop View Post
    So put "printf("%d", a[i%9]);" in a for loop. What's stopping you?
    Code:
    for (i = 4; i < 35; i++) {
        printf("%d", a[i%9]);
    }
    I also would like to check the elements before 4, thats my problem! Only skip the position where I'm at, but elements before an after I would like to check there values.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Oh, I see what he was asking for now.....
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by lamko View Post
    I also would like to check the elements before 4, thats my problem!
    What makes you think that doesn't?

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by lamko View Post
    I also would like to check the elements before 4, thats my problem! Only skip the position where I'm at, but elements before an after I would like to check there values.
    Do us a favor and run this code:
    Code:
    #include <stdio.h>
    
    int main(void){
    
    	for(int i=4;i<35;i++)
    		printf("%d ", i%9);
    
    	getchar();
    	return (0);
    }
    See if you see a pattern.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    In your example it works, but how do you implement it in mine example

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by tabstop View Post
    What makes you think that doesn't?
    Can I have that hammer back? I think I might need it.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by lamko View Post
    In your example it works, but how do you implement it in mine example
    Look at tabstops post, then look at my post and put the two together.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The question is (to me, anyway): you know full well that there are nine entries in the array (you need to know this, otherwise you don't know what to do the % by). Is it important that you go from 3 to 2 instead of 0 to 9? If for some reason it is, then do so:
    Code:
    for (i = pos; i < pos+9; i++) {
        /* NEVER use i by itself in here, always i%9 */
    }

  12. #12
    Registered User
    Join Date
    Jul 2011
    Posts
    44
    That was it, what I was looking for, so simple pos+9 but I was just looking over it.

    Code:
    #include <stdio.h>
    
    int main ( void )
    {
      int board[] = {9, 8, 6, 3, 4, 1, 3, 5, 2};
      int pos = 3;
    
      int i;
        for ( i = pos; i < pos+9; i++ )
            if (pos == i % 9)
                continue;
            else if (board[i % 9] == board[pos])
                printf ( "%d same wrong  \n", board[i] );
                //return true to use in other part of the program  
    return 0;
    }
    Thx guys now to see if its working right!
    Last edited by lamko; 08-17-2011 at 11:29 AM.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Read the comment I put in again.

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by tabstop View Post
    Code:
    for (i = pos; i < pos+9; i++) {
        /* NEVER use i by itself in here, always i%9 */
    }
    Quote Originally Posted by lamko View Post
    Code:
        for ( i = pos; i < pos+9; i++ )
            if (pos == i % 9)
                continue;
            else if (board[i % 9] == board[pos])
                printf ( "%d same wrong  \n", board[i] );
                //return true to use in other part of the program  
    return 0;
    }
    Hmm....it would appear that handing out the answers doesn't even work. (we should have a disgusted emoticon)
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you want to start at 3 and end at 2 just do this...
    Code:
    int board[9];    // board size
    int pos = 3;     // start position
    int stop = 2;    // stop position
    
    while( pos != stop)
      { //do stuff to your board
    
        printf("%d\t%d\n",pos, board[pos];  // diagnostic
    
        pos = ++pos % 9; }  // advance around circle
    Last edited by CommonTater; 08-17-2011 at 11:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. It's a wrap!
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 06-10-2007, 02:00 PM
  2. Word wrap
    By Orrill in forum C++ Programming
    Replies: 6
    Last Post: 10-14-2005, 02:00 PM
  3. Word Wrap
    By sethjackson in forum Windows Programming
    Replies: 4
    Last Post: 09-21-2005, 04:35 PM
  4. Word Wrap
    By osal in forum Windows Programming
    Replies: 4
    Last Post: 07-02-2004, 11:16 AM
  5. word wrap
    By max in forum C Programming
    Replies: 6
    Last Post: 08-24-2002, 10:44 AM