Thread: help with searching a string on a char matrix.

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    26

    help with searching a string on a char matrix.

    So i started learning c quite recently and have a question.

    I want to make a program that receives the number of lines and collumns of a matrix, a matrix of char, the number of pairs of coordinates and the coordinates. the program has to return the char (or chars) that are on those coordinates on the matrix.

    ---------------------------------------------------
    example:

    receives

    2 3
    ABC
    DEF
    2
    1 1
    1 2

    returns

    AB

    ---------------------------------------------------

    this is how i tried to solve this problem:

    ---------------------------------------------------


    Code:
    #include 
    #define MAX 1000
    
    int main() {
      int nlin, ncol;
      char mat[MAX][MAX];
      int x[MAX], y[MAX];
      int ncoords;
      int l, c, n;
    
    /* receiving variables and storing matrix.*/
    
      scanf("%d%d\n", &nlin, &ncol);
      for(l = 0; l < nlin; l++) {
        for(c = 0; c < ncol; c++)
          mat[l][c] = getchar();
        getchar(); /* read the \n at the end of each line */
      }
      scanf("%d", &ncoords);
      for(n = 0; n < ncoords; n++) {
        scanf("%d%d", &x[n], &y[n]);
      }
    
      /*finding string with the received coordinates*/
    
        int f;
        int e;
        int r;
        char result [1000];
    
        for ( r=0; ncoords != 0; ncoords--,r++,f++) {
            for(l=1,f=0;x[f]!=l;l++);
            for(c=1,e=0;y[e]!=c;c++)
            result[r]=mat[f][e];
        }
        printf("found string '%s'\n",result);
        return 0;
    }
    ---------------------------------------------------


    For some reason that i can't seem to find, this solution is not right, may someone explain me why and point me into the right direction?
    Last edited by Lired; 04-12-2014 at 09:53 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Code:
            for(l=1,f=0;x[f]!=l;l++);
            for(c=1,e=0;y[e]!=c;c++)
            result[r]=mat[f][e];
    The ; at the end of the first loop means it does nothing.
    What are the values of f and e when those loops exit?

    It seems an excessively complicated version of something like
    result[r] = mat[x[r]-1][y[r]-1];


    > printf("found string '%s'\n",result);
    You need to make sure your result has a \0 in the right place.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    26
    Quote Originally Posted by Salem View Post
    Code:
            for(l=1,f=0;x[f]!=l;l++);
            for(c=1,e=0;y[e]!=c;c++)
            result[r]=mat[f][e];
    The ; at the end of the first loop means it does nothing.
    What are the values of f and e when those loops exit?

    It seems an excessively complicated version of something like
    result[r] = mat[x[r]-1][y[r]-1];


    > printf("found string '%s'\n",result);
    You need to make sure your result has a \0 in the right place.

    yes you are right about the \0, i forgot about that.

    And i'm sorry i made a mistake while writing the code here. i will edit on the main post.

    its suposed to be like:

    Code:
    for (r=0; ncoords!=0;ncoords--,r++,e++,f++) {
            for(l=1,f=0;x[f]!=l;l++);
            for(c=1,e=0;y[e]!=c;c++);
            result[r]=mat[f][e];
    }

  4. #4
    Registered User
    Join Date
    Apr 2014
    Posts
    26
    Quote Originally Posted by Salem View Post
    [code]

    The ; at the end of the first loop means it does nothing.

    Why do you say it does nothing? the objective is to make the l and c (lines and collumns) the value that the user inserted before on the previous scanf.

    so yeah it must be

    result[r]=mat[l][c];

    intead of what i wrote before. but that still doesnt make it right. there is something else that is not right...

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Lired
    Why do you say it does nothing?
    Well, let's write this:
    Code:
    for(l=1,f=0;x[f]!=l;l++);
    for(c=1,e=0;y[e]!=c;c++)
    result[r]=mat[f][e];
    In an equivalent form, with proper indentation:
    Code:
    for (l = 1, f = 0; x[f] != l; l++)
    {
        /* do nothing in the loop body */
    }
    
    for (c = 1, e = 0; y[e] != c; c++)
    {
        result[r] = mat[f][e];
    }
    Obviously, the first for loop has no net effect beyond incrementing l. If this is not what you intended, then the semi-colon at the end of the first for loop is a mistake.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2014
    Posts
    26
    Now i see, that code you wrote,

    Code:
    result[r] = mat[x[r]-1][y[r]-1];

    it's much simpler and works without any problem. And it makes much more sense then what i was thinking... Thanks for the help!

  7. #7
    Registered User
    Join Date
    Apr 2014
    Posts
    26
    the objective was to increment l, as the objective of the second loop was to increment c. but only increment them to the point they are the same the user writes on the previous scanf.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  2. Searching word(string) in matrix.
    By Aleremilcapo in forum C Programming
    Replies: 7
    Last Post: 09-22-2010, 09:41 PM
  3. Replies: 2
    Last Post: 09-12-2010, 09:15 AM
  4. What to use for searching var for char
    By blackcell in forum C Programming
    Replies: 1
    Last Post: 03-05-2008, 09:08 PM
  5. Help with searching for a char in a string
    By -Prime- in forum C Programming
    Replies: 3
    Last Post: 11-09-2006, 04:02 PM

Tags for this Thread