Skip to content

Instantly share code, notes, and snippets.

@PlasmaticJewel
Created October 27, 2023 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save PlasmaticJewel/7b3b4920a3b138dd72d6dfcb1c963f15 to your computer and use it in GitHub Desktop.
Save PlasmaticJewel/7b3b4920a3b138dd72d6dfcb1c963f15 to your computer and use it in GitHub Desktop.
This program uses my class to implement an arraylist matrix to find the determinant of a matrix using my professors main method.
/*
* Name: Tony Silvestri
* Date: 09/28/2022
* Course Number: CSC 220 - D01
* Course Name: Data Structure and Algorithms
* Problem Number: Sparse Array Processing Part 2 of 4
* Email: silvestri@stcc.edu
* Short Description of the Problem:
* Deter.java: Main Program that implements the Gauss-Elimination Method to solve determinants
* Matrix.java: Hides implementation details of a 2D matrix from main program
*/
package v2;
import java.util.Scanner;
public class Deter {
private final static String TITLE = "Determinants Part 2";
private final static String CONTINUE_PROMPT = "Do this again? [y/N] ";
// **********************************************
// Put as many methods you need here
private static void swaprows(Matrix a, int i, int j) {
for (var k = 0; k < a.size(); k++) {
double temp = a.get(i, k);
a.set(i, k, a.get(j, k));
a.set(j, k, temp);
}
}
private static double det(Matrix a) {
double temp = 1.0, coeff;
int i, j, l;
for (i = 0; i < a.size(); i++) {
if (a.get(i, i) == 0.0) {
for (j = i; j < a.size(); j++)
if (a.get(j, i) != 0)
break;
if (j < a.size()) {
swaprows(a, i, j);
temp = -temp;
} else
break;
}
temp *= coeff = a.get(i, i);
for (j = i; j < a.size(); j++)
a.set(i, j, a.get(i, j) / coeff);
for (l = i + 1; l < a.size(); l++) {
coeff = a.get(l, i);
for (j = i; j < a.size(); j++)
a.set(l, j, a.get(l, j) - coeff * a.get(i, j));
}
}
return i < a.size() ? 0.0 : temp;
}
private static void displayMatrix(Matrix a) {
for (var i = 0; i < a.size(); i++) {
for (var j = 0; j < a.size(); j++) {
System.out.printf("%10.2f", a.get(i, j));
}
System.out.println();
}
}
private static void readMatrix(Scanner input, Matrix a) {
for (var i = 0; i < a.size(); i++) {
for (var j = 0; j < a.size(); j++)
a.set(i, j, input.nextDouble());
}
}
private static Matrix getDeterminant(Scanner input) {
var size = input.nextInt();
input.nextLine();
Matrix a = new Matrix(size);
readMatrix(input, a);
return a;
}
// **********************************************
// Start your logic coding in the process method
private static void process(Scanner input, String args[]) throws Exception {
var a = getDeterminant(input);
System.out.printf("Matrix Elements = \n");
displayMatrix(a);
System.out.printf("The determinate = %.3f\n", det(a));
input.nextLine(); // Clear the Keyboard
}
// **********************************************
// Do not change the doThisAgain method
private static boolean doThisAgain(Scanner input, String prompt) {
System.out.print(prompt);
String doOver = input.nextLine();
return doOver.trim().equalsIgnoreCase("Y");
}
// **********************************************
// Do not change the main method
public static void main(String args[]) throws Exception {
System.out.println("Welcome to " + TITLE);
Scanner input = new Scanner(System.in);
do {
process(input, args);
} while (doThisAgain(input, CONTINUE_PROMPT));
input.close();
System.out.println("Thank you for using " + TITLE);
}
}
/*
* Name:Nathan Wheeler
* Date:10/27/23
* Course Number:CsC-220-D01
* Course Name:Data structures/ algrythms
* Problem Number:Chapter 10 4/4 Matrix Determinants
* Email: nrwheeler2201@student.stcc.edu
* This class will take inputs of a matrix of a sparce array.
*/
package v2;
import java.util.ArrayList;
class ValueItem {
int row;
int column;
double value;
}
public class Matrix {
//Replacement for 2d array field
private static final int MAXCOUNT = 100;
private ArrayList<ValueItem> matrixList = new ArrayList<ValueItem>();
private int matrixSize = matrixList.size();
public Matrix(int size) {
matrixSize = size;
}
private ValueItem search(int row, int col) {
for (int i = 0; i < matrixList.size(); i++) {
if (matrixList.get(i).row == row && matrixList.get(i).column == col) {
return matrixList.get(i);
}
}
// if the value is not found
return null;
}
// This function returns the size of the matrix
public int size() {
// take input of matrix size from the console
return matrixSize;
}
// return the values at each row and column
public double get(int row, int col) {
if (row >= matrixSize || col >= matrixSize) {
throw new IndexOutOfBoundsException("Size of input too large.");
}
if (row < 0 || col < 0) {
throw new IndexOutOfBoundsException("Size of input cannot be less than or 0.");
}
ValueItem item = search(row, col);
if(item== null) {
return 0;
}
return item.value;
}
// sets values at matrix[row][column]= value
public void set(int row, int col, double value) {
if (row >= matrixSize || col >= matrixSize) {
throw new IndexOutOfBoundsException("Size of input too large.");
}
if (row < 0 || col < 0) {
throw new IndexOutOfBoundsException("Set value must be greater than 0.");
}
ValueItem foundValue = search(row, col);
if (foundValue == null) {
if (value == 0) {
return;
}
ValueItem populationControl = new ValueItem();
populationControl.value = value;
populationControl.row = row;
populationControl.column = col;
matrixList.add(populationControl);
}
else {
if (value == 0) {
matrixList.remove(foundValue);
} else {
foundValue.value = value;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment