Skip to content

Instantly share code, notes, and snippets.

@adrientetar
Created November 23, 2012 16:15
Show Gist options
  • Save adrientetar/4136301 to your computer and use it in GitHub Desktop.
Save adrientetar/4136301 to your computer and use it in GitHub Desktop.
Matrix
/**
* Ce programme calcule la matrice inverse à l'aide du
* pivot de Gauss.
* @author Adrien Tétar
* @version 1.0-WIP
*
* Copyright © 2012, Adrien Tétar. All rights reserved.
*/
import java.awt.GridLayout;
import javax.swing.*;
class Main {
/**
* @mat: matrix declared in main (e.g: mat[][] = new int[3][3];)
* @rows: number of matrix rows (e.g: int rows = 3;)
* @columns: number of matrix columns (e.g: int columns = 3;)
*/
public static int[][] inputMatrix(int[][] mat, int rows, int columns)
{
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(rows,columns));
for (int a=0; a<(rows*columns); a++)
{
panel.add(new JTextField(a));
}
if (JOptionPane.showConfirmDialog(null, panel, "Enter the matrix", JOptionPane.OK_CANCEL_OPTION)
== JOptionPane.OK_OPTION)
{
for(int a=0; a<(rows*columns); a++){
for(int b=0; b<rows; b++){
for(int c=0; c<columns; c++){
/* FIXME: the crap below catches wrong values... */
mat[b][c] = Integer.parseInt(((JTextField)panel.getComponent(a)).getText());
}
}
}
}
/* If the user clicks on Cancel, quit the program immediately. */
else
{
System.exit(0);
}
return mat;
}
/* Display an array of arrays. */
public static void dispMatrix(int[][] mat, int rows, int columns)
{
for(int a=0; a<rows; a++){
for(int b=0; b<columns; b++){
System.out.print(mat[a][b] + " ");
}
System.out.print("\n");
}
}
public static void main(String[] args)
{
/* Use 3/3 matrix. */
final int rows = 3;
final int columns = 3;
int[][] mat = new int[rows][columns];
inputMatrix(mat, rows, columns);
// int[][] mat = {{0, 1, 0}, {1, 0, 1}, {0, 1, 0}};
// System.out.println(Arrays.toString(mat));
dispMatrix(mat, rows, columns);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment