Thread: using modulus & weighting factors

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    66

    using modulus & weighting factors

    What is wrong with this code:

    #include <stdio.h>
    #include <string.h>
    #define MODULUS 11

    void main()
    {
    char code[] = "24686", weight[] = "23456", *wgt_ptr = weight[ ( strlen( code ) - 2) ];
    int loop, tot = 0;

    for( loop = 0; loop < ( strlen( code ) ) - 1; loop++ )
    {
    tot = ( ( code[loop] - '0') * ( ( wgt_ptr-- ) - '0' ) );
    }
    tot = MODULUS - ( tot % MODULUS );
    if( tot != code[ strlen( code ) ] )
    printf("Invalid code");
    }

    I think the problem lies with if( tot != .....

    What I want is for a function to be able to validate code[] using a modulus of 11 and a weight factor of 5432 if code[] is 5 characters long and 65432 if code[] is 6 characters long.

    By the way code[] wouldn't be a fixed literal normally that was just for the sake of testing it.

    T
    T

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    - It has no code tags surrounding it.

    - void main()
    nuff said. Search the boards if you don't know why.

    - *wgt_ptr = weight[(strlen(code) - 2)]
    This is all a bit wrong. What are you trying to do here? A quick fix is this, but I don't know if it's doing what you want:
    >*wgt_ptr = &weight[(strlen(code) - 2)];

    - (wgt_ptr--) - '0')
    What is this supposed to be doing? Did you mean
    >(*wgt_ptr--) - '0')

    There might be more...
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Here's my version of the code...I'm only guessing on what you're trying to accomplish. Perhaps you should clarify:

    Let's address some errors first:
    1) wgt_ptr-- should be *wgt_ptr-- since you want the value of it and not the address where it's pointing to. Also when you initialized it it should be: *wgt_ptr = &weight....

    2) if( tot != code[ strlen( code ) ] )
    Your string has 5 characters so you will get code[5] but the 5th index contains the '\0' character. Change it to strlen(code) - 1 if you really wanted the last character in your string.

    3) I don't understand your for loop. It keeps overwriting the old tot value everytime it loops. So why don't you run it once with this:
    tot = (code[strlen(code)-2] - '0') * (weight[0] - '0');
    It does the exact same thing as your for loop.

    Code:
    #include <stdio.h>
    #include <string.h>
    #define MODULUS 11
    
    int main()
    {
       char code[] = "24686", weight[] = "23456", *wgt_ptr = &weight[ ( strlen( code ) - 2) ];
       int loop, tot = 0;
    
       for( loop = 0; loop < ( strlen( code ) ) - 1; loop++ )
       {
          tot = ( ( code[loop] - '0') * ( ( *wgt_ptr-- ) - '0' ) );
       }
       tot = MODULUS - ( tot % MODULUS );
       if( tot != code[ strlen( code )-1 ] - '0')
          printf("Invalid code");
       return 0;
    }
    Again, I'm not sure what you're doing. Hope this gets you on the right track though.

    EDIT - yeah hammer, there was more
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    66
    int main()
    {
    char code[] = "24686", weight[] = "23456", *wgt_ptr = &weight[ ( strlen( code ) - 2) ];
    int loop, tot = 0;

    for( loop = 0; loop < ( strlen( code ) ) - 1; loop++ )
    {
    tot+= ( ( code[loop] - '0') * ( ( *wgt_ptr-- ) - '0' ) );
    }
    tot = MODULUS - ( tot % MODULUS );
    if( tot != code[ strlen( code )-1 ] - '0')
    printf("Invalid code");
    return 0;
    }

    The purpose of the loop was to accumalate the total. It should of been tot+= not tot =
    Sorry
    T

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    66
    int main()
    {
    char code[] = "24686", weight[] = "23456", *wgt_ptr = &weight[ ( strlen( code ) - 2) ];
    int loop, tot = 0;

    for( loop = 0; loop < ( strlen( code ) ) - 1; loop++ )
    {
    tot+= ( ( code[loop] - '0') * ( ( *wgt_ptr-- ) - '0' ) );
    }
    tot = MODULUS - ( tot % MODULUS );
    if( tot != weight[ strlen( code )-1 ] - '0')
    printf("Invalid code");
    return 0;
    }

    /* And if( tot != code[strlen( code ) -1] - '0') should be
    if(( tot != weight[ strlen( code) -1] - '0')

    Sorry again.

    T
    T

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-19-2009, 10:32 PM
  2. Replies: 3
    Last Post: 03-29-2005, 04:24 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Help with homework please
    By vleonte in forum C Programming
    Replies: 20
    Last Post: 10-13-2003, 11:16 AM
  5. How do I do a program with factors?
    By Desperado in forum C Programming
    Replies: 3
    Last Post: 12-07-2001, 06:03 PM