Click here to Skip to main content
16,005,080 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey guys,
i have a problem in columnar transposition cipher
I've made half of the code but i really have a serious problem in for loop
and here is the code

C#
string text = textBox1.Text;
double cols = double.Parse(textBox2.Text);
double len = text.Length;

string d;
if (cols > len)
{
    label3.Text = "cols must be less than text";

}
else
{
    double test = len / cols;
    if (test % 1 == 0)
    {
        double row = test;
        char[,] enc = new char[(int)cols, (int)row];
        char[,] matrix = new char[(int)row, (int)cols];
        char[] characters = text.ToCharArray();
        for (double i = 0; i < row; i++)
        {
            for (double j = 0; j < cols; j++)
            {
                matrix[(int)i, (int)j] = characters[(int)(i + j + i * 2)];
                MessageBox.Show(Convert.ToString(matrix[(int)i, (int)j]));
            }

        }
        for (double c = 0; c < cols; c++)
        {
            for (double r = 0; r < row; r++)
            {
                
                MessageBox.Show(Convert.ToString(matrix[(int)c, (int)r]));
                
            }
        }




can any one help me :(
Posted
Updated 28-Apr-12 12:36pm
v2
Comments
VJ Reddy 28-Apr-12 18:36pm    
Edit: pre tag for C# code added.
[no name] 28-Apr-12 20:17pm    
"serious problem" does not tell us anything. We can't read your mind.

1 solution

As seen from the code given in the question
C#
double cols = double.Parse(textBox2.Text);
double len = text.Length;

cols is used for the matrix size, so it is preferable to have it as int.
The property text.Length is of type int and it can be used as it is and there is no need to assign to a local variable.
C#
double test = len / cols;
if (test % 1 == 0)

can be given as an else if like
C#
else if (len % cols != 0)
  label3.Text = "The text length shall be equal to rows * cols ";

The transposition can be done in the else block
By taking row, cols as int the cast (int) can be avoided.

Further for transpose
1. The matrix can be declared with text.Length /cols as rows and cols as columns.
2. Each element in the matrix can be assigned by using for loop on text like
C#
matrix[i/cols,i%cols]=text[i];
 
Share this answer
 
v2
Comments
Shahin Khorshidnia 29-Apr-12 2:20am    
nice
VJ Reddy 29-Apr-12 11:10am    
Thank you, Shahin.
mooraamoora 29-Apr-12 13:10pm    
thanks very much for helping :)...finally done,
columnar transposition is a way you assign the value of textbox to 2-d array so that you can read the array column by column not as usual row by row, the problem I had was when I first assigned the textbox value to 1-D array, then assigning this array to 2-D array..every time it says array is outside bound, but finally I got it.
this is the new code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace w
{

public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}

public void button1_Click(object sender, EventArgs e)
{

//lets goooooooooooooooooooooooooooo
string text = textBox1.Text;
double cols = double.Parse(textBox2.Text);
double len = text.Length;
double ind = 0;

if (cols > len)
{
label3.Text = "cols must be less than text";

}
else
{
double test = len / cols;


double row = Math.Ceiling(test);
label3.Text = ("number of rows = " + Convert.ToString(row));
char[,] matrix = new char[(int)row, (int)cols];
char[] characters = text.ToCharArray();
for (double i = 0; i < matrix.GetLength(0); i++)
{
for (double j = 0; j < matrix.GetLength(1); j++)
{
matrix[(int)i, (int)j] = characters[(int)(ind)];
ind++;

if (ind > len - 1)
{
break;
}
}

}
double ri = 0, cj = 0;
for (double i = 0; i <= matrix.GetLength(1); i++)
{
for (double j = 0; j <= matrix.GetLength(0); j++)
{
label7.Text += matrix[(int)ri, (int)cj];

ri++;
if (ri >= matrix.GetLength(0))
{
break;
}
}
ri = 0; cj++;
if (cj >= matrix.GetLength(1))
{
break;
}
}




}
}

private void button2_Click(object sender, EventArgs e)
{
string text = label7.Text;
double cols = double.Parse(textBox2.Text);
double len = text.Length;
double ind = 0;

if (cols > len)
{
label3.Text = "cols must be less than text";

}
else
{
double test = len / cols;


double row = Math.Ceiling(test);

char[,] matrix = new char[(int)row, (int)cols];
char[] characters = textBox1.Text.ToCharArray();
for (double i = 0; i < matrix.GetLength(0); i++)
{
for (double j = 0; j < matrix.GetLength(1); j++)
{
matrix[(int)i, (int)j] = characters[(int)(ind)];
ind++;
label8.Text += matrix[(int)i, (int)j];

if (ind > len - 1)
{
break;
}
}

}

}


}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}
Day Star 29-Apr-12 15:00pm    
how clear label
there are IndexOutOfRangeException in
matrix[(int)i, (int)j] = characters[(int)(ind)];
ind++;
label8.Text += matrix[(int)i, (int)j];

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900