Least Common Subsequence

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AnabelleRose
    New Member
    • Jul 2007
    • 4

    Least Common Subsequence

    I have currently been trying to code the Least Common Subsequence Algorithm in C and I am having some basic problems with pointers and arrays. First of all, how do I read in a string and then pass that string to the pointer? To be more clear I will paste a portion of my code (it's not working yet)[code=c]
    #include<stdio. h>
    #include<string .h>
    #include<math.h >
    #include<stdlib .h>
    #define MAX(a,b) (a>b?a:b)


    static int LCS_length(char **ptr_x,char **ptr_y,char **ptr_c,int m,int n);
    static backTrack(char **ptr_c,char **ptr_x,char **ptr_y,int i,int j);
    static print(char **ptr_c,char **ptr_x,char **ptr_y, int i, int j);


    int main()
    {
    char **ptr_x,**ptr_y ,**ptr_c;
    char x,y;
    int m,n;
    int i=0,j=0;
    int index_i,index_j ,index_c;
    printf("Enter the value of m\n");
    scanf("%d",&m);
    printf("Enter the first string\n");
    scanf("%s",x);
    strcpy(ptr_x,x) ;
    [/code] ptr_x is the pointer variable and its pointing to a 2 D array. I need to read in a string and let this ptr_x point to it.
    If i was not using dynamic memory allocation, I would do it as follows:
    Declare an array:[code=c]
    char a[10][10];
    char **ptr_x;
    ptr_x=a;
    printf(enter the value of a);
    scanf("%s",a)[/code]
    I am stuck here, please help me what and how i should use pointers and arrays together when using 2D arrays and dynamic memory allocation.

    I am getting these errors when I compile
    lcs.c:24: warning: passing argument 1 of âstrcpyâ from incompatible pointer type
    lcs.c:24: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast
    lcs.c:35: warning: passing argument 1 of âstrcpyâ from incompatible pointer type
    lcs.c:35: warning: passing argument 2 of âstrcpyâ makes pointer from integer without a cast


    Thanks in advance!
    Anabelle
    Last edited by sicarie; Jul 18 '07, 06:12 PM. Reason: Please use [code=c] and [/code] tags around your code.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Firstly, this code won't compile:
    Originally posted by AnnabelleRose
    int main()
    {
    char a[10][10];
    char **ptr_x;
    ptr_x=a; <<<<<<!!!!!
    printf(enter the value of a);
    scanf("%s",a)
    }
    You cannot assgn a (an array) to a pointer-to-pointer to an int.

    The reason is that the name of an array (in this case a) is the address of element 0. Element 0 if the array is an array of 10 char.

    Therefore, a[0] is an array of 10 char. Therefore the name a is the address of an array of 10 char:

    char (*ptr_x)[10] = a; //OK.

    A char** is a pointer to a pointer to a single int.

    Since you need to read in a string, why not just delcare an array large enough to hold the string:

    char array[100];

    and scanf() into that??

    Later you can treat this array as a 10x10.

    Comment

    • AnabelleRose
      New Member
      • Jul 2007
      • 4

      #3
      I am dynamically allocating memory to the array which is pointed to by the pointer ptr_x. Now my question is, how do I read in a string value from stdin and store it in the **ptr_x? I do not want to do this:

      char a[10][10];
      char **ptr_x;
      ptr_x=(int **) (malloc sizeof(int *)) //allocate memory to ptr_x.
      printf("Enter a string value for a\n");
      scanf("%s",a);
      ptr_x=a; // I don't think this works!
      This is the part where I am stuck.
      I am not sure how to accept string input from the user and store it into a 2D array pointed to by a pointer!

      Thanks,
      Anabelle

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You are making this too hard.

        There are no multi-dimensional arrays on C. There are only one-dimensional arrays.

        Assume you have this in memory:

        RosesAreRedViol etsAreBlue

        There are 25 characters there.

        A char array[25] in memory would look like:

        RosesAreRedViol etsAreBlue

        A char array[5][5] in memory would look like:

        RosesAreRedViol etsAreBlue

        Kinda the same, right?

        So, you allocate as for a one-dimensional array. In your case, allocate 25 chars.

        Read in your string.

        To access the V of Violets you could:

        array[11];

        If this was 2D array of [5][5] then you want array[2][1] or

        array[ 2* 5*sizeof(char) + 1 * 1 * sizeof(char)]

        Otherwise written as array[2][1].

        It's just a question of who does the pointer aritmetic.

        So starting with char array[25] and you string in there

        RosesAreRedViol etsAreBlue

        You could declare a pointer to an array of 5 char:

        char (*ptr_x)[5];

        and then typecast the array name to the address of an array of 5 char.

        ptr_x = (char (*)[5])array;

        Now if you use array, it's one dimensional of 25
        If you use ptr_x it's two-dimensional. Each element is an array of 5 char.

        and the memory you are using is the same memory.

        Comment

        • donthi
          New Member
          • Nov 2008
          • 1

          #5
          1. #include<stdio. h>
          2. #include<string .h>
          3. #include<math.h >
          4. #include<stdlib .h>
          5. #define MAX(a,b) (a>b?a:b)
          6.
          7.
          8. static int LCS_length(char **ptr_x,char **ptr_y,char **ptr_c,int m,int n);
          9. static backTrack(char **ptr_c,char **ptr_x,char **ptr_y,int i,int j);
          10. static print(char **ptr_c,char **ptr_x,char **ptr_y, int i, int j);
          11.
          12.
          13. int main()
          14. {
          15. char **ptr_x,**ptr_y ,**ptr_c;
          16. char x,y;
          17. int m,n;
          18. int i=0,j=0;
          19. int index_i,index_j ,index_c;
          20. printf("Enter the value of m\n");
          21. scanf("%d",&m);
          22. printf("Enter the first string\n");
          23. scanf("%s",x);
          24. strcpy(ptr_x,x) ;






          can u write reaming code for it

          Comment

          • qartar
            New Member
            • Nov 2008
            • 12

            #6
            Given your code:

            char a[10][10];
            char **ptr_x;

            Like cats said, your multi-dimensional array is essentially interpreted as an single dimensional array with a size of 100; as in it is a continuous block of 100 characters. Your ptr_x is pointing to a pointer to an array. So when you use array operators [] with ptr_x it expects to find an array of pointers which would then point to your character array. But your multi-dimensional array just has the 100 characters in it, not the array of pointers that ptr_x expects. Maybe that is helpful? :/

            Comment

            Working...