finding cofactor and determinant of a 3x3 matrix (java) - java

I computed the product, sum, and transpose(of product), but I have no clue where to start on the cofactor and determinent of the product matrix.
Ive seen a couple examples but none that resemble my particular code so if anyone could show me how i could incorporate these two methods into my program I'd high appreciate it.
please dont ask me what i've tried because as I stated I am clueless at the moment
//here is the code I wrote for the product:
package programEx;
import java.io.*;
import java.util.*;
import java.io.File.*;
import java.util.Scanner;
public class progEx {
public static void main(String[] args) {
//Establishing Input Stream
Scanner s = new Scanner(System.in);
System.out.print("Enter number of rows in A: ");
int rowsInA = s.nextInt();
System.out.print("Enter number of columns in A / rows in B: ");
int columnsInA = s.nextInt();
System.out.print("Enter number of columns in B: ");
int columnsInB = s.nextInt();
int[][] a = new int[rowsInA][columnsInA];
int[][] b = new int[columnsInA][columnsInB];
//Taking user input for 1st matrix
System.out.println("Enter matrix A");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
a[i][j] = s.nextInt();
}
}
//Taking user input for 2nd matrix
System.out.println("Enter matrix B");
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[0].length; j++) {
b[i][j] = s.nextInt();
}
}
//calling the multiplication method and passing it to both matrices
int[][] c = multiply(a, b);
System.out.println("Product of A and B is");
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[0].length; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
}
//Separate method for the multiplication of 1st and 2nd matrices
public static int[][] multiply(int[][] a, int[][] b) {
int rowsInA = a.length;
int columnsInA = a[0].length;
int columnsInB = b[0].length;
int[][] c = new int[rowsInA][columnsInB];
for (int i = 0; i < rowsInA; i++) {
for (int j = 0; j < columnsInB; j++) {
for (int k = 0; k < columnsInA; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
return c;
}
}

Well,a 3*3 matrix has 3 columns and 3 rows to begin with.
Each starts with an index 0.
Determinant can be calculated as:
int d=determinant();
int determinant()
{
int x=a[0][0]*((a[1][1]*a[2][2])-(a[2][1]*a[1][2]));
int y=-a[0][1]*((a[0][1]*a[2][2])-(a[2][0]*a[1][2]));
int z=a[0][2]*((a[1][0]*a[2][1])-(a[1][1]*a[2][0]));
int r=x+y+z;
return r;
}
This is only for finding the determinant of a 3*3 matrix.

Related

I want to find the Variance and Standard-Deviation using a 2D array. I did the program but the output coming is incorrect

I want to find the Variance and Standard-Deviation using a 2D array I did the program but the output comming is incorrect. Please help me what should I do / modify it to get the required output.
Input
3 4
1 0 1 1 1 1 1 1 1 1 1 1
Solution output
0.07513148009038192
0.27410122234383033
Expected output
0.0764
0.2764
my program:
import java.util.*;
import java.text.DecimalFormat;
public class Source {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int m = s.nextInt();
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
arr[i][j] = s.nextInt();
varianceAndStandardDeviation(arr, n, m);
}
// Method for calculating variance
static void varianceAndStandardDeviation(int arr[][], int n, int m) {
// Write your code here
double[][] diff = new double[n][m];
double avg = 0f;
double var = 0f;
double std_dev = 0f;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
avg = (avg + arr[i][j]);
avg = avg/(n*m);
diff[i][j] = (avg - arr[i][j]);
var = ((diff[i][j] * diff[i][j])/((n*m)-1));
std_dev = Math.pow(var, 0.5);
}
System.out.println(var);
System.out.println(std_dev);
}
}
https://www.geeksforgeeks.org/variance-standard-deviation-matrix/ there you have full tutorial.
Make 2 passes: one to compute the average, another for variance:
import java.util.Scanner;
public class Source {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int m = s.nextInt();
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
arr[i][j] = s.nextInt();
varianceAndStandardDeviation(arr, n, m);
}
// Method for calculating variance
static void varianceAndStandardDeviation(int arr[][], int n, int m) {
double avg = 0;
double variance = 0;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
avg += arr[i][j];
avg /= n * m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
variance += Math.pow((arr[i][j] - avg), 2);
variance /= n * m;
System.out.printf("%.4f%n", variance);
System.out.printf("%.4f%n", Math.sqrt(variance));
}
}

Multi-Dimesional Array with User Input

Trying to change this code so I can get user input instead of a predefined array. Can anyone help? This code needs to have the method sum with the double[][] parameter. The method should return the sum of the elements of the array passed to it and should be rectangular.
import java.util.Scanner;
class Array2_1
{
public static double sum (double[][] array)
{
double sum = 0;
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[0].length; j++)
{
sum += array[i][j];
}
}
return sum;
}
public static void main(String[] args)
{
System.out.println("")
double a [][] = {
{1.2, 2},
{6, 7.2},
{11, 12}
};
System.out.println(sum(a));
}
}
You can use Scanner to read values from the keyboard. First, read the values for the dimensions of the array and then input values for the individual indices using nested loops.
You can also define a method to display the array in the tabular form.
Demo:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = scanner.nextInt();
System.out.print("Enter the number of columns: ");
int cols = scanner.nextInt();
double array[][] = new double[rows][cols];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.printf("Enter value for array[%d][%d]: ", i, j);
array[i][j] = scanner.nextDouble();
}
}
System.out.println("The array:");
print(array);
System.out.println("Sum of elements: " + sum(array));
}
public static double sum(double[][] array) {
double sum = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
sum += array[i][j];
}
}
return sum;
}
public static void print(double[][] array) {
for (double[] row : array) {
for (double col : row) {
System.out.print(col + "\t\t");
}
System.out.println();
}
}
}
A sample run:
Enter the number of rows: 3
Enter the number of columns: 2
Enter value for array[0][0]: 1.2
Enter value for array[0][1]: 2
Enter value for array[1][0]: 6
Enter value for array[1][1]: 7.2
Enter value for array[2][0]: 11
Enter value for array[2][1]: 12
The array:
1.2 2.0
6.0 7.2
11.0 12.0
Sum of elements: 39.4
I have added comments to the code for your understanding. Hope it answers your query.
public static void main(String[] args) {
//You can use the Scanner class for taking input
Scanner scan = new Scanner(System.in);
//take input for the number of rows in array
System.out.println("Enter no. of rows in array");
int row = scan.nextInt();
//take input for the number of columns in array
System.out.println("Enter no. of columns in array");
int col = scan.nextInt();
//create an array with row and col as dimensions of the 2D array
double[][] arr = new double[row][col];
//this for loop is for inputting elements in your array
for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++)
arr[i][j] = scan.nextInt();
//this for loop is for printing out the elements
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++)
System.out.print(arr[i][j] + " ");
System.out.println();
}
scan.close();
//now give a call to your sum() method and print the result
System.out.println(sum(arr));
}

cannot convert int to int[] and operator is undefined for types int[], int []

I am having issues printing out my array as my loops will not print due to it saying cannot convert int to int[]. and my addition and multiplication wont concatenate as it says the operator "+" and "*" are undefined for types int[],int[]
If someone could help me figure out why these issues are happening as I though I had it written correctly. IM not very knowledgeable with arrays so any information would be greatly appreciated
import java.util.*;
public class Matrix {
public static void main (String[] args) {
System.out.println("Please enter number of rows");
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
System.out.println("Please enter number of columns");
int column = sc.nextInt();
int[][] a1 = new int[row][column];
int[][] a2 = new int[row][column];
for(int r = 0; r < row; r++) {
for(int c = 0; c < column; c++) {
System.out.println(String.format("Enter first [%d][%d] integer",r,c));
a1[r]= sc.nextInt(); //cannot convert int to int[]
}
}
for(int r = 0; r < row; r++) {
for(int c = 0; c < column; c++) {
System.out.println(String.format("Enter first [%d][%d] integer",r,c));
a2[r]= sc.nextInt(); cannot convert int to int[]
}
}
System.out.println("First Matrix: ");
PrintArray(a1);
System.out.println("First Matrix: ");
PrintArray(a2);
add(a1,a2);
multiply(a1,a2);
}
public static void add(int[][] a1,int[][] a2) {
int row = a1.length;
int column = a1[0].length;
int[][] add = new int [row][column];
for(int r = 0; r < row; r++) {
for(int c = 0; c < column; c++) {
add[r] = (a1[r] + a2[r]); // operator "+" is undefined for types int[],int[]
}
}
System.out.println("Sum of added arrays: "+ add);
}
public static void multiply(int[][] a1,int[][] a2) {
int row = a1.length;
int column = a1[0].length;
int[][] add = new int [row][column];
for(int r = 0; r < row; r++) {
for(int c = 0; c < column; c++) {
add[r] = (a1[r] * a2[r]); // operator "*" is undefined for types int[],int[]
}
}
System.out.println("Sum of added arrays: "+ add);
}
public static void PrintArray(int[][] matrix) {
for(int r = 0; r < matrix.length; r++ ) {
for(int c = 0; c <matrix[0].length; c++) {
System.out.println(matrix[r] + "\t" );
}
System.out.println();
}
}
}
you have to put this code then it works
your code is fine you just forgot to put an array with two dimensions a[r][c]
import java.util.*;
public class Matrix {
public static void main (String[] args) {
System.out.println("Please enter number of rows");
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
System.out.println("Please enter number of columns");
int column = sc.nextInt();
int[][] a1 = new int[row][column];
int[][] a2 = new int[row][column];
for(int r = 0; r < row; r++) {
for(int c = 0; c < column; c++) {
System.out.println(String.format("Enter first [%d][%d] integer",r,c));
a1[r][c]= sc.nextInt(); // 2 dimensions a1[r] is not fine
}
}
for(int r = 0; r < row; r++) {
for(int c = 0; c < column; c++) {
System.out.println(String.format("Enter first [%d][%d] integer",r,c));
a2[r][c]= sc.nextInt(); // 2 dimensions a2[r] is not fine
}
}
System.out.println("First Matrix: ");
PrintArray(a1);
System.out.println("First Matrix: ");
PrintArray(a2);
add(a1,a2);
multiply(a1,a2);
}
public static void add(int[][] a1,int[][] a2) {
int row = a1.length;
int column = a1[0].length;
int[][] add = new int [row][column];
for(int r = 0; r < row; r++) {
for(int c = 0; c < column; c++) {
add[r][c] = (a1[r][c] + a2[r][c]); // operator "+" is undefined for types int[],int[]
}
}
System.out.println("Sum of added arrays: "+ add);
}
public static void multiply(int[][] a1,int[][] a2) {
int row = a1.length;
int column = a1[0].length;
int[][] add = new int [row][column];
for(int r = 0; r < row; r++) {
for(int c = 0; c < column; c++) {
add[r][c] = (a1[r][c] * a2[r][c]); // operator "*" is undefined for types int[],int[]
}
}
System.out.println("Sum of added arrays: "+ add);
}
public static void PrintArray(int[][] matrix) {
for(int r = 0; r < matrix.length; r++ ) {
for(int c = 0; c <matrix[0].length; c++) {
System.out.println(matrix[r] + "\t" );
}
System.out.println();
}
}
}
your output for 2 row 2 column for example
Please enter number of rows
2
Please enter number of columns
2
Enter first [0][0] integer
12
Enter first [0][1] integer
36
Enter first [1][0] integer
25
Enter first [1][1] integer
23
In your example, a1 and a2 are 2 dimensional arrays.
Your for loop iteration is fine, but a1[r] is an int[] and not int.
You are trying to assign an integer value to an array, so you are getting a cannot convert error.
a1[r]= sc.nextInt();
Should be changed to:
a1[r][c] = sc.nextInt();
Similarly addition and multiplication are only available for the primitive type or wrappers but not on arrays.
add[r] = (a1[r] + a2[r]);
Has to be changed to:
add[r][c] = (a1[r][c] + a2[r][c]);
And
add[r] = (a1[r] * a2[r]);
Has to be changed to:
add[r][c] = (a1[r][c] * a2[r][c]);

Logical error while inserting elements in to Two dimensional array using Scanner

I'm trying to sum of rows of Matrix
When I just put elements in 2D array output is right but when I'm trying using Scanner output result is different
SAMPLE INPUT
2
1 2
3 4
Output:
3
7
Below code result correct
import java.io.*;
import java.util.*;
public class matrix {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int a[][] = {
{1, 2,},
{ 3, 4}
};
int rows, cols, sumRow, sumCol;
//Initialize matrix a
//Calculates number of rows and columns present in given matrix
rows = a.length;
cols = a[0].length;
//Calculates sum of each row of given matrix
for(int i = 0; i < rows; i++){
sumRow = 0;
for(int j = 0; j < cols; j++){
sumRow = sumRow + a[i][j];
}
System.out.println(sumRow);
}
//Calculates sum of each column of given matrix
for(int i = 0; i < cols; i++){
sumCol = 0;
for(int j = 0; j < rows; j++){
sumCol = sumCol + a[j][i];
}
}
}
}
Result incorrect if I'm trying with Scanner
import java.io.*;
import java.util.*;
public class matrix {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
int column = sc.nextInt();
int [][] a = new int[row][column];
for (int i = 0; i < row; i++)
{
for(int j = 0; j < column; j++) {
a[i][j] = sc.nextInt();
}
}
int rows, cols, sumRow, sumCol;
//Initialize matrix a
//Calculates number of rows and columns present in given matrix
rows = a.length;
cols = a[0].length;
//Calculates sum of each row of given matrix
for(int i = 0; i < rows; i++){
sumRow = 0;
for(int j = 0; j < cols; j++){
sumRow = sumRow + a[i][j];
}
System.out.println(sumRow);
}
//Calculates sum of each column of given matrix
for(int i = 0; i < cols; i++){
sumCol = 0;
for(int j = 0; j < rows; j++){
sumCol = sumCol + a[j][i];
}
}
}
}
With the sample input you've provided, you shouldn't be reading the number of rows and columns, but just a single int for the number of both rows and columns:
int size = sc.nextInt();
int [][] a = new int[size][size];
for (int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
a[i][j] = sc.nextInt();
}
}
I do not see any logical problem with your code. However, it is equally important to keep your code clean and user friendly. I recommend you address the following points if you are serious about programming:
The following declaration is unnecessary:
rows = a.length;
cols = a[0].length;
You can simply use row and column instead of creating rows and cols for the same thing.
You should remove all such unnecessary things which create noise in your code.
You have missed printing the sum of each column i.e.
System.out.println(sumCol);
You do not need to declare throws IOException with main for this code.
You should always display a message describing the input; otherwise, the user remains clueless about what he/she is supposed to input.
Given below is the code incorporating these comments:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int row = sc.nextInt();
System.out.print("Enter the number of columns: ");
int column = sc.nextInt();
int[][] a = new int[row][column];
for (int i = 0; i < row; i++) {
System.out.println("Enter " + column + " integers: ");
for (int j = 0; j < column; j++) {
a[i][j] = sc.nextInt();
}
}
int sumRow, sumCol;
// Calculates sum of each row of given matrix
for (int i = 0; i < row; i++) {
sumRow = 0;
for (int j = 0; j < column; j++) {
sumRow = sumRow + a[i][j];
}
System.out.println("Sum of row " + i + ": " + sumRow);
}
// Calculates sum of each column of given matrix
for (int i = 0; i < column; i++) {
sumCol = 0;
for (int j = 0; j < row; j++) {
sumCol = sumCol + a[j][i];
}
System.out.println("Sum of column " + i + ": " + sumCol);
}
}
}
A sample run:
Enter the number of rows: 3
Enter the number of columns: 4
Enter 4 integers:
1 9 2 8
Enter 4 integers:
2 8 3 7
Enter 4 integers:
3 7 4 6
Sum of row 0: 20
Sum of row 1: 20
Sum of row 2: 20
Sum of column 0: 6
Sum of column 1: 24
Sum of column 2: 9
Sum of column 3: 21

Java Matrix using Arrays

I'm struggling to find an answer to the problem below. User inputs rows and columns. Example below is given for 4 x 4 matrix.
1 8 9 16
2 7 10 15
3 6 11 14
4 5 12 13
I cannot find how to relate the numbers when printing the array. The only obvious relation is how it goes downwards and upwards. From my perspective looks really hard. I'm just a beginner.
Not quite sure if there is any point to post the code, but it's just the basic lines:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter your array rows: ");
int rows = scanner.nextInt();
System.out.println("Please enter your array columns: ");
int columns = scanner.nextInt();
int[][] array = new int[rows][columns];
int counter = 0;
for (int j = 0; j < columns; j++){
for (int i = 0; i < rows; i++) {
counter++;
array[i][j]=...(stuck at the beginning);
}
Probably I'd need to use several loops , not only the above-mentioned or probably it is totally wrong ...
Thank you in advance!
I think this should do it.
int counter = 0;
boolean top_to_bottom=true;
for (int j = 0; j < columns; j++){
for (int i = 0; i < rows; i++) {
counter++;
if(top_to_bottom)
array[i][j]=counter;
else
array[rows-1-i][j]=counter;
}
if(top_to_bottom)
top_to_bottom=false;
else top_to_bottom=true;
}
It serves the purpose
import java.util.*;
public class Seq
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter your array rows: ");
int rows = scanner.nextInt();
System.out.println("Please enter your array columns: ");
int columns = scanner.nextInt();
int[][] array = new int[rows][columns];
int counter = 0;
for (int j = 0; j < columns; j++){
if(j%2==0)
{
for (int i = 0; i < rows; i++)
{
counter=counter+1;
array[i][j]=counter;
}
}
else
{
for (int i = rows-1; i >=0; i--)
{
counter=counter+1;
array[i][j]=counter;
}
}
}
for(int i=0;i<rows;i++)
{
for(int j=0;j<columns;j++)
{
System.out.print(array[i][j]+" ");
}
System.out.println();
}
}
}

Categories