Thread: Dealing with the end of an Array

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    63

    Dealing with the end of an Array

    k is an int between 0 and 25 (letters of the alphabet).

    Code:
    in a for loop that increments i
    
       int temp = message[i];
       temp = temp+k;
       message[i] = temp;
    What I'm doing is a letter shift. A=0, B=1, C=2, etc. If k is 0, message[i] remains the same. if k is 1, message[i] is moved from letter 1 (lets say A) to letter 2 (B).

    Here is a hypothetical situation to illustrate my problem:

    k = 25
    message[i] = Z

    ASCII value of 90 + 25 = 115, a lower case "s".

    What I'd do is print out all Upper case letters. So if message[i] reaches 90 it returns back to 65 (A) and increments from there. (In this example that would result in Z translating to 85, the letter U).

    Any ideas?

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I'm confused about exactly what's happening in this program, but:

    >>So if message[i] reaches 90 it returns back to 65 (A) and increments from there.
    You should consider using 'Z' and 'A' rather than 90 and 65, as they are much easier to understand. As for the problem of having it 'wrap', consider using the modulus (%) operator.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    63
    I was using Z and A to begin with...it really confused me.
    I then put in ASCII values and it worked out a lot better. I might not have been perfectly clear in explaining the problem

    all of them are inputted as Z/A/whatever letter, but then changed to ASCII for the inner workings of the program (its easier to make an A a B if all you have to do is add 1)

    modulous operator? Drat! I always forget that its at my disposal :P (I guess I just think of the basic functions on the calculator [*, +, -, =, etc])
    Thanks! *goes back to program to see what happens*

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>but then changed to ASCII for the inner workings of the program
    You should remember though, all characters are stored as ASCII anyways A 'char' is just an int that holds values from -128 to 127, and you can do +1 -1 etc. as you would to any other integral datatype.

    Have fun with modulus
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    63
    Doh! You're right! :P

    Hmm...I can't seem to figure it out.
    I did a google search on wrapping an array with modulus and couldn't find anything to help either.

    Gunna think out loud (or in text I guess..) here...

    So I need anything greater then Z (90) back to A (65) and then continue on....

    while ( temp > 90) { // something over Z
    // wrap back to 65 // go back to A, 26 letters in alphabet
    }
    OK, I put in a temp = (temp-26); and that seemed to work...

    But you've gotten me curious now, how could it be done with modulus?
    Last edited by Philandrew; 10-21-2004 at 10:33 PM.

  6. #6
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Z%26 will always be between 0 and 25 and will loop back to 0 when it hits 26. Add that to 65 and there ya go

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Yes, try something like:
    wrappedValue = ((value - 'A') % ('Z' - 'A' + 1)) + 'A'

    First, bring 'A' to be represented by 0, 'B' by 1, etc. (value - 'A') so that it can be used with modulus. Then, wrap it with modulus (% by number of characters in alphabet), and convert it back to ordinary ASCII (+ 'A'), and you have your answer!

    Note that since it's common knowledge that the alphabet contains 26 characters, you could replace ('Z' - 'A' + 1) with (26). But I just felt like demonstrating the power of ' '
    Last edited by Hunter2; 10-21-2004 at 10:56 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    63
    Ah, I get it! Thanks guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM