Comp Sci Rotate Arrays in Java: Troubleshooting 90 Degree Left Rotation Code

AI Thread Summary
The code provided attempts to rotate a 2D array 90 degrees to the left but contains an error in the indexing logic. The current implementation incorrectly maps the elements, leading to an incorrect output. To achieve a proper left rotation, the correct indexing should be adjusted to reflect the new positions of the elements in the rotated array. Users are encouraged to revise the indexing in the nested loops to ensure the elements are placed correctly. Debugging the code will help achieve the desired 90-degree left rotation of the array.
physicsfun
Messages
10
Reaction score
0

Homework Statement



Write code to rotate an array...

Homework Equations



why is this code not rotating my array 90 degrees to the left? thanks SO much in advance!

The Attempt at a Solution



public static int[][] rotateLeft(int[][] source) {
int[][] result = new int[source.length][source[0].length];
for (int i = 0; i < result.length; i++) {
for (int k = 0; k < result[0].length; k++) {
result[k] = source[source.length-1-i];}}
return result;}
 
Physics news on Phys.org
Do you see anything here:

Code:
result[i][k] = source[source.length-1-i][i];}}
 

Similar threads

Replies
2
Views
1K
Replies
4
Views
1K
Replies
1
Views
2K
Replies
5
Views
2K
Replies
3
Views
1K
Replies
7
Views
2K
Replies
5
Views
3K
Back
Top