Reputation: 5
I'm trying to reorder a 2D array with for loops. The first generateSong() method creates the array with random doubles and works fine. Then I have the simulateSong() method. Its purpose is to take the rows from the generateSong() array and reprint them as columns, starting with the bottom one.
import java.util.concurrent.ThreadLocalRandom;
public class Guitar {
private int strings;
private int chords;
private double[][] song;
public Guitar(int mstrings, int mchords) {
this.strings = mstrings;
this.chords = mchords;
song = new double[mstrings][mchords];
}
public void generateSong() {
for (int i = 0; i < song.length; i++) {
for (int j = 0; j < song[i].length; j++) {
song[i][j] = ThreadLocalRandom.current().nextDouble(27.5, 4186);
System.out.printf(" %.2f",song[i][j]);
}
System.out.println();
}
}
public void simulateSong() throws InterruptedException {
System.out.println("\nGuitar.simualateSong() ");
for(int i = song.length-1; i >= 0; i--) {
for(int j = song[i].length-1; j >= 0; j--) {
song[i][j] = song[i][0];
System.out.printf(" %.2f",song[i][j]);
}
System.out.println();
}
}
}
The number of rows and columns are set by command line arguments in the main method.
public class Songwriter {
public static void main(String[] args) throws InterruptedException {
System.out.println("Guitar(): Generated new guitar with " + args[0] + " strings. Song length is " + args[1] + " chords.");
String args0 = args[0];
int strings = Integer.parseInt(args0);
String args1 = args[1];
int chords = Integer.parseInt(args1);
Guitar guitarObj1 = new Guitar(strings, chords);
guitarObj1.generateSong();
guitarObj1.simulateSong();
}
}
So ultimately what I'm trying to do is make it so that the rows originally read left to right are now read as columns from top to bottom. Here's the intended output with 3 rows and 4 columns set as command line arguments.
Guitar(): Generated new guitar with 3 strings. Song length is 4 chords.
2538.83 2269.30 1128.09 3419.77
2356.74 2530.88 2466.83 3025.77
3898.32 3804.22 3613.94 337.93
Guitar.simualateSong()
3898.32 2356.74 2538.83
3804.22 2530.88 2269.30
3613.94 2466.83 1128.09
337.93 3025.77 3419.77
And with the code that I currently have this is the output I'm getting.
Guitar.simualateSong()
3898.32 3898.32 3898.32 3898.32
2356.74 2356.74 2356.74 2356.74
2538.83 2538.83 2538.83 2538.83
I know that the only problem(s) lie in the for loops of the simulateSong() method. As you can see my output is close, but no cigar.
Upvotes: 0
Views: 164
Reputation: 2166
You can see in the inner loop of your simulateSong method...
for(int j = song[i].length-1; j >= 0; j--) {
song[i][j] = song[i][0];
System.out.printf(" %.2f",song[i][j]);
}
We're setting the current song value [i][j] to be the value [i][0]. The 0 value is never changing, so all you're doing here is setting each value of the song to the songs first value (with 0 being the first value in song i).
So the logic is clearly wrong. Some pointers for you to work on:
You will need to have a temporary variable to store values for when you are switching your values. E.g. you want to make [i][j] store the value of [j][i], and vice versa, you will need to store one of these values in temp, so you can then replace that value, and then move it to the value you just changed in the temp variable (as it will be overwritten now in the 2nd variable, see pseudocode below)
temp = row[i][j]
row[i][j] = row[j][i]
row[j][i] = temp
Hope this helps.
Upvotes: 0
Reputation: 78
There are two problems in your function. First of all, if I've understood correctly you dont want to change your array in any sort of way, you just want to print it column by column.
Though, in your function there is this line :
song[i][j] = song[i][0];
Which clearly makes you change your array. If you DID want to change your array, then that's not the correct way as you're going to destroy some values you'll need later by inserting the new one. Plus, you indicate "0" as the second dimension index, which mean you will basically copy only one row whatsoever. If you did not want to change your array, then just delete this line, I don't see how it would help for you're tring to do.
The second problem is in this line :
System.out.printf(" %.2f",song[i][j]);
Leaving it as it is right now (and deleting the other line) you would print your array from the end to the begging, but you wouldn't print by column. In ordor to do so you only have to reverse "i" and "j".
Which would lead to this function :
public void simulateSong() {
System.out.println("\nGuitar.simualateSong() ");
for (int i = 0; i < chords; i++) {
for (int j = 0; j < strings; j++) {
System.out.printf(" %.2f", song[j][i]);
}
System.out.println();
}
}
Upvotes: 0
Reputation: 3290
if i understand you right it should be something like this...
public void simulateSong() {
System.out.println("\nGuitar.simualateSong() ");
for (int i = 0; i < chords; i++) {
for (int j = 0; j < strings; j++) {
System.out.printf(" %.2f", song[j][i]);
}
System.out.println();
}
}
generateSong makes something like
A1 A2 A3 A4
B1 B2 B3 B4
C1 C2 C3 C4
simulateSong makes something like
A1 B1 C1
A2 B2 C2
A3 B3 C3
A4 B4 C4
Upvotes: 1