how to fix this pattern code..

This is a discussion on how to fix this pattern code.. within the C Programming forums, part of the General Programming Boards category; I wrote a program which recognizes the following pattern (num,num) (num,num) (num,num) num is an integer number. I took a ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    how to fix this pattern code..

    I wrote a program which recognizes the following pattern
    (num,num) (num,num) (num,num)

    num is an integer number.

    I took a copy of the input array to temp array.
    then i changed temp like this:
    first i transformed every space char ' ' into '' (no char)
    then i transformed ever number before ',' to 4 and after ',' to 5 .
    so my legal input should look like (4,5) (4,5) (4,5)
    then if the char in the loop is ( it should expect 4,5)
    if the char is 4 it should expect ,5) after it and ) before it

    There are five terms to be completed each cycle
    if the counter%5 =0 then the input is legal.

    i wrote this code but as i showed in the code by comments it has errors
    how to fix them?

    Code:
    #include <stdio.h>
    
    int check_if_legal(char mines[],int length);
    int main() {
            int index;
        char str[40];
        int i;
        int length;
    
     printf("enter length\n");
     scanf("%d",&length);
     printf("enter string\n");
     for(index=0;index<length;index++){
       scanf("%c",&str[index]); //13
     }
     i=getchar();
    
     printf("%d",check_if_legal(str,length));
    return 0;
    }
    
    int check_if_legal(char mines[],int length)
    {
        int state=-1;
        int counter=0;
        int flag_o=0;
        int flag_failure=0;
        int flag_z=0;
        int index2;
        int index;
        char temp[40];
    
    for(index=0;index<length;index++){
       temp[index]=mines[index]; //13
     }
    
         for (index=0;index<length;index++){
             if (temp[index]==' '){      
                 temp[index]='';   ////|39|empty character constant|
    
                 if ((temp[index]<='9')&&(temp[index]=>'0')){ //|41|error: syntax error before '>' token|
                     temp[index]='4';
                     if (temp[index-1]=','){
                        temp[index]='5';
                     }
                     index2=index+1;
                     while((temp[index2]<='9')&&(temp[index2]=>'0')){//|47|error: syntax error before '>' token|
                          temp[index2]=''; //|48|empty character constant|
                         index2++;
                     }//end while
                 }//end if
             }//end if
         }//end for
                  for (index=0;index<length;index++){  // (4,5)
    
                    if(temp[index]=='('){
    
                        if((temp[index+1]=='4')&&(temp[index+2]==',')&&(temp[index+3]=='5')&&(temp[index+4]==')')){
    
                             counter++;
                         }
                         else
                         {
                         state=1;
                         }
                    }
    
                    if(temp[index]=='4'){
    
                       if((temp[index-1]=='(')&&(temp[index+1]==',')&&(temp[index+2]=='5')&&(temp[index+3]==')')){
    
                                     counter++;
                         }
                         else
                         {
                         state=0;
                         }
    
                    }
    
                    if((temp[index]==','){
    
                         if((temp[index-1]=='4')&&(temp[index-2]=='(')&&(temp[index+1]=='5')&&(temp[index+2]==')')){
    
                                     counter++;
                         }
                         else
                         {
                         state=0;
                         }
    
                    }
                    if((temp[index]=='5')){
    
                     if((temp[index-3]=='(')&&(temp[index-1]==',')&&(temp[index-2]=='4')&&(temp[index+1]==')')){
    
                                      counter++;
                         }
                         else
                         {
                         state=0;
                         }
    
                    }
                    if((temp[index]==')'){
    
                        if((temp[index-1]=='5')&&(temp[index-2]==',')&&(temp[index-3]=='4')&&(temp[index-4]==')')){
    
                                      counter++;
                         }
                         else
                         {
                         state=0;
                         }
    
                    }
                   }//end for
                   if ((counter>0)&&(counter%5==0){
    
                    state=1;
                   }
    
         return state;
     }//end func

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Post the errors you're getting. I did notice that you have too many opening parentheses here and elsewhere:
    Code:
    if((temp[index]==')'){
    Code:
    temp[index]='';   ////|39|empty character constant|
    You can't really have an empty character constant like that. I think perhaps you want to shift all of the characters to the right of temp[index] left one index, so that you effectively overwrite temp[index]. You can do this quite easily with memmove(), or implement it yourself.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i posted each error as a comment near the line

    i am pretty sure i could replace ' ' with '' i have done it in java

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by transgalactic2
    i am pretty sure i could replace ' ' with '' i have done it in java
    I am pretty sure that Java is not C and C is not Java. As you pointed out, the error message is right there: "empty character constant".

    Actually, Java does not allow empty character literals either. You might be thinking of empty strings, which both C and Java allow, except that the C version would consist of a null character as the first character.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so how to replace ' ' to an empty char
    ??

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so it will put the next char instead of it?

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Let's start from scratch. You want to check if user input matches this pattern: (num,num) (num,num) (num,num).

    First, does this mean that num is always the same, e.g., you will accept (2,2) (2,2), (2,2) but reject (1,2) (3,4) (5,6)?

    Now, the first step that I would take is to write code that matches a number. Then I would extend that to match (num,num). Finally, I would extend that to match the full pattern. I would not attempt to write the code to match the entire pattern all at once.

    Given that the pattern is pretty simple, I would most likely not attempt to replace or move characters.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    no
    if i see a number i put instead of the first digit 4 or 5 (depends on the location with respect of ',' location to it) and cut off the rest of the digits of the number


    4 or 5 are just sign that there is some number here
    i compose a BASIC STRUCTURE OF MY INPUT
    deleting spaces an changing each number to a single digit
    just to say "here is a number"
    this function only determines if the input is legal
    (1,2) (3,4) (5,6) is a legal input
    Last edited by transgalactic2; 12-27-2008 at 04:25 AM.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by transgalactic2
    4 or 5 are just sign that there is some number here
    i compose a BASIC STRUCTURE OF MY INPUT
    deleting spaces an changing each number to a single digit
    just to say "here is a number"
    Although you could do that, I think that it is unnecessary.

    I am going to give you a skeleton of a possible test program:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int is_valid(const char *input);
    
    int main()
    {
        char input[] = "(1,2) (34,5) (67,8)";
        if (is_valid(input))
        {
            printf("%s is valid.\n", input);
        }
        else
        {
            printf("%s is invalid.\n", input);
        }
    
        return 0;
    }
    
    /* Match one or more consecutive digits and return a pointer to the first char
     * that is not a digit.
     */
    const char *match_number(const char *input)
    {
        /* Fill in your code here (e.g., by using isdigit()) */
    
        return input;
    }
    
    /* Match "(<number>,<number>)" pattern.
     * Return a pointer to the first char after the matched pattern, or
     * NULL if there is no match.
     */
    const char *match_pair(const char *input)
    {
        if (*input != '(')
        {
            return NULL;
        }
    
        input = match_number(input + 1);
    
        /* Fill in your code here */
    
        return input + 1;
    }
    
    /* Match "(<number>,<number>) (<number>,<number>) (<number>,<number>)" pattern.
     * Return 1 if there is a match and 0 otherwise.
     */
    int is_valid(const char *input)
    {
        input = match_pair(input);
        if (input == NULL)
        {
            return 0;
        }
    
        /* Fill in your code here */
    
        return *input == '\0';
    }
    Notice that instead of your "replace number with a single digit" strategy, I have a function named match_number that simply matches an entire number. I break up the pieces of the pattern into subpatterns that are matched with functions. In this case the pattern is simple enough to just use two such auxiliary functions (and in fact match_number is so simple that I could have integrated it into match_pair without sacrificing readability).
    Last edited by laserlight; 12-27-2008 at 05:13 AM. Reason: Minor fix for const-correctness.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried to write the function you said
    what is the algorithm for the is_valid function?

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int is_valid(const char *input);
    
    int main()
    {
        char input[] = "(1,2) (34,5) (67,8)";
        if (is_valid(input))
        {
            printf("%s is valid.\n", input);
        }
        else
        {
            printf("%s is invalid.\n", input);
        }
    
        return 0;
    }
    
    /* Match one or more consecutive digits and return a pointer to the first char
     * that is not a digit.
     */
    const char *match_number(const char *input)
    {
        while((*input<='9')&&(*input=>'0')){
         
         input++; //increasing the  address by1   
        }
    
        return input;
    }
    
    /* Match "(<number>,<number>)" pattern.
     * Return a pointer to the first char after the matched pattern, or
     * NULL if there is no match.
     */
    const char *match_pair(const char *input)
    {
        if (*input != '(')
        {
            return NULL;
        }
    
        input = match_number(input + 1);
    
        if (*input=','){
               input = match_number(input + 1);
        }
       if (*input=')'){
               
        }
        else {
         input=NULL;   
        }
        return input + 1;
    }
    
    /* Match "(<number>,<number>) (<number>,<number>) (<number>,<number>)" pattern.
     * Return 1 if there is a match and 0 otherwise.
     */
    int is_valid(const char *input)
    {
        input = match_pair(input);
        if (input == NULL)
        {
            return 0;
        }
    
        /* Fill in your code here */
    
        return *input == '\0';
    }

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Quote Originally Posted by transgalactic2
    what is the algorithm for the is_valid function?
    Very straightforward: use match_pair() and character comparison with space to check that the input matches the pattern. If at any point match_pair() returns a null pointer or the character in between each pair is not a space, return 0 immediately.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    did you mean that?
    Code:
    const char *match_pair(const char *input)
    {
        if (*input != '(')
        {
            return NULL;
        }
    
        input = match_number(input + 1);
    
        if (*input=','){
               input = match_number(input + 1);
        }
       if (*input=')'){
               
        }
        else {
         input=NULL;   
        }
        return input + 1;
    }

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    Yes, something like that, though you should be using != instead of = and an empty if block is quite meaningless. You should not be assigning NULL to input since you should return NULL immediately.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i fixed it to..
    Code:
    const char *match_pair(const char *input)
    {
        if (*input != '(')
        {
            return NULL;
        }
    
        input = match_number(input + 1);
    
        if (*input==','){
               input = match_number(input + 1);
        }
       if (*input==')'){
               
        }
        else {
         return NULL;   
        }
        return input + 1;
    }

  15. #15
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    did i wrote is_valid correctly??
    Code:
    int is_valid(const char *input)
    {
        input = match_pair(input);
        if (input == NULL)
        {
            return 0;
        }
    
        else{
    
        return 1;
        }
        
    }

Page 1 of 8 12345678 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-20-2009, 08:35 AM
  2. Replies: 28
    Last Post: 07-17-2006, 12:35 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21