• Post Reply Bookmark Topic Watch Topic
  • New Topic

Double Transposition  RSS feed

 
Steven Ericksen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help programming a two dimensional array transposition in java. I have never taken a java class before, the only experience I have is from online video tutorials or reading text books. The program is for school and is supposed to take ciphertext and decrypt it and render the plaintext. The cipher used is double transposition. So far, I'm trying to get the transposition portion down. Once I figure out how to properly switch entire columns and implement a counter or a way to continue the swap automatically I can move on to incorporating a dictionary scan to search for specific words. That portion will need to take the text from the array after a transposition has occurred and run it through the dictionary scanner. The scanned text will be sent back to the array if the dictionary parameters are not met. I'll be working on that part later as I have not yet learned how to implement a dictionary scan. Currently, the code that I have so far isn't working correctly and does not continue moving columns until all variations are met.






I don't know how to fix it so that it maintains column integrity and continues the transposition process in sequence. Do I need to add an additional for loop for the transposition portion? Any assistance would be greatly appreciated.



 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code that I have so far isn't working correctly and does not continue moving columns until all variations are met.

Could you post a small example of the output that shows the problem?

For example, given a 3x3 array:
123
456
789
Swapping 1st and 2nd columns should give
213
546
879
But the program gives
113
446
779
The first two columns are the same
 
Steven Ericksen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure. Using the same 3x3 array.
123
456
789
The output is
321
645
978
It should be performing as you indicated, in the case below columns 2 and 3 switch.
132
465
798
I have no idea why it switches the way it currently does.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you debugging the code to see where it is going wrong?  I use print statement to print out the values of variables as there values are changed.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think you shou‍ld remove all silicon‑based technology from your vicinity and move one period up the periodic table (to carbon). There is carbon in paper, and in an eraser, and lots of carbon in a pencil. Write down very simply how you would do it without using a computer. If you get it down to words of one syllable, you are doing well. Make sure you have a decent eraser, because you will use it a lot

Create yourself a utility class, like MyUtilities in this post from last week. You won't need the isSorted method, so delete that. What you could probably use in that class is a method to swap two elements in an array. I am not saying any more at the moment.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the problem in the swapMatrix method? 

Can you explain the logic of what that method is doing?
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not convinced you are swapping there. More rotating the entire array. As Norman said, please explain what that method is supposed to do. Only when you know what you want to do can you work out how to do it.
 
Norman Radder
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some suggestions for debugging the code:
change the size to 3 from 10 so there is less print out to go through
add a simple String to load the array with to make it easier to see what happens: "123456789"
Use the Arrays class's deepToString() method to format the matrix for debugging printing
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Ericksen wrote:

This code clobbers the input values as you go through the loop iterations. You're effectively overwriting parts of the matrix that you needed for later.  Notice that you return the same array that you passed in. That's your red flag right there. What you should do is create another array, call it results, for example. This is the array you want to put on the left side of assignment statements and what you want to return on line 44. Since it's only used for input, you shouldn't assign new values to any of the elements of the matrix array. EDIT: I misunderstood what the code was doing. See my other reply later in this thread. Apologies for any confusion created.

Also, you shouldn't hard-code 10 as your upper bound for the loop on lines 37 and 39. This makes your code assume that the matrix array will always represent a 10×10 matrix.  Use matrix.length and matrix[i].length instead for the row and column upper bounds, respectively.
 
Steven Ericksen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method swapMatrix should perform a column swap.
123
456
789
The swap should change the array to
231
564
897

Then later row swaps

564
897
231

Before I get to that point I need to figure out why I'm messing up right now. Then it should be fairly simple to change the operations to change rows instead of columns.

@Junilu Lacar
Am I understanding you correctly? That for the swap, I should create an additional array that I pass the changes to. Then once completed, for further iterations of the swap, write that temp array over the matrix so that the changes are reflected there subsequent changes take place on the changed array. Row 10 is hard coded for the purpose of posting the question. Normally it is not hard coded, I have assigned variables that change the size of the matrix based on user input.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Steven
transposing a matrix in maths means that for eachh 0 <= i < matrix.nrOfRows,  row[i] becomes the column[i] of the transposed matrix.
Here you seem to mean swapping of columns (and later rows). Now that is something different, but the important thing is that we all mean the same thing.

What would be handy to circumvent all these nasty indices is to have a method 'void swap(int[] firstArray, int[] secondArray); that would make arrayFirst equal to arraySeciond and vice versa. (try not to use indices here as well!. Make use of the fact that non primitive variables are references to objects).

If we also have a method 'int[] getColumn(int[][] array, int columnNumber); then it would be an easy thing to come up with a method 'void swapColumns(int[][] array, int columnFirst, int columnSecond);
It will make 'swapRows'(...)' also easy, and last but not least: it wil make your code pretty nicely readable too.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The method swapMatrix should perform a column swap.
123
456
789
The swap should change the array to
231
564
897

The term "column swap" implies to me that 2 columns are being swapped.  With 3 columns one of the columns would be unchanged.  The posted example looks like all three columns have been changed.
That looks to me more like a rotation of columns with wrapping when it moves all the columns.
 
Steven Ericksen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try your suggestions. This project was supposed to be a team project. I didn't know java, but my partner did. He dropped the class three days into the project and left me, with zero programming experience to complete it on my own. Some of the terms that you guys have used are foreign to me. I'm looking them up though and will try to implement them. I'll post what I get later if there are further errors that I can't figure out. Thanks for your help guys!
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Ericksen wrote:. . . a team project. . . . my partner . . . dropped the class three days into the project . . .
And have you spoken to your teachers? You now have a problem which you cannot sort out, and you are liable to get poor marks through no fault of your own. You need to seek advice before things get worse.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steven Ericksen wrote:Before I get to that point I need to figure out why I'm messing up right now. Then it should be fairly simple to change the operations to change rows instead of columns.

@Junilu Lacar
Am I understanding you correctly? That for the swap, I should create an additional array that I pass the changes to.

My apologies. I misunderstood what your code was doing. It looks like you're shifting columns in the matrix one position to the left, moving the first column over to the rightmost position. If that's what you intended to do, that code is actually correct. The only issue is that it is hard-coded to handle a 10×10 matrix.
 
Steven Ericksen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have spoken with the teacher. Their response was to remove the deadline for the project. All other groups have submit their projects. His suggestion was to keep plugging away until I figured it out. As far as the cipher goes itself, I can do it backwards and forwards on paper without issues. I have already deciphered the ciphertext manually. My objective now is to automate the process. To put my thought process in words and not in programming this is what I'm trying to achieve.

