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
Related
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.
I can print a single pine tree of varying sizes, but how do I print an exact replica of it next to it and underneath it, in a grid, using only for loops? This project utilizes a scanner so that the User can vary the size and number of section each tree has. The last portion of the project allows the user to pick how many trees to print, horizontally(next to each other) and vertically(underneath it). The exact wording and an example: Program makes use of a third input to print a number of trees. Ex: When NUM_TREES is three, a 3x3 grid of trees is printed. Any tips or hints are helpful, thank you.
Here is the code I have to get a single tree:
import java.util.Scanner;
public class PineTreeUpgrade {
static int numSections;
static int sectionSize;
public static void main(String[] args) {
askTheUserForInput();
System.out.println();
System.out.println("Here is your amazing creation! How artistic!");
printTree();
printStem();
}
public static void askTheUserForInput() {
Scanner userInput = new Scanner(System.in);
System.out.println("Hello, user!");
System.out.println("How many section would you like in your pine tree?");
numSections = userInput.nextInt();
System.out.println("Great! How big would you like each section to be?");
sectionSize = userInput.nextInt();
}
public static void printTree() {
for (int k = 0; k < numSections; k++) {
//prints the next section, each one increasing in size
for(int i = 0; i < sectionSize; i++) {
//prints the spaces and number of stars in each section
for (int j = i; j < (sectionSize - 1) + (numSections - 1) - k; j++){
System.out.print(" ");
}
System.out.print("/");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.print("*");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.println("\\");
}
}
}
public static void printStem() {
for(int i = 0; i < 2; i++) {
for (int j = 0; j < (sectionSize - 1) + (numSections -1) + 1; j++) {
System.out.print(" ");
}
System.out.println("|");
}
for (int i = 0; i < 1; i++) {
for (int j = 0; j < (sectionSize - 1) + (numSections -1); j++) {
System.out.print(" ");
}
System.out.print("___");
}
}
}
import java.util.Scanner;
public class PineTreeUpgrade {
static int numSections;
static int sectionSize;
static int gridSize;
public static void main(String[] args) {
askTheUserForInput();
System.out.println();
System.out.println("Here is your amazing creation! How artistic!");
for(int x = 0; x < gridSize; x++){
printTree();
printStem();
System.out.println();
}
}
public static void askTheUserForInput() {
Scanner userInput = new Scanner(System.in);
System.out.println("Hello, user!");
System.out.println("How many section would you like in your pine tree?");
numSections = userInput.nextInt();
System.out.println("Great! How big would you like each section to be?");
sectionSize = userInput.nextInt();
System.out.println("What grid size do you want?");
gridSize = userInput.nextInt();
}
public static void printTree() {
for (int k = 0; k < numSections; k++) {
//prints the next section, each one increasing in size
for(int i = 0; i < sectionSize; i++) {
//prints the spaces and number of stars in each section
for(int x = 0; x < gridSize; x++){
for (int j = i; j < (sectionSize - 1) + (numSections - 1) - k; j++){
System.out.print(" ");
}
System.out.print("/");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.print("*");
for (int j = 0; j < i + k; j++) {
System.out.print("*");
}
System.out.print("\\");
int width = 2 * (sectionSize + numSections - 1) + 1;
if(x != gridSize - 1){
for(int y = sectionSize + numSections + k + i; y < width; y++)
System.out.print(" ");
System.out.print(" ");
}
}
System.out.println();
}
}
}
public static void printStem() {
for(int i = 0; i < 2; i++) {
for(int x = 0; x < gridSize; x++){
for (int j = 0; j < (sectionSize - 1) + (numSections -1) + 1; j++)
System.out.print(" ");
System.out.print("|");
if(x != gridSize - 1)
for (int j = 0; j <= sectionSize + numSections; j++)
System.out.print(" ");
}
System.out.println();
}
for (int i = 0; i < 1; i++) {
for(int x = 0; x < gridSize; x++){
for (int j = 0; j < (sectionSize - 1) + (numSections -1); j++)
System.out.print(" ");
System.out.print("___");
if(x != gridSize - 1)
for (int j = 0; j < sectionSize + numSections; j++)
System.out.print(" ");
}
}
}
}
width_of_double_pine_tree = 2 * width_of_single_pine_tree + buffer_between_trees
For loops don't have to define a variable, they can use an existing one.
int j = 0;
for(; j < (width_of_double_pine_tree - (width_of_single_pine_tree + buffer_between_trees)); j++)
//this is your first tree
for(; j < (width_of_double_pine_tree - width_of_single_pine_tree); j++)
//this is your buffer (S.o.p spaces)
for(; j < (width_of_double_pine_tree); j++)
//this is your second tree
Your canvas is always a rectangle, you subtract from it, the edge spaces you aren't using, void space with spaces (tabs ruin everything, because they aren't uniform, use spaces in everything!)
Use your i variable to keep track of the height that you're currently painting on your canvas so you can math in your pine needles and trunk. For the second tree, in the math portion, offset your j by the buffer_between_trees and width_of_single_tree if(i < (j + buffer + width)), etc.
Make sure you do the assignment before you tackle extra stuff.
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] + " ");
}
}
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();
}
}
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);
}