I have to put the following block of code in a loop. It is to display a 10x10 array that will hold char values for a message encrypted in a transposition cipher.

I have not been able to figure out a loop or a system of loops that can do this. I need to do the loop for 10x10, 5x20, and 4x25, and 2x50. The snippet below is for 10x10, but if I can figure out how to do it for 10x10, I imagine it should be easy to do the other array dimensions too.

cout << e[0][0]
         << e[0][1] << e[1][0]
         << e[0][2] << e[1][1] << e[2][0]
         << e[0][3] << e[1][2] << e[2][1] << e[3][0]
         << e[0][4] << e[1][3] << e[2][2] << e[3][1] << e[4][0]
         << e[0][5] << e[1][4] << e[2][3] << e[3][2] << e[4][1] << e[5][0]
         << e[0][6] << e[1][5] << e[2][4] << e[3][3] << e[4][2] << e[5][1] << e[6][0]
         << e[0][7] << e[1][6] << e[2][5] << e[3][4] << e[4][3] << e[5][2] << e[6][1] << e[7][0]
         << e[0][8] << e[1][7] << e[2][6] << e[3][5] << e[4][4] << e[5][3] << e[6][2] << e[7][1] << e[8][0]
         << e[0][9] << e[1][8] << e[2][7] << e[3][6] << e[4][5] << e[5][4] << e[6][3] << e[7][2] << e[8][1] << e[9][0]
                    << e[1][9] << e[2][8] << e[3][7] << e[4][6] << e[5][5] << e[6][4] << e[7][3] << e[8][2] << e[9][1]
                               << e[2][9] << e[3][8] << e[4][7] << e[5][6] << e[6][5] << e[7][4] << e[8][3] << e[9][2]
                                          << e[3][9] << e[4][8] << e[5][7] << e[6][6] << e[7][5] << e[8][4] << e[9][3]
                                                     << e[4][9] << e[5][8] << e[6][7] << e[7][6] << e[8][5] << e[9][4]
                                                                << e[5][9] << e[6][8] << e[7][7] << e[8][6] << e[9][5]
                                                                           << e[6][9] << e[7][8] << e[8][7] << e[9][6]
                                                                                      << e[7][9] << e[8][8] << e[9][7]
                                                                                                 << e[8][9] << e[9][8]
                                                                                                            << e[9][9];

Btw, this is in Dev-C++. Any help greatly appreciated :cheesy:

i work in tubo c compiler.
if u want 2 enode using that, i can help.

I have to put the following block of code in a loop. It is to display a 10x10 array that will hold char values for a message encrypted in a transposition cipher.

I have not been able to figure out a loop or a system of loops that can do this. I need to do the loop for 10x10, 5x20, and 4x25, and 2x50. The snippet below is for 10x10, but if I can figure out how to do it for 10x10, I imagine it should be easy to do the other array dimensions too.

Have a close look at the repeated patterns in that code you've just pasted.

Remember that a 'for' loop is for repetition. Any code inside that loop is typically repeated a set number of times.
Try creating a for-loop that counts from 0 to 9 ..... (It will repeat 10 times)
- How would you repeat that for-loop, so that the loop itself gets executed 10 times..?

i work in tubo c compiler.
if u want 2 enode using that, i can help.

He's asking about a loop which is not a compiler dependent command. Even Digital Mars could handle this...

apurv, if you want to help, feel free. I have experience with Turbo C, Dev-C, and Borland. But I don't think what I need is compiler-specific.

Bench, I've tried multiple times to do huge sets of multi-for-loops, but I cannot seem to figure it out. Can you describe what you mean a bit more clearly, I just can't seem to figure this out.

Thanx for the replies btw.

Finally, some success. I don't know why I didn't realize this before, but this was very simple, all it needed was some clear uninterrupted thought. I managed to create 2 loops to do my bidding

for (z=0;z<10;z++)
    {
        x=0;
        y=z;
        do
        {
            cout << "[" << x << y << "]";
            x++;
            y--;
        }
        while (x<=z);
        cout << endl;
    }
    cout << endl;
    for (z=1;z<10;z++)
    {
        x=z;
        y=9;
        do
        {
            cout << "[" << x << y << "]";
            x++;
            y--;
        }
        while (x<=9);
        cout << endl;
    }

This can be implemented to do the task I need, now I just need to figure out how to combine to two loops to save lines, and then do it for other array dimensions. Any help on doing the combining is appreciated in advance.

edit - deleted, got it wrong.

While coding the decrypter, I came across some issues as well. So here's what I need help with so far:

I need to get the following numbers into a loop to read off of an array that has the encrypted 100 char message.

