Java :: Aufgabe #55 :: Lösung #6

5 Lösungen Lösungen öffentlich
#55

Matrizen um 90 Grad drehen

Anfänger - Java von pocki - 11.11.2013 um 16:38 Uhr
Erstelle ein Programm, welches als Eingabe eine Matrix beliebiger Größe entgegennimmt und diese dann um 90 Grad dreht.

Beispiel an einer 4x4 Matrix:
Eingabematrix mit den Elementen:
a11 a12 a13 a14
a21 a22 a23 a24
a31 a32 a33 a34
a41 a42 a43 a44

sollte diese Ausgabematrix erzeugen:
a41 a31 a21 a11
a42 a32 a22 a12
a43 a33 a23 a13
a44 a34 a24 a14

Das Programm sollte für unterschiedlich großen und auch mit nicht quadratischen Matrizen funktionieren.
#6
vote_ok
von programmer (210 Punkte) - 11.08.2015 um 14:19 Uhr
Quellcode ausblenden Java-Code
import java.util.Scanner;


public class matrix {
	
	
	
	

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		 Scanner scanner = new Scanner(System.in);
		
		int numRow, numColumn;
		
		System.out.println("Bitte geben sie die Reihenanzahl an : ");
		numRow = scanner.nextInt();
		System.out.println("Bitte geben sie die Spaltenanzahl an : ");
		numColumn = scanner.nextInt();
		
		Matrix2D myMatrix = new Matrix2D(numRow, numColumn);
		myMatrix.fillMatrix();
		myMatrix.ouputMatrix();
		myMatrix.rotateMatrix();
		
	}

}
Quellcode ausblenden Java-Code
import java.util.Scanner;


public class Matrix2D {

	int numRow;					// Anzahl Zeilen
	int numColumn;				// Anzahl Spalten
	int [][] mat2D;				// unsere Matrix
	
	
	
	
	public Matrix2D(int numRow, int numColumn) {
	
		this.numRow = numRow;
		this.numColumn = numColumn;
		mat2D = new int[numRow][numColumn];
		
	}
	
	
	int [][] fillMatrix()
	{
		Scanner scanner = new Scanner(System.in);
		
		// loop over 2dimensional matrix and fill it 
		
		System.out.println("Füllen der matrix : \n");
		
		for(int i = 0; i < numRow; i++)
		{
			for(int k = 0; k < numColumn; k++)
			{
				System.out.println("Reihe : " + i + "Spalte : " + k);
				mat2D[i][k] = scanner.nextInt();
			}
		}
	
	
		return mat2D;
	
	}
	
	
	void ouputMatrix ()
	{
		System.out.println("Aktuelle Matrix : \n");
		
		// output every number of matrix 
		
		for(int i = 0; i < numRow; i++)
		{
			for(int m = 0; m < numColumn; m++)
			{
				System.out.print(mat2D[i][m] + " ");
			}
			
			System.out.println();
		}
	}
	
	void rotateMatrix()
	{
		// rotate Matrix 90 degrees to south ****
		
		int [][] matrixTmp = new int[numColumn][numRow];
		
		for(int j = 0; j < numRow; j++)
		{
			for(int h = 0; h < numColumn; h++)
			{
				matrixTmp[h][j] = mat2D[j][h];
			}
		}
		
		// ***************************************
		
		// now output the rotated Matrix 
		
		System.out.println("\nRotierte Matrix : \n");
		
		for(int j = 0; j < numColumn; j++)
		{
			for(int h = 0; h < numRow; h++)
			{
				System.out.print(matrixTmp[j][h] + " ") ;
			}
			
			System.out.println();
		
		}
		
	}
	
	
}


// Ausgabe : *******************************//

Bitte geben sie die Reihenanzahl an :
3
Bitte geben sie die Spaltenanzahl an :
4
Füllen der matrix :

Reihe : 0Spalte : 0
1
Reihe : 0Spalte : 1
2
Reihe : 0Spalte : 2
3
Reihe : 0Spalte : 3
4
Reihe : 1Spalte : 0
1
Reihe : 1Spalte : 1
2
Reihe : 1Spalte : 2
3
Reihe : 1Spalte : 3
4
Reihe : 2Spalte : 0
1
Reihe : 2Spalte : 1
2
Reihe : 2Spalte : 2
3
Reihe : 2Spalte : 3
4
Aktuelle Matrix :

1 2 3 4
1 2 3 4
1 2 3 4

Rotierte Matrix :

1 1 1
2 2 2
3 3 3
4 4 4

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben
2107638

Du scheinst einen AdBlocker zu nutzen. Ich würde mich freuen, wenn du ihn auf dieser Seite deaktivierst und dich davon überzeugst, dass die Werbung hier nicht störend ist.