DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: 2D Array

  1. #1
    Adam Guest

    2D Array


    Hi

    I was hoping someone could help me with this. I have tried to make a little
    encryption program but I am having some trouble with the 2d array that I
    have set up.

    Can someone please look at this code and give me a hint.

    ifstream
    Read;
    ofstream
    Write;
    fstream ReadWrite;
    const int Rows = 2;
    const int Columns = 27;
    char Encode[Rows][Columns]= {{'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', ' '}, {'R', 'Y', 'G', 'C', 'P', 'M', 'J', 'S', 'K', 'A',
    'V', 'I', 'Z', 'W', 'E', 'B', 'O', 'L', 'Q', 'D', 'F', 'T', 'H', 'X', 'U',
    'N', '!'}};

    const int MAX = 20;
    char String[MAX] = "HELLO THERE";
    int RowIndex = 0;
    int ColumnIndex = 0;
    int i = 0;
    char Coded[MAX];
    void main ()
    {
    for (int ColumnIndex = 0; ColumnIndex < Columns; ++ ColumnIndex)
    for (int i = 0; i < MAX; ++i)
    if (String[i] = (Encode[1][ColumnIndex]))
    Coded[i] = (Encode[2][ColumnIndex]);
    else
    Coded[i] = String[i];
    cout << Coded;
    }

    Thanks very much.

    Regards
    Adam

  2. #2
    Mark C Guest

    Re: 2D Array


    One problem you have is this...

    if (String[i] = (Encode[1][ColumnIndex]))

    you should have

    if (String[i] == (Encode[1][ColumnIndex]))

    Otherwise, I think you want to do this...

    void main ()
    {
    BOOL bMatchFound;

    // Loop through each character in the string to be
    // encoded.
    for( int i = 0; i < MAX; ++i )
    {
    bMatchFound = FALSE;

    // Loop through the array of characters to find
    // a match for the character we want to encode
    // until we find a match or run out of characters.
    for( int ColumnIndex = 0;
    ( !bMatchFound ) && ( ColumnIndex < Columns );
    ++ ColumnIndex )
    {
    // If we've found a match
    if( String[i] == (Encode[1][ColumnIndex]) )
    {
    // Encode the character.
    Coded[i] = (Encode[2][ColumnIndex]);

    // Set a flag to get out of the loop.
    bMatchFound = TRUE;
    }
    }

    // If we didn't find a match, just copy the
    // character into the result string.
    if( !bMatchFound )
    {
    Coded[i] = String[i];
    }
    }

    cout << Coded;
    }

    "Adam" <astein@optusnet.com.au> wrote:
    >
    >Hi
    >
    >I was hoping someone could help me with this. I have tried to make a little
    >encryption program but I am having some trouble with the 2d array that I
    >have set up.
    >
    >Can someone please look at this code and give me a hint.
    >
    >ifstream
    > Read;
    >ofstream
    > Write;
    >fstream ReadWrite;
    >const int Rows = 2;
    >const int Columns = 27;
    >char Encode[Rows][Columns]= {{'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', ' '}, {'R', 'Y', 'G', 'C', 'P', 'M', 'J', 'S', 'K', 'A',
    >'V', 'I', 'Z', 'W', 'E', 'B', 'O', 'L', 'Q', 'D', 'F', 'T', 'H', 'X', 'U',
    >'N', '!'}};
    >
    >const int MAX = 20;
    >char String[MAX] = "HELLO THERE";
    >int RowIndex = 0;
    >int ColumnIndex = 0;
    >int i = 0;
    >char Coded[MAX];
    >void main ()
    >{
    > for (int ColumnIndex = 0; ColumnIndex < Columns; ++ ColumnIndex)
    > for (int i = 0; i < MAX; ++i)
    > if (String[i] = (Encode[1][ColumnIndex]))
    > Coded[i] = (Encode[2][ColumnIndex]);
    > else
    > Coded[i] = String[i];
    >cout << Coded;
    >}
    >
    >Thanks very much.
    >
    >Regards
    >Adam



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center