1      2     4     7    11    16    22    29    37    46
3      5     8    12    17    23    30    38    47    56
6      9    13    18    24    31    39    48    57    65
10    14    19    25    32    40    49    58    66    73
15    20    26    33    41    50    59    67    74    80
21    27    34    42    51    60    68    75    81    86
28    35    43    52    61    69    76    82    87    91
36    44    53    62    70    77    83    88    92    95
45    54    63    71    78    84    89    93    96    98
55    64    72    79    85    90    94    97    99   100

As well as getting the bottom two loops into 1 loop.

for (z=0;z<10;z++)
    {
        x=0;
        y=z;
        do
        {
            cout << e[x][y];
            x++;
            y--;
        }
        while (x<=z);
    }
    for (z=1;z<10;z++)
    {
        x=z;
        y=9;
        do
        {
            cout << e[x][y];
            x++;
            y--;
        }
        while (x<=9);
    }

Any hints or help appreciated greatly :mrgreen:

While coding the decrypter, I came across some issues as well. So here's what I need help with so far:

I need to get the following numbers into a loop to read off of an array that has the encrypted 100 char message.

1      2     4     7    11    16    22    29    37    46
3      5     8    12    17    23    30    38    47    56
6      9    13    18    24    31    39    48    57    65
10    14    19    25    32    40    49    58    66    73
15    20    26    33    41    50    59    67    74    80
21    27    34    42    51    60    68    75    81    86
28    35    43    52    61    69    76    82    87    91
36    44    53    62    70    77    83    88    92    95
45    54    63    71    78    84    89    93    96    98
55    64    72    79    85    90    94    97    99   100

As suggested previously, look at the numbers and look for a pattern.

As well as getting the bottom two loops into 1 loop.

for (z=0;z<10;z++)
    {
        x=0;
        y=z;
        do
        {
            cout << e[x][y];
            x++;
            y--;
        }
        while (x<=z);
    }
    for (z=1;z<10;z++)
    {
        x=z;
        y=9;
        do
        {
            cout << e[x][y];
            x++;
            y--;
        }
        while (x<=9);
    }

Any hints or help appreciated greatly :mrgreen:

An algorithm does not have to be a single loop. IMO you have the answer here. There is no need to complicate the thing by trying to cram both loops together into one.

I just can't seem to get the right loops and variables to do the loop for decrypting 10x10 arrays.

1      2     4     7    11    16    22    29    37    46
3      5     8    12    17    23    30    38    47    56
6      9    13    18    24    31    39    48    57    65
10    14    19    25    32    40    49    58    66    73
15    20    26    33    41    50    59    67    74    80
21    27    34    42    51    60    68    75    81    86
28    35    43    52    61    69    76    82    87    91
36    44    53    62    70    77    83    88    92    95
45    54    63    71    78    84    89    93    96    98
55    64    72    79    85    90    94    97    99   100

I have come up with this, which I know doesn't include the right offsetting (since the loop doesnt always add incrementing numbers, but decrementing as well). Can anyone try to correct this code more, or atleast post some more pointers. I've look at the numbers, and I can see the definitive patterns, but I cannot translate that into loops.

for (z=0;z<10;z++)
    {
        c=1;
        c+=z;
        b=0;
        for (a=1;a<11;a++)
        {
            c*=a;
            b+=c;
            cout << b << " ";
        }
        cout << endl;
    }

Since you've shown that you are trying....

row = 0;                  // initialize the starting row value
for (r=1; r<=10; r++)     // loop for 10 rows
{
    row += r;             // add current row to starting row value
    col = row;            // setup the column value
    for (c=0; c<10; c++)  // loop for ten columns
    {
        col += c;         // add current column to the column value 
        printf("%2d ", col);
    }
    printf("\n");
}

Thanx WaltP, I modified your code and came up with this slightly larger code.

x=0;
    for (z=1;z<=10;z++)
    {
        x+=z;
        a=z;
        t=0;
        cout << x << " ";
        if (z==10)
        {
            a--;
            t=1;
        }
        y=x;
        for (c=0;c<9;c++)
        {
            if (a<9)
            {
                y+=a;
                a++;
                cout << y << " ";
            }
            else
            {
                if (t<2)
                {
                    y+=a;
                    t++;
                    cout << y << " ";
                }
                else
                {
                    a=8;
                    while (c<9)
                    {
                        y+=a;
                        a--;
                        c++;
                        cout << y << " ";
                    }
                    c=9;
                }
            }
        }
        cout << endl;
    }

This gives me the incrementing pattern below, the one that makes the 1-100 sequence work.

123456789
234567899
345678998
456789987
567899876
678998765
789987654
899876543
998765432
987654321

If you notice anything redundant or unnecesary with the above c code, please let me know. I'm very close to finishing the transposition cipher program, I just need to change the loops for other array dimensions.

Thanx WaltP, I modified your code and came up with this slightly larger code.

Slightly larger? It's 4 times larger! You have a great future in management. Or politics.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.