109.1.. Učitiat i ispisati dvodimenzionalni niz - matricu 2x3.
Opis programa:
Listing programa:
package Matrica;
import java.util.Scanner;
public class P10918001 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Ucitaj broj clanova matrice (red kolona) ");
int red = input.nextInt();
int kolona = input.nextInt();
int niz[][] = new int[red][kolona];
System.out.println("Ucitaj clanove matrice:");
for (int i = 0; i < red; i++) {
for (int j = 0; j < kolona; j++) {
System.out.print("X[" +i+ "," +j +"]"+ "-->");
niz[i][j] = input.nextInt();
}
}
// ulaz
System.out.println("\nMatrica:");
for (int i = 0; i < red; i++) {
for (int j = 0; j < kolona; System.out.print(niz[i][j++] + " "));
System.out.println();
}
// Obrazovanje transponovane matrice:
int[][] b = new int[kolona][red];
for (int i = 0; i < kolona; i++)
for (int j = 0; j < red; j++)
b[i][j] = niz[j][i];
// Zamjena stare matrice novom matricom:
niz = b;
b = null;
int p = red;
red = kolona;
kolona = p;
// Ispisivanje rezultata :
System.out.println("\nTransponovana matrica:");
for (int i = 0; i < red; i++) {
for (int j = 0; j < kolona; System.out.print(niz[i][j++] + " "));
System.out.println();
}
}
}
Ispis na ekranu:
Ucitaj broj clanova matrice (red kolona) 2 3
Ucitaj clanove matrice:
X[0,0]-->1
X[0,1]-->2
X[0,2]-->3
X[1,0]-->4
X[1,1]-->5
X[1,2]-->6
Matrica:
1 2 3
4 5 6
Transponovana matrica:
1 4
2 5
3 6
Ispis na ekranu:
Index
|
|