Methods to keep a number within a certain range?

This is a discussion on Methods to keep a number within a certain range? within the C Programming forums, part of the General Programming Boards category; Hi all, I have numbers X and Y, which have to be used to perform a calculation such that the ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    42

    Methods to keep a number within a certain range?

    Hi all,

    I have numbers X and Y, which have to be used to perform a calculation such that the resulting number, Z, is always within a particular range. However, as I'll need to find the inverse of this calculation to return to X or Y, I cannot use the modulus operator.

    Anyone got any ideas?

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Show example please.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Well, I didn't need any code to do it, I just wanted ideas on how to do it.

    However, here's the code which gets the unicode numbers from a string called 'plain'. Each character of the array refers to a number iin nums[], which corresponds to A from my original post.

    Code:
    for (m = 0; m < strlen(plain); m++)
            {
                nums[m] = (int)plain[m];
            }
    Here is the code which gets the unicode numbers from a string called 'key_ext'. Each character of which refers to a number in keynums[], which is B in my original post.

    Code:
    for (m = 0; m < strlen(key_ext); m++)
            {
                keynums[m] = (int)key_ext[m];
            }
    For example, if nums[0] was 56 and keynums[0] was 109, I need to do something specific to these two numbers to consistently produce a result between 32 and 127. The method by which this is done would also need to be reverseable.

    I'm not really sure how to do this, and would like to know if anyone can throw in some ideas.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Is this a class assignment, such that you cannot use the obvious solution?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    42
    Quote Originally Posted by Dino View Post
    Is this a class assignment, such that you cannot use the obvious solution?
    It's part of an encryption program assignment, which is why I cannot use the mod operator, as the inverses aren't always precise, so I may end up with multiple solutions when I try to decrypt. This specific section is using the Vernam Cipher, but it's only one part of my encryption/decryption algorithm. My full algorithm involves the above mentioned vernam cipher, shift substitutions and columnar transpositions over multiple iterations.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking the number (int/double) range
    By l2u in forum C++ Programming
    Replies: 7
    Last Post: 08-05-2008, 05:27 AM
  2. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  3. Stone Age Rumble
    By KONI in forum Contests Board
    Replies: 30
    Last Post: 04-02-2007, 09:53 PM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

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