[Tutoral] Multidimensional Arrays.

Discussion in 'iOS Development' started by SkylarEC, Jan 11, 2009.

  1. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    For some reason, whenever I post anything here or anywhere else about creating multidimensional arrays, people never tend to respend, or tend to flat out insist that they have no clue about a two dimensional array.


    I honestly never really cared whether or not you guys opted to follow the advice I gave, or looked up how to proceed following my advice. However, I was recently going through some old crap from those couple of semesters I actually went to college, and found something that might help you out. One of my assignments in my C++ class was to create a "Decoder ring." That is, a program where the user could enter a string, and have it accurately encoded and decoded. I recompiled the app using xcode, so I will guarantee its working on at least that. Then, I added a "Self destruct" message and made the app sleep for five seconds. Aside from those two additions, this is the exact code I turned in and got a perfect grade on.

    Looking back it, I'll admit that it can use some improvements. Regardless, it shows you how to handle a two dimensional array. In this case, the array handles the actual cipher.

    Note, I don't go into detail about using a multidimensional array. That is because it is exactly the same as using a single dimensional array. And yes, you can apply this to your Objective-C, and it will work.

    [OBJC]#include <iostream>
    #include <cctype>

    using namespace std;

    void enter_string(char *lPtr);
    void encode (char *lPtr, const char key[][37]);
    void decode(char *lPtr, const char key[][37]);
    void disp_string (char *lPtr);

    int main() {
    const char key[2][37] = {
    {' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'},
    {'g', 'p', 'r', 'h', '9', 'w', 'q', '4', 'b', 'v', '1', 'c', 'o', '6', 'l', 'd', '5', 'f', 'x', 'e', 's', '7', '8', 't', 'k', 'i', 'u', '3', 'y', 'z', ' ', '2', 'j', 'm', '0', 'a', 'n'}};
    char line[256]; //the user's line to encode
    char flag = 'y'; //the continue or not flag (set to continue)
    char response; //the user's menu choice
    char sflag; //is needed to loop the switch
    int enCtr = 0; //encoder counter

    cout << "\n\n\n\t\t\tSUPER ENCODER/DECODER RING!\n"
    << "\t\t\t(Put this on your finger!)\n";

    do {

    cout << "\n\nTOP SECRET MENU:\n"
    << "\n 1. (E)nter a new message."
    << "\n 2. E(n)code message."
    << "\n 3. (D)ecode Message."
    << "\n 4. Di(s)play the current message."
    << "\n 5. (Q)uit.\n\n";
    sflag = 'l';

    do {
    cin >> response;

    switch (response) {
    case '1':
    case 'E':
    case 'e':
    enter_string(line);
    sflag = 'c';
    break;
    case '2':
    case 'N':
    case 'n':
    encode (line, key);
    enCtr++;
    sflag = 'c';
    break;
    case '3':
    case 'D':
    case 'd':
    do{
    decode(line, key);
    sflag = 'c';
    enCtr--;
    }while (enCtr > 0);
    break;
    case '4':
    case 'S':
    case 's':
    disp_string(line);
    sflag = 'c';
    break;
    case '5':
    case 'Q':
    case 'q':
    cout << "This ring will self destruct in 5 seconds!" << endl;
    sleep(5); //Added for effect. Feel free to remove.
    return 0;
    break;
    default:
    "\nEnter the letter in parentheses, or the number option, please!\n";
    sflag = 'l';
    }
    } while (sflag =='l');

    } while((flag !='n') || (flag != 'N'));

    return 0;
    }


    /////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////

    void enter_string(char *lPtr) {
    cout << "Enter new message:\t";
    cin >> lPtr;
    }


    /////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////

    void disp_string(char *lPtr) {

    cout << "The messgage currently is:\t";

    for( ; *lPtr != '\0'; lPtr++)
    cout << *lPtr;

    cout << "\n\n";
    }


    /////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////

    void encode(char *lPtr, const char key[][37]) {

    for( ; *lPtr != '\0'; lPtr++) {
    if (isupper(*lPtr))
    *lPtr = tolower(*lPtr);
    for (int i = 0; i <= 36; i++) {
    if (*lPtr == key[0]) {
    *lPtr = key[1];
    break;
    }
    }
    }
    }


    /////////////////////////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////////////////////////

    void decode(char *lPtr, const char key[][37]) {

    for( ; *lPtr != '\0'; lPtr++){
    for (int i = 0; i <= 36; i++) {
    if (*lPtr == key[1]) {
    *lPtr = key[0];
    break;
    }
    }
    }
    }[/OBJC]
  2. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Looks very interesting (really)

    Please Register or Log in to view images

    Please Register or Log in to view images



    I just don't have the maths brain right now, i may give it a try, but it looks complicated.

    Thanks for your contribution though!!!!!!!!!!! (really)
  3. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    LOL @ 'Top Secret Menu'. =]

    Fairly simple application, but still a fun one! I made something like this a while back that took a text file that i passed as an argument, and it decoded the entire file, and then decoded it when i used the other argument. Nice work.
  4. JoshuaCaputo

    JoshuaCaputo New Member

    Joined:
    Aug 2, 2008
    Messages:
    605
    Likes Received:
    0
    Device:
    iPod touch
    Well, I seem like an ass now huh?
  5. Chicken

    Chicken Retired Moderator

    Joined:
    Feb 17, 2008
    Messages:
    2,494
    Likes Received:
    7
    Device:
    iPhone 5S
    Looks really intresting may be useful when I become better

    P.S. JoshuaCaputo yes that really does make you look like an ass
  6. JoshuaCaputo

    JoshuaCaputo New Member

    Joined:
    Aug 2, 2008
    Messages:
    605
    Likes Received:
    0
    Device:
    iPod touch
    and sadly im still at a loss. haha
  7. centroid

    centroid New Member

    Joined:
    Nov 12, 2007
    Messages:
    405
    Likes Received:
    0
    Device:
    iPad 2 (Black)
    learning this in CS class right now. At your "from those couple of semesters I actually went to college"... I must be some school nerd cause I actually enjoy my CS classes.. they just seem enjoyable compared to physics or history...
  8. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    I was just looking at tutorials here and found this one, and I must say it's fun how people find workarounds to do some things.

    Like, a simple one, instead of putting it in a while with a condition, you could have put it all in a while(1) and in case 5, etc, send an exit signal exit(0) or something like that.

    // Warning, complex stuff!
    And for a real encoding, you could first ask for a password, cast the characters to their ASCII value, and add them or concatenate them, then use a Linear Feedback Shift Register to get an 8 bit pseudo random integer.

    Then do an XOR with that int and every character in the string, save the resultant number and convert it to char, and store a new string with those new characters.
    When you apply the same process to the new string, the result will be the original one (that's why we like XOR so much

    Please Register or Log in to view images

    ).

    If you encode a string with a password, and then try to decode it with another password, it will give you random characters.

    This is not as hard as implement as it sounds, and it provides a much stronger encoding.

    This was an assignment I just did for second semester of Computer Sciences, but I did it for encoding pictures, not strings.
    I'll share my files if anybody is interested, just note that they are in Java (meh, I don't like Java) and heavily commented, but in spanish

    Please Register or Log in to view images

    .
  9. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Nah, the assingment was just simple character swap. The teacher even provided the array contents to us (the "code"). It was not an exercise in actually encrypting or decrypting anything.

    And no, what you're suggesting doesn't sound the least bit complicated or difficult to implement. It was just beyond the scope of the assignment.

Share This Page