C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-02-2009, 05:29 AM   #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?
osiris^ is offline   Reply With Quote
Old 12-02-2009, 06:33 AM   #2
Jack of many languages
 
Dino's Avatar
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 2,131
Show example please.
__________________
Mac and Windows cross platform programmer. Ruby lover.
Dino is offline   Reply With Quote
Old 12-02-2009, 08:19 AM   #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.
osiris^ is offline   Reply With Quote
Old 12-02-2009, 10:19 AM   #4
Jack of many languages
 
Dino's Avatar
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 2,131
Is this a class assignment, such that you cannot use the obvious solution?
__________________
Mac and Windows cross platform programmer. Ruby lover.
Dino is offline   Reply With Quote
Old 12-02-2009, 10:30 AM   #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.
osiris^ is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:05 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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