walking through an array in c

Discussion in 'Mac Programming' started by dukebound85, Nov 7, 2006.

  1. dukebound85 macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #1
    hi all, i am trying to walk through a 2 d array to find a sequence of letters in c

    i tried this

    void letterfind(char arr[AMT][AMT], char result[])
    {

    int i;
    int j;

    for(i=0; i< AMT; i++)
    {
    if(result==arr[0]) //row 0 ,any column
    printf("%s",result)
    }

    }

    right now i just want to search the top row for the same letter as the desired word. is this on the right track?

    Thanks for any help
     
  2. jsw Moderator emeritus

    jsw

    Joined:
    Mar 16, 2004
    Location:
    Andover, MA
    #2
    Are you trying to find a string in the array (i.e., find "ing" in "reading is fun"), or trying to see where the array line matches characters in the result string (i.e., result = "ABCDEFG", arr[x] = "JBWDOTG", printed = "BDG")?
     
  3. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #3
    a string in an array. much like a wordsearch puzzle

    thanks
     
  4. jsw Moderator emeritus

    jsw

    Joined:
    Mar 16, 2004
    Location:
    Andover, MA
    #4
    Well, then, a hint I'd give would be to first determine the search string's length. Then step through each row of the array like you're doing until/unless you find the first character of the search string, then see if the array, starting at that point, equals the search string (which means you need to first check if there is enough room left in the row to fit the search string, then see if each successive array character equals the search string's character at that position.

    Non-code:

    Code:
    search_length = length of search string
    
    for each row in array:
      for each character in the row*:
        see if character equals first character in search string
          if so, see if enough room is left in array row to fit search string
            if so, loop through characters in search string to see if they match
    
    
    * technically, you only need to go until you're close enough to the end of the row the the search string would no longer fit
    
     
  5. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #5
    thanks

    i am able to find the first letter of the answer string in the 2d array but am a little confused about how to compare the surrounding letters to the answer string. the answer string is read from top to bottom or left to right

    do i do another for loop?

    thanks for the help
     
  6. DrEasy macrumors member

    Joined:
    Jan 12, 2004
    #6
    Once you find the first letter, you can do a while loop to see if you get a match horizontally (incrementing the column), then another while loop to see if you get a match vertically (incrementing the row this time).
     
  7. dukebound85 thread starter macrumors P6

    dukebound85

    Joined:
    Jul 17, 2005
    Location:
    5045 feet above sea level
    #7
    thanks for all the help its really appreciated
     

Share This Page