The program when run will start requesting three inputs. The first two are for the dimensions of the array or grid that is used for the cipher. The third input is the ciphertext. Currently the array is hardcoded as a 10x10 while I work through getting the appropriate statements in place to do the column swaps. (Not transposition as I've learned now that that refers to switching the values of column[i] with row[i]). Since the project itself is a 10x10 grid, I have to develop a counter so that it goes though every possible combinations of the columns swapping. So if there are only 3 columns. col[0], col[1], col[2], I need it to be able to swap those from (0,1,2) to (0,2,1) and (1,0,2), (1,2,0), (2,0,1) and (2,1,0). After each swap the string that fills the array needs to be taken and passed through a dictionary scan comparing the text to common words. I understand this can be done by referencing a text document with the words listed in it. I haven't gotten that far yet. However, once the highest number of words can be found after switching the columns, the program would then move on to switching to rows until the plaintext message is displayed. Since I'm still learning the proper syntax and how to even program the most basic of programs in Java this whole thing has become a very difficult task. From my perspective, a program that can accomplish this feat would be for a more advanced java programmer. I have spoken with some of my classmates, most of which have never taken java or have taken only intro to or beginning java courses. They all remarked that what they had learned so far doesn't scratch what is required for this project. Most have also only written a program that decrypts the ciphertext using the key. So for a more visual approach to what I'm trying to do:
Enter the number or rows: 2
Enter the number of columns: 5
Enter the ciphertext: rlwdollhoe
(The program does the column swaps checking with the dictionary, then performs the row swaps. For this it may not do the row swaps as both rows are words, so it would then need to print out both variations of row swaps)
The plain text is: helloworld or worldhello
I currently have two Java books in pdf form and I just purchased another in hard book form. The current code that I have does not do a column swap keeping the integrity of the column. For each swap the entire column/row needs to remain intact. It won't give the correct results if the columns and rows do not maintain their individual integrity regardless of their position within the array. I'm going to take what all of you have suggested so far and try to refine the method by which the swaps are handled so that when column 1 switches with column 2, each row doesn't change their values and each column shifts according to the function that moves them. Thanks again guys for your help.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Working on one part at at a time: the column swap
if there are only 3 columns. col[0], col[1], col[2], I need it to be able to swap those from (0,1,2) to (0,2,1) 

In words, the swap was between column 1 and column 2, column 0 was left in place:
123
456
789
swapping columns 1 and 2 gives
132
465
798

If there were a method to do that, the args to the method could be the indexes of two columns to be swapped.
 
Steven Ericksen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, instead of

I would change swapMatrix(char[][] "instead of matrix, two arguments identifying specific columns like first and second column")?
 
Steven Ericksen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
EDIT: (as I can't see a button to edit my response.

That is how the code actually is. The other one is what NetBeans suggested to change the for loop here to and enhanced for loop like the above post. I don't know which one is correct.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know which one is correct.

Which one compiles and returns the correct result when executed.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also the code has no comments describing what it is trying to do.  How can anyone tell if the code is doing what the author wants it to do?
 
Steven Ericksen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quite simply it isn't compiling. I tried to change from the hardcoded 10x10 matrix to allow for user input. When I did that it stopped working. I'm trying to figure out how to fix that. As for what that last bit of code represented, it's in response to the most recent reply where it was suggested to change the arguments in the method. So for clarity on my part, I had included that segment of the code to see if that was where you were referring that I change the arguments to use the indexes. So here is the entire code I'm working with currently. I have included comments for specific parts to identify errors and issues. I'm really sorry if I'm not getting what you guys are saying. This is all new to me and I'm trying to make changes as they are recommended.

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  it isn't compiling.

If you are getting compiler error messages you need help with, copy the full text and paste it here.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
swapMatrix the goal is to swap columns so the 0,1,2 could become 0,2,1

The method then is supposed to swap column 1 with column 2, leaving column 0 alone.  Is that correct?  It will always just swap those two columns no matter how large the array is.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//or 0,1,2 to 1,0,2. 

The swap method will swap columns 0 and 1.
That contradicts what the other description for the method.  How will the method know which swap it is supposed to make?

One solution is to pass the columns to be swapped as arguments to the method so it knows which columns to swap.
 
Steven Ericksen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. So do I need to write a method for each swap?
One for (0,1,2) to (1,0,2)
One for (0,1,2) to (0,2,1)
One for (0,1,2) to (2,1,0)
And additional methods to handle the results from the above methods if the array is larger than three columns? When building the array would I have it saved as a unique variable that would then be used in each method to accomplish the necessary swaps? After each swap that original array would be used for each additional swap. Or am I overcomplicating my approach to this problem?
I would think in the case where I would use the original array and put it through the swap methods, that I would need to wrap things up in if statements that check the size of the array based on which methods are applied. So if there are 3 columns, the if statement would include methods that would accomplish the three swaps listed above. If there are 4 columns, then the if statement would have different methods it would use to operate the necessary swaps to achieve every possible layout of the columns. That seems like it would require tons of code to do that. Still, I may be aiming in the wrong direction causing unnecessary lines of code and headache.
 
Steven Ericksen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run it this is the error message that I get:

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run it

You should fix all the compiler errors before trying to execute(run) the program.

So do I need to write a method for each swap? 

No.  One method that is told what columns to swap in the args it is passed.  It would be called like this:
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



The most straightforward way to do this is to pass the variables into the method as parameters.
 
Steven Ericksen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I've made some progress since posting here last. I've figured out how to pass input variables from the main into the methods. I'm having difficulty still with swapping the columns. Since I need the program to arrange the columns in every permutation possible I've come up with an idea on how to do this.
Present code is as follows:



When I run this code and use the string "helloworld" and set the array to 2 rows 5 columns, this is the swap that occurs:



Good news is it is running and using the variables from the main. Bad news, it wasn't doing a single swap I thought it would do. I got with a Computer Science Tutor at my school and he suggested trying to use something to permutate the columns. We found this:



I bring in this class and implement it as follows:


By using this type of permutation class, and not using the current swapMatrix method, I'm trying to figure out two things.
First, for the permutation class to work, I have to give it an array based on the input of the user. So, for the column
size which is input as 'c', I need to find how to convert that from an integer to a range. Something like [0,(c-1)), then
take those values and fill the array a[] that is used in the class. So far I haven't found a way to do that.
Now, the permutation class will give every possible permutation of those values (ie. if the user inputs 3 columns, that means
the indexes for the columns are (0,1,2)) so every combination would be computed. I want to take each iteration of those
permutations and use them for the column swap.
It it's possible, I want to take the array that is built with the fillMatrix method and build a new array with the column order
based on the permutations from the permutation class. I spent about 3 hours yesterday with a tutor trying to figure out if that
were possible, and we determined it was, but we couldn't figure it out. Any help or direction would be greatly appreciated.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show a simple example of what you mean by permutations?
Given three columns: 0,1,2
would these be the permutations:
012
021
102
120
201
210

If these were the 6 values, then a 2 dim array with 6 rows with 3 columns in each row would be a way to save the indexes for swapping columns to get 6 new arrays.

this method is not performing a column swap

The method needs to know what columns to swap.  They can be passed to the method as arguments.
The normal way to swap two variables is to use a temp:
temp = var1  // save var1
var1 = var2  // move var2 to var1
var2 = temp // put old var1 in var2
 
Steven Ericksen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is exactly what I need. Instead of the print function in the permutation class, I need to save it in an array, the call in sequence each row to implement the column swap. I just haven't the foggiest on how to even start that.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There must be descriptions on algorithms for getting the permutations of a group of numbers online somewhere.  They doing an internet search to find one.
 
Steven Ericksen
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to build the array for a[] for the permutation class. I got this from the tutor and it works.


Instead of using the print function in permutation class, the two dimensional array is stored and pulled into the swapMatrix to perform the permutations of the array.

   0  1  2  3
0 a  b  c  d
1 e  f   g h
With the column indexes above, that would be an original array.
Taking the values of a[0] which could be (2,1,3,0)
How would I build a new array so that it looks like below:
   2  1  3  0
0 c  b  d  a
1 g  f  h  e

I'm not sure how to get that to work.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have an algorithm that will fill an array with all the permutations of a set of numbers?
Like the list of numbers I posted earlier for  3 numbers: 0,1,2

Given that array of numbers, one row of the array would give you the order for the columns:
For example if a row had 2,1,0
Then the output array would be build with column 2 first, followed by column 1 followed by column 0

That would require a method that takes a source array and the array for the output columns selection and returns an array with the newly positioned columns.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!