Rotate Arrays in Java: Troubleshooting 90 Degree Left Rotation Code

  • Context: Comp Sci 
  • Thread starter Thread starter physicsfun
  • Start date Start date
  • Tags Tags
    Arrays Java Rotating
Click For Summary
SUMMARY

The forum discussion addresses a Java code snippet intended to rotate a 2D array 90 degrees to the left. The provided code incorrectly assigns values, leading to an improper rotation. The line result[i][k] = source[source.length-1-i][i]; fails to achieve the desired transformation, as it does not account for the correct indexing needed for a left rotation. A proper implementation should adjust the indices to reflect the leftward shift accurately.

PREREQUISITES
  • Understanding of Java programming language
  • Familiarity with 2D arrays in Java
  • Knowledge of array indexing and manipulation
  • Basic algorithm design principles
NEXT STEPS
  • Review Java array manipulation techniques
  • Learn about matrix rotation algorithms
  • Explore debugging techniques for Java code
  • Study the differences between clockwise and counterclockwise rotations
USEFUL FOR

Java developers, computer science students, and anyone interested in algorithm design and array manipulation techniques.

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 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K