I have a homework question that I have trouble debugging. The purpose of the program is to state which rows and columns have the same numbers, as well as major and minor diagonals. So far I have kind of figured out the rows and columns that are the same, and printing them.
Here is the output of the program thus far:
0 0 0 0 0 0 0 0
0 0 1 0 1 0 0 0
0 0 0 0 1 0 1 0
0 0 1 0 0 1 1 0
0 0 1 0 0 1 1 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 0
All 0 on row 0
All 0 on column 0
All 0 on column 1
All 0 on column 1
All 0 on column 7
All 0 on column 7
As you can see the column print is repeated, and I can't figure out why and how to fix it. I also have a problem in which it is not displaying row 6 as being all the same.
My desired output should be:
All 0 on row 0
All 0 on row 6
All 0 on column 0
All 0 on column 1
All 0 on column 7
Thank you in advance.
import java.util.Scanner;
public class javaTest
{
// Main method
public static void main(String[] args)
{
int[][] array = {
{0,0,0,0,0,0,0,0},
{0,0,1,0,1,0,0,0},
{0,0,0,0,1,0,1,0},
{0,0,1,0,0,1,1,0},
{0,0,1,0,0,1,1,0},
{0,0,0,0,0,0,1,0},
{0,0,0,0,0,0,0,0},
{0,0,1,1,1,1,1,0}
};
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
System.out.print(array[i][j] + " ");
System.out.println();
}
checkRow(array);
checkCol(array);
}
// Check if the row is the same
public static void checkRow(int array[][])
{
String checkRow = "";
int rowCount = 0;
int count = 0;
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length;j++)
{
// Create a new array to compare
int num = array[i][j];
for(int k = 0; k < array[i].length; k++)
{
// Check if the first number of the row is equal to the next
if(num == array[j][k])
// If so increment count
count++;
else
count = 0;
}
// If all numbers of the row is the same, total would be 8 and print
if(count == array.length)
System.out.println("All " + num + " on row " + rowCount);
}
rowCount++;
}
}
// Check if column is the same
public static void checkCol(int array[][])
{
String checkCol = "";
int colCount = 0;
int count = 0;
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
int num = array[i][j];
for(int k = 0; k < array[i].length; k++)
{
if(num == array[k][i])
count++;
else
count = 0;
}
if(count == array.length)
System.out.println("All " + num + " on column " + colCount);
}
colCount++;
}
}
}
I don't think you need 3 nested for loop in your checkRow or checkCol methods. I'll explain how you could it with just 2 for methods for checkCol.
You would still have your outer 2 for loops
i going from 0 to array.length - 1
j going from 0 to array[i].length - 1
Then inside this for loop, you check if every element at array[i][j] is equal to array[0][j] (which is the element at the 0th row for that particular column).
Maintain a boolean flag that you set to false if any of the elements in a particular column is not equal to the element at the 0th row of the column. Make sure this flag is set to true before you even enter the for loop.
Outside of the inner for loop, you check if the flag is set to true, if so print that particular column and number. Reset the flag to true.
I think this would probably fix up the issue of printing columns multiple times. You could do something similar for the rows as well.
Let me know if you don't understand any of the steps.
I believe that this is your problem
if(count == array.length)
System.out.println("All " + num + " on row " + rowCount);
}
rowCount++;// this is inside the first for loop but not the second. so it has to go through all the other inner for loops before it can count this again so its skipping rows
This problem can be solved using two for-loops, as suggested by #maesydy. Here is the implementation with output.
public class Test {
// Main method
public static void main(String[] args) {
int[][] array = {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 1, 0},
{0, 0, 1, 0, 0, 1, 1, 0},
{0, 0, 1, 0, 0, 1, 1, 0},
{0, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 1, 1, 0}
};
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++)
System.out.print(array[i][j] + " ");
System.out.println();
}
checkRow(array);
checkCol(array);
}
/**
* Check if all elements of row are same
* #param a array
*/
public static void checkRow(int a[][]) {
for (int r= 0; r < a.length; r++) {
int rowNum = r + 1;
boolean isMatching = true;
for (int c = 0; c < a[r].length -1; c++) {
//Compare two subsequent columns in same column
if(a[r][c] != a[r][c+1]) {
isMatching = false;
break;
}
}
//If all elements matched print output
if(isMatching) {
System.out.println("Row " + rowNum + " has all matching elements");
}
}
}
/**
* Check if all elements of column are same
* #param a array
*/
public static void checkCol(int a[][]) {
for (int c = 0; c < a.length; c++) {
int colNum = c + 1;
boolean isMatching = true;
for (int r = 0; r < a[c].length -1; r++) {
//Compare two subsequent rows in same column
if(a[r][c] != a[r+1][c]) {
isMatching = false;
break;
}
}
//If all elements matched print output
if(isMatching) {
System.out.println("Column " + colNum + " has all matching elements");
}
}
}
}
Notes: I have used indexes named r/c to depict row and column index.
Output:
0 0 0 0 0 0 0 0
0 0 1 0 1 0 0 0
0 0 0 0 1 0 1 0
0 0 1 0 0 1 1 0
0 0 1 0 0 1 1 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0
0 0 1 1 1 1 1 0
Row 1 has all matching elements
Row 7 has all matching elements
Column 1 has all matching elements
Column 2 has all matching elements
Column 8 has all matching elements
Hope this helps. Happy programming, enjoy!
Related
I have a 2d array like this:
2 0 0 2 0 4
2 0 0 2 0 4
2 0 0 2 0 4
And I want to shift all the zeros to the left, so for that I made this method:
public static void shiftLeft(int [][] array){
for (int j = 0; j < array.length; j++) {
for (int i = 0; i < array.length - 1; i++) {
if ((array[j][i] != 0) && (array[j][i + 1] == 0)) {
array[j][i + 1] = array[j][i];
array[j][i] = 0;
}
}
}
}
But the output I get is this:
0 0 2 0 2 4
0 0 2 0 2 4
0 0 2 0 2 4
How can I make all zeros to go to the left?
In my opinion the easiest way to do this is using 3 nested loops.
Variable i iterates over the rows.
Variable j1 finds the first nonzero element starting from the left of each row.
Variable j2 finds the first zero element after j1 and swaps them.
The code below assumes that the bidimensional matrix A was declared as A[N][M], where N and M are respectively the number of rows and number of columns.
for(int i =0;i<N;i++){
for(int j1=0;j1<M;j1++){
if (A[i][j1]==0)
continue;
for(int j2=j1;j2<M;j2++){
if( A[i][j2]==0){
//swap
int tmp=A[i][j1];
A[i][j1]=A[i][j2];
A[i][j2]=tmp;
}
}
}
}
In fact Trugis's answer is also correct but it will just swap the zero with the first non zero. So the order of the numbers will change.
This answer will not change the order of the numbers :
int[][] A = { { 2, 3, 4, 2, 4, 4, 5, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 4, 3, 4, 5, 6 },
{ 2, 0, 4, 2, 0, 4, 1, 2, 3, 4 }};
int N = A.length;
int M = A[0].length;
int firstZeros = 0;
for(int i = 0; i < N; i++) { // Loop over the rows
for(int j1 = 0; j1 < M; j1++) {
// If there is a zero we pass by
if (A[i][j1] == 0 && firstZeros == j1) {
firstZeros++;
continue;
}
// Otherwise, we have a value so we want to check if there is a zero afterwards
for(int j2 = j1+1; j2 < M; j2++) {
// If we find a zero we move it to the left
if(A[i][j2] == 0) {
for (int j3 = j2; j3 > firstZeros; j3--) {
// Change zero with previous value
A[i][j3] = A[i][j3-1];
A[i][j3-1] = 0;
}
firstZeros++;
}
}
}
firstZeros = 0;
}
I have a multi-dimensional array made of two separate arrays.
// slot1 = new int[][] { {Array1}, {Array2}}
slot1 = new int[][] { {1, 2 ,3}, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
I'm trying to do two separate things.
I'm trying to print the contents of array slot1 , but with a space between each array. For example, I want my output to be something like:
1 2 3 ----- 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Note: where I have the dashes, I'll actually be adding a space
I want to know how I would modify the values of each internal array separately?
For example, how would I change the value of index 5 in Array2? And add value 1? So that if I print slot1 array, my values should look like this?
1 2 3 ----- 0 0 0 0 0 1 0 0 0 0 0 0 0 0
I have looked other questions related to this, but I haven't found a clear answer when working two a 2D array made of other arrays? Would ArrayList work best for this type of scenario?
Here is my complete Code
public class Sandbox {
static int[][] slot1;
public static void main(String[] args) {
Sandbox.setCache();
Sandbox.displayCache();
}
public static void setCache() {
slot1 = new int[][] { { 1, 2 ,3}, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } };
}
public static void displayCache() {
for (int i = 0; i < slot1.length; i++) {
for (int j = 0; j < slot1[i].length; j++) {
System.out.print(slot1[i][j] + " "); // How to add a space between i and j?
}
}
}
}
I will assume that this is a beginner question.
To put spaces between the two arrays in slot1 you can proceed in this simple way:
public static void displayCache() {
for (int i = 0; i < slot1.length; i++) {
for (int j = 0; j < slot1[i].length; j++) {
System.out.print(slot1[i][j] + " ");
}
System.out.print(" "); //Add spaces here using your preferred way
}
}
But, you'd probably doesn't want the spaces after the last array, so this is another (better) way:
public static void displayCache() {
if(slot1.length > 0) {
for (int j = 0; j < slot1[0].length; j++)
System.out.print(slot1[0][j] + " ");
for (int i = 1; i < slot1.length; i++) { //Note: now i = 1
System.out.print(" "); //Add spaces here using your preferred way
for (int j = 0; j < slot1[i].length; j++)
System.out.print(slot1[i][j] + " ");
}
}
}
To remove the space at the end of each array last element, you can apply the same logic (this is a work for you).
To change a value in slot1 (using your example as example) you can do this:
slot1[1][5] = 1;
public class Numbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
int w[] = new int[] { 5, 4, 3, 2, 3 };
int max = 0;
int min = 0;
for (int i = 0; i < w.length; i++) {
if (w[i] > w[i]++) {
w[i] = max;
}
for (int j = 0; j < w.length; j++) {
if (w[j] < w[j]++) {
w[j] = min;
}
System.out.println(min + max);
}
}
}
}
All I get for output is this:
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Why does it even print the 0 that many times?
you never mutate min or max. within your loops you change the array. when you set w[i] = max, you are setting the item at place i to = 0(which is what you initialized min and max at.)
change the order to
max=w[i]
or
min=w[i]
as for WHY its printing that many times, you have the print statement in your for loop, move the print statement outside of the for loop
I would also change the print statement to be
System.out.println("Max= " + max + "Min= " + min)
Just makes the results a little clearer
I apologize for my english. So far I got this code that sort an array. The user input 10 numbers and after that, the program makes the sorting. But what I want is that every time the user inputs a number, the program immediately makes the sort. How can I do that?
For example, if I input 5 and then 3, immediately takes the 3 to the first position. And then if I put 2, immediately take it to the first position and sort the others (2,3,5). Then if I put 1, takes it to the first position, sorting the others(1,2,3,5) and so on.
import java.util.Scanner;
public class Nine{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int temp = 0;
int[] num = new int[10];
for(int i = 0; i < 10; i++){
System.out.print("Número: ");
num[i] = input.nextInt();
}
System.out.println();
for(int i = 0; i < 10; i++){
System.out.print(num[i] + " ");
}
System.out.println();
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10 - i - 1; j++){
if(num[j+1] < num[j]){
temp = num[j+1];
num[j+1] = num[j];
num[j] = temp;
}
}
}
System.out.println();
for(int i = 0; i < 10; i++){
System.out.print(num[i] + " ");
}
}
}
Now I have this code and it works. It does what I wanted to do. But to me it's a little bit complicated. I'm still a beginner. I understand what it does but is there a better way to do it. An easier way? Thanks
import java.util.Scanner;
public class practice {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int[] num = new int[10];
int n = 0, l = 0, t = 0;
for(int i = 0; i < num.length; i++){
System.out.print("Número: ");
n = input.nextInt();
l = 0;
while(num[l] < n && l < i){
l = l + 1;
}
t = i;
while(t > l){
num[t] = num[t - 1];
t = t - 1;
}
num[l] = n;
for(int temp : num){
System.out.print(temp + " ");
}
System.out.println();
}
}
}
here you go
public class TestProgram {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
int temp = 0;
int[] num = new int[10];
for (int b = 0; b < 10; b++) {
System.out.println("Número: ");
num[b] = input.nextInt();
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10 - i - 1; j++) {
if (num[j + 1] < num[j]) {
temp = num[j + 1];
num[j + 1] = num[j];
num[j] = temp;
}
}
}
System.out.println();
for (int k = 0; k < 10; k++) {
System.out.println(num[k] + " ");
}
}
}
}
To do this create a sort method which you can call to sort an array then return a new sorted array. Next every time a user inputs run a for loop which will create an array with the current amount entered. While entering just use i+1. Finally, with the new array call the sort method and the sorted array will be returned and you can do as you wish with the new array.
You make things more difficult for yourself using an array but assuming you want to start with an array of size 10 filled with 0s (so 0 is not a valid input) the basic algorithm is to go through the currently sorted array and if the current value is less than the indexed value move all the values in the sorted array to the right and insert the current value at the current index. As others have already mentioned for larger datasets this is very inefficient but for an array of size 10 it's not a big deal.
int current = input.nextInt();
for (int j = 0; j < sorted.length; j++) {
if (sorted[j] == 0) {
sorted[j] = current;
break;
}
if (current < sorted[j]) {
for (int k = sorted.length - 1; k > j; k--) {
sorted[k] = sorted[k - 1];
}
sorted[j] = current;
break;
}
}
Here's what the output at each iteration would look like for the input 5, 3, 2, 1, 4, 10, 20, 15, 13, 5:
5 0 0 0 0 0 0 0 0 0
3 5 0 0 0 0 0 0 0 0
2 3 5 0 0 0 0 0 0 0
1 2 3 5 0 0 0 0 0 0
1 2 3 4 5 0 0 0 0 0
1 2 3 4 5 10 0 0 0 0
1 2 3 4 5 10 20 0 0 0
1 2 3 4 5 10 15 20 0 0
1 2 3 4 5 10 13 15 20 0
1 2 3 4 5 5 10 13 15 20
Building a Sudoku game. How can I update my 2 dimensional array with the users input if they decide to input data using columns and not rows? I can not figure out why it will not work properly?
else if (dataSelection == 2) {
if (boardSize == 1) {
int column = 1;
int column2 = 0;
while (column < 4) {
Scanner firstColumn4x4 = new Scanner(System.in);
System.out.println("Please enter four values using commas for column " + column);
String column1Values4x4 = firstColumn4x4.next();
String strArray[] = column1Values4x4.split(",");
int arraySidesInteger[] = new int[strArray.length];
for (int i = 0; i < strArray.length; i++) {
arraySidesInteger[i] = Integer.parseInt(strArray[i]);
}
***fourArray[column-1][column2] = arraySidesInteger[column2];*** //can not figure out thisstatement
for (int i = 0; i < fourArray.length; i++)
{
for (int j = 0; j < fourArray.length; j++)
System.out.print(fourArray[i][j] + " ");
System.out.println();
}
column++;
column2++;
}
If user inputs 1,2,3,4 for column 1, I want it to print out:
1 0 0 0
2 0 0 0
3 0 0 0
4 0 0 0
However I keep getting:
1 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
Thanks!
You're never looping over the rows to actually insert the user's values. For the line
fourArray[column-1][column2] = arraySidesInteger[column2];
you probably want something like
for (int i = 0; i < arraySidesInteger.length(); i++) {
fourArray[i][column2] = arraySidesInteger[i];
}
and just do away with the column variable altogether.
(EDIT: Accordingly, since column2 is the variable that you are actually using to index to the right column, the while loop should be looping on that, not column.)