Skip to content

Instantly share code, notes, and snippets.

@sdmg15
Last active January 2, 2017 05:30
Show Gist options
  • Save sdmg15/8b97a6670b3cd612d4c13ad14b15f23d to your computer and use it in GitHub Desktop.
Save sdmg15/8b97a6670b3cd612d4c13ad14b15f23d to your computer and use it in GitHub Desktop.
A manner to compute matrix trace, and display it with matrix data.
import java.util.Scanner;
public class sdz {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int numberOfrows ;
System.out.print("Enter the number of row and cols in the matrix : ");
int trace = 0;
numberOfrows = sc.nextInt();
int[][] tab = new int[numberOfrows][numberOfrows];
int tmp = 0;
for(int i = 0; i < numberOfrows; i ++){
for(int j =0; j < numberOfrows ; j++){
System.out.print("Enter the element of row " + (i+1) + " col " + (j+1));
tab[i][j] = sc.nextInt();
}
trace += tab[i][i];
}
System.out.println(trace);
for(int i = 0; i < tab.length; i++){
for(int j = 0; j< tab.length; j++){
System.out.print(tab[i][j] + " ");
}
System.out.println();
}
int smallest = tab[0][0];
for(int i = 0; i < tab.length; i++){
for(int j = 0; j < tab.length; j++){
if(tab[i][j] <= smallest){
smallest = tab[i][j];
}else{
smallest = smallest;
}
}
tmp = smallest;
}
System.out.println("The smallest element of the matrix is " + tmp);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment