Get the major diagonal and sub diagonal for matrix - java

Am confused about getting the "sub diagonal" for 2D array, I can get the columns , rows and major diagonal, but i don't know how to get sub diagonal, here is what i'v done so far:
int size = input.nextInt();
int[][] list = initiateArr(size);
for (int i = 0; i < list.length; i++) {
for (int j = i ; j < i + 1; j++) {
System.out.print(list[i][j] + " ");
}
}
My attempt for "sub diagonal":
for (int i = 0; i < list.length; i++) {
for (int j = (list.length / 2) + 1; j > list.length - (i + 1) ; j--) {
System.out.print(list[i][j] + " ");
System.out.println("j = " + j);
}
}
How to get sub diagonal?
I can get the major diagonal right as described in the output 1111, the sub diagonal should be 1010.

Here is the answer. Let me know if you have any questions.
public static void main(String args[]) {
int[][] list = {{1,2,3}, {4,5,6},{7,8,9}};
int matrixSize = 3;
System.out.println("Subdiagonal");
for( int i = 0; i < matrixSize ; i ++){
System.out.print( list[i][matrixSize - i -1] + " ");
}
System.out.println("");
System.out.println("Major ");
for( int i = 0; i < matrixSize ; i ++){
System.out.print( list[i][i] + " ");
}
}

Related

Index out of bounds, error in IF line of code

So, this code is supposed to do the following:
Check if the neighboring members in a row of the square matrix and write them if their sum is an even number and also write the members which sum is a odd number. But it sends an error in the first if statement. But it only does the first row and outputs the Index out of bounds. Here is the code:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Ucitati broj clanova kvadratne matrice: ");
int n = scan.nextInt();
int niz[][] = new int[n][n];
System.out.println("Ucitati clanove matrice: ");
for (int i = 0; i < niz.length; i++) {
for (int j = 0; j < niz.length; j++) {
System.out.print("n[" + i + ", " + j + "]" + "=");
niz[i][j] = scan.nextInt();
}
}
System.out.println();
for (int i = 0; i < niz.length; i++) {
for (int j = 0; j < niz.length; j++) {
System.out.print(niz[i][j] + " ");
}
System.out.println();
}
System.out.println("Susedni clanovi cija je suma parna su: ");
for (int i = 0; i < niz.length; i++) {
for (int j = 0; j < niz.length; j++) {
if ((niz[i][j] + niz[i][j + 1]) % 2 == 0) {
System.out.print(niz[i][j] + " " + niz[i][j + 1] + "; ");
}
}
}
System.out.println("Susedni clanovi cija je suma neparna su: ");
for (int i = 0; i < niz.length; i++) {
for (int j = 0; j < niz.length; j++) {
if ((niz[i][j] + niz[i][j + 1]) % 2 != 0) {
System.out.print(niz[i][j] + " " + niz[i][j + 1] + "; ");
}
}
}
}
System.out.println are in Serbian so if u need a translate let me know, but that shouldn't be needed.

Optimal way to creating a multiplication table -java

Hello I am trying to create a java program that output multiplication grid and I want to know if there is way to do it without having a lot of if statement if I had n values. Here is the code
public class MultiplicationGrid {
public static void main(String[] args) {
int num[][] = new int[4][4];
//String size[][] = new String[1][13];
for(int i = 0; i < num.length; ++i) {
for(int j = 0; j < num[i].length;++j) {
num[i][j] = (j+1)*(i+1);
}
}
int count = 0;
int count1 = 0;
int count2 = 0;
int count3 = 0;
for (int i = 0; i < num.length; ++i) {
for(int j = 0; j < num[i].length; ++j) {
if(count == 0) {
count = num [i][j];
continue;
}
if(count1 == 0) {
count1 = num [i][j];
continue;
}
if(count2 == 0) {
count2 = num [i][j];
continue;
}
if(count3 == 0) {
count3 = num [i][j];
}
System.out.println(count + " " + (count1) + " " + (count2) + " " + (count3));
count = 0;
count1 = 0;
count2 = 0;
count3 = 0;
}
}
}
}
Thanks in advance.
You can define the table size and print the multiplication grid as follows:
public static void main(String[]args) {
final int TABLE_SIZE = 12;
// Declare the rectangular array to store the multiplication table:
int[][] table = new int[TABLE_SIZE][TABLE_SIZE];
// Fill in the array with the multiplication table:
for(int i = 0 ; i < table.length ; ++i) {
for(int j = 0 ; j < table[i].length ; ++j) {
table[i][j] = (i+1)*(j+1);
}
}
// Output the table heading
System.out.print(" :"); // Row name column heading
for(int j = 1 ; j <= table[0].length ; ++j) {
System.out.print((j<10 ? " ": " ") + j);
}
System.out.println("\n-------------------------------------------------------");
// Output the table contents
for(int i = 0 ; i < table.length ; ++i) {
System.out.print("Row" + (i<9 ? " ":" ") + (i+1) + ":");
for(int j = 0; j < table[i].length; ++j) {
System.out.print((table[i][j] < 10 ? " " : table[i][j] < 100 ? " " : " ") + table[i][j]);
}
System.out.println();
}
}

java - Sorting matrix with function

I have an exercise where I need to sort a matrix.
The matrix should be sorted so that a smaller number would be in the first row on the left side of the matrix and so on ...
Until now I have written code to sort the rows in a matrix and the columns but I can not sort out all the matrix.
Clarification: Do not use a one-dimensional array helped.
I think I'm almost done I'd love to help
import java.util.Scanner;
public class matSortingCol {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter length of mat:");
int length = in.nextInt();
System.out.println("Enter search number: ");
int num = in.nextInt();
int[][] mat = new int[length][length];
int[] arrSorting = new int[length];
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat.length; j++) {
mat[i][j] = in.nextInt();
}
}
System.out.println("Orginal matrix: ");
// print matrix
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat.length; j++) {
System.out.print("[" + mat[i][j] + "]");
}
System.out.println();
}
for (int j = 0; j < mat.length; j++) {
for (int i = 0; i < mat.length; i++) {
arrSorting[i] = mat[i][j];
}// end for i
sorting(arrSorting);
System.out.println();
for (int i = 0; i < mat.length; i++) {
mat[i][j] = arrSorting[i];
}// end for i
}// end for j
System.out.println();
// print matrix
System.out.println("matrix after sorting: ");
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat.length; j++) {
System.out.print("[" + mat[i][j] + "]");
}// end for j
System.out.println();
}// end for i
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat.length; j++) {
if (num == mat[i][j]) {
int search = 0;
search = mat[i][j];
System.out.print("Find number " + "[" + search + "]"
+ " >> " + "index of row is: " + "[" + i + "]"
+ ", " + "index of col is: " + "[" + j + "]");
System.out.println();
}
}
}
}// end main
public static void sorting(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int min = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[min]) {
min = j;
}// end if
}// end j
if (i != min) {
int swap = arr[i];
arr[i] = arr[min];
arr[min] = swap;
}// end if
}// end i
System.out.print("The arr after sotring: ");
for (int i = 0; i < arr.length; i++) {
System.out.print("[" + arr[i] + "]");
}
}// end sorting
}// end class
Thank you

Already sorted array

I have an array including user's inputs. The program is about Bubble sort, Selection sort and Insertion sort. First Bubble, Second Selection and then Insertion sort comes.
I couldn't manage to solve a problem. When the code run into selection sort, the array is already sorted by bubble sort.
I tried to make 2 temporary arrays at first to use the "source array" at selection and insertion sorting but those arrays re-arranged by bubble sort again. ( Which I don't understand why )
Is there any way to sort my array seperately or I have to make them methods ? I'm counting the swaps and comparisons also BTW. Thanks !
System.out.println("• Please enter the number of elements in the Sorting Bag:");
length = input.nextInt();
System.out.println("• The number of elements: " + length);
int[] SorBag = new int[length];
int[] SorBag2 = new int[length];
int[] SorBag3 = new int[length];
System.out.println("• Please enter the elements of Sorting Bag:");
for (int i = 0; i < SorBag.length ; i++) {
SorBag[i] = input.nextInt();
}
SorBag2 = SorBag;
SorBag3 = SorBag;
System.out.print("• Elements in the Sorting Bag are:");
for (int j = 0; j < SorBag.length; j++) {
System.out.print(" " + SorBag[j]);
}
System.out.println("");
System.out.println("");
//Bubble Sort
for (int i = 1; i < SorBag.length; i++) {
for (int j = 0; j < SorBag.length - i; j++) {
BComparison++;
if (SorBag[j] > SorBag[j + 1]) {
BSwaps++;
temp1 = SorBag[j + 1];
SorBag[j + 1] = SorBag[j];
SorBag[j] = temp1;
}
}
}
System.out.print("• Bubble Sort:");
for (int k = 0; k < SorBag.length; k++) {
System.out.print(" " + SorBag[k] + " ");
}
System.out.print("Comparisons: " + BComparison + " Swaps: " + BSwaps);
System.out.println(" ");
//Selection Sort
for (int i = 0; i < SorBag2.length; i++) {
min = i;
for (int j = i + 1; j < SorBag2.length; j++) {
SComparison++;
if (SorBag2[j] < SorBag2[min]) {
min = j;
}
if (min != i) {
temp2 = SorBag2[i];
SorBag2[i] = SorBag2[min];
SorBag2[min] = temp2;
SSwaps++;
}
}
}
System.out.print("• Selection Sort:");
for (int k = 0; k < SorBag2.length; k++) {
System.out.print(" " + SorBag2[k] + " ");
}
System.out.print("Comparisons: " + SComparison + " Swaps: " + SSwaps);
System.out.println(" ");
//Insertion Sort
for (int i = 1; i < SorBag3.length; i++) {
int j = 0;
while (j > i && SorBag3[j] < SorBag3[j - 1]) {
temp3 = SorBag3[j];
SorBag3[j] = SorBag3[j - 1];
SorBag3[j - 1] = temp3;
ISwaps++;
j--;
}
IComparison++;
}
System.out.print("• Insertion Sort:");
for (int k = 0; k < SorBag3.length; k++) {
System.out.print(" " + SorBag3[k] + " ");
}
System.out.print("Comparisons: " + IComparison + " Swaps: " + ISwaps);
System.out.println(" ");
}
}
SorBag2 = SorBag and SorBag3 = SorBag copies the reference of SorBag to the other two arrays, instead of only copying the data. So instead of:
System.out.println("• Please enter the elements of Sorting Bag:");
for (int i = 0; i < SorBag.length ; i++) {
SorBag[i] = input.nextInt();
}
SorBag2 = SorBag;
SorBag3 = SorBag;
Try this:
System.out.println("• Please enter the elements of Sorting Bag:");
for (int i = 0; i < SorBag.length ; i++) {
int nextInt = intput.nextInt();
SorBag[i] = nextInt;
SorBag2[i] = nextInt;
SorBag3[i] = nextInt;
}

Print largest number in a 2d array - why do my code print three numbers

I am trying to print out the largest number in a 2D array. My problem is that my output are three numbers instead of one - the largest. Why?
Here is my code:
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
int maxRows = 3;
int maxCols = 4;
int [] onedArray = new int [maxRows];
for (int i = 0; i < maxRows; i++){
onedArray[i] = (int) ((Math.random() * 100) * maxCols);
}
int [][] twodArray = new int[maxRows][];
for (int i = 0; i < maxRows; i++){
twodArray[i] = new int[maxCols];
}
for (int i = 0; i < twodArray.length; i++){
for (int j = 0; j < twodArray[i].length; j++){
twodArray[i][j] = (int) (Math.random() * 100);
}
}
System.out.println("2 - The 2D array: ");
for (int i = 0; i < twodArray.length; i++){
for (int j = 0; j < twodArray[i].length; j++){
System.out.print(twodArray[i][j] + " ");
}
System.out.println("");
}
int maxValue = 1;
System.out.println("\nMax values in 2D array: ");
for (int i = 0; i < twodArray.length; i++) {
for (int j = 0; j < twodArray.length; j++)
if (twodArray[i][j] > maxValue) {
maxValue = twodArray[i][j];
}
System.out.println(maxValue);
}
}
}
Everything up until the last sequence of instructions is correct (although poorly formatted).
Here is original:
int maxValue = 1;
System.out.println("\nMax values in 2D array: ");
for (int i = 0; i < twodArray.length; i++) {
for (int j = 0; j < twodArray.length; j++)
if (twodArray[i][j] > maxValue) {
maxValue = twodArray[i][j];
}
System.out.println(maxValue);
}
Here is better version:
int maxValue = 0;
System.out.println("\nMax values in 2D array: ");
for (int i = 0; i < twodArray.length; i++) {
for (int j = 0; j < twodArray[i].length; j++) {
if (twodArray[i][j] > maxValue) {
maxValue = twodArray[i][j];
}
}
System.out.println("Max value of row " + i + ": " + maxValue);
}
Look carefully and you'll see that I added the { character after the second for-loop.
If you wanted to find total max, and minimize open and close curly-braces here is another version:
int maxValue = 0;
System.out.println("\nMax values in 2D array: ");
for (int i = 0; i < twodArray.length; i++)
for (int j = 0; j < twodArray[i].length; j++)
if (twodArray[i][j] > maxValue)
maxValue = twodArray[i][j];
System.out.println("Maximum value: " + maxValue);
Good luck.
int m,n,max;
int a[][]=new int[10][10];
Scanner S=new Scanner(System.in);
System.out.println("Enter m*n matrix");
m=S.nextInt();
n=S.nextInt();
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
a[i][j]=S.nextInt();
}
}
max=a[0][0];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]>max)
{
max=a[i][j];
}
}
}
System.out.println(max);
Your line System.out.println(maxValue); needs to come out of the loop over the variable i. It's being printed 3 times because it's inside this loop.
This would be easier to see if your code was indented properly; this is a good habit to get into anyway.
The answer is in your code once it's indented correctly:
for (int i = 0; i < twodArray.length; i++) {
for (int j = 0; j < twodArray.length; j++)
if (twodArray[i][j] > maxValue) {
maxValue = twodArray[i][j];
}
System.out.println(maxValue);
}
}
Don't underestimate how useful good indentation can be for catching this kind of bug :)
int max;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of rows : ");
int n = sc.nextInt();
System.out.println("Enter number of columns : ");
int m = sc.nextInt();
int[][] array = new int[n][m];
System.out.println("Enter the elements of array : ");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
System.out.print("X[" + i + "," + j + "]" + "=");
array[i][j] = sc.nextInt();
}
}
max = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (array[i][j] > max) {
max = array[i][j];
}
}
}
System.out.println("Max value of the array is " + max);
}

Categories