Scanner array (n x n) - java

My code :
import java.util.Scanner;
class Test1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[][] num = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
System.out.print(num[i][j] + " ");
System.out.println();
}
}
the answer come out
0 0 0
0 0 0
0 0 0
but i want to make
1 0 0
0 1 0
0 0 1

As explain in the doc of Java, a int is initialized with the value 0. So when you only declare your array without setting specific value, it only contains 0. That's why your output is
0 0 0
0 0 0
0 0 0
You need to first initialize your array to put the value 1 in the wanted places. In your case you want to make an identity matrix, so put 1 in the places that have the same index for row and column (In the diagonal). This should look like this :
for (int i = 0; i < n; i++)
num[i][i] = 1;
Place this initialization before printing the array, so the full code will look like :
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[][] num = new int[n][n];
for (int i = 0; i < n; i++)
num[i][i] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
System.out.print(num[i][j] + " ");
System.out.println();
}

Related

Two-dimentional arrays [duplicate]

This question already has answers here:
Syntax for creating a two-dimensional array in Java
(13 answers)
Closed 3 years ago.
I am working with 2-D arrays and I require help on this topic. My task is to create a 2-D array such that it is n by n (i.e. the number of rows and columns are equal). Fill the array with alternating 0's and 1's
void setup()
{
int n=3;
// code to populate the array
// code to display the output of array in a table format
/* output should be as follows:
The expected result when n=3 should be as the following:
1 0 1
0 1 0
1 0 1
*/
}
Solution:
class test{
public static void main(String[] args) {
int n = 3;
int[][] arr = setup(n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(arr[i][j]);
}
System.out.println();
}
}
static int[][] setup(int n){
int[][] arr = new int [n][n];
boolean even = false;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = even ? 1 : 0;
even = !even;
}
}
return arr;
}
}
Output:
0 1 0
1 0 1
0 1 0
This should work.
You can replace the 3 that is given to n with any number you want as the 2D-Array's length.
void setup(){
int n = 3;
int count = 0;
int[][] myArray = new int[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(count % 2 == 0){
myArray[i][j] = 1;
}
else{
myArray[i][j] = 0;
}
System.out.print(myArray[i][j] + " ");
count++;
}
System.out.println();
}
}
Output:
0 1 0
1 0 1
0 1 0
It's not complex, you just have to iterate two loops, that's it. Although you can get the solution on different ways.
public static void main(String[] args) {
int n = 3;
int [][] arr = new int[n][n];
int val = 1;
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
arr[i][j] = val;
val = (val == 0) ? 1 : 0;
}
}
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
Output:
0 1 0
1 0 1
0 1 0
A simple implementation:
void setup()
{
int n=3;
final int[][] array = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((n * i + j) % 2 == 0) {
array[i][j] = 1;
}
}
}
}
Here is how you can approach your logic , create a counter and negate each time after it prints value.
public static void main(String[] args) {
int counter = 0;
int n = 3;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print(counter==0?1:0);
counter = ~counter;
}
System.out.println();
}
}
Output
101
010
101
Here is Online source code.

Scanner Two-dimensional array (n x n)

import java.util.Scanner;
class Test1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[][] num = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < 0; j++)
if (i == j)
num[i][j] = 1;
else
num[i][j] = 0;
for (int[] a : num) {
for (int b : a)
System.out.print(b + " ");
System.out.println();
}
}
}
i want to make
1 0 0
0 1 0
0 0 1
but my answer come out like
0 0 0
0 0 0
0 0 0
It looks like your code is kicking out of the inner loop, the one using j as the variable. You initialize j = 0, and then if j < 0 it will execute. However, 0 < 0 is false so it never iterates thru the inner loop. Try changing that line to
for (int j = 0; j < n; j++) and I'll expect you'll get the result you are looking for.
you need to loop through all elements in 2d array not just the 0th element. see modified for loop.
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[][] num = new int[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (i == j)
num[i][j] = 1;
else
num[i][j] = 0;
for (int[] a : num) {
for (int b : a)
System.out.print(b + " ");
System.out.println();
}
}
}

Compare rows in same 2d array in java using deepEquals method

user input n number of rows in 2d array
Elements are stored in an array of size [n][4].
Input Format:
First line of the input is an integer ā€œnā€ that denotes the number of rows.
Next n lines contain the elements of the row
Output Format:
if 2 rows match then separate it by hyphen
If no rows is same, print "None".
If the array size is negative, print "Invalid Input" and terminate the program.
Sample Input 1:
5
3 1 2 4
2 4 5 1
3 1 2 4
2 4 5 1
3 1 2 4
Sample Output 1:
1-3
1-5
2-4
3-5
Sample Input 2:
3
3 1 2 4
2 4 5 1
3 4 2 5
Sample Output 2:
None
Sample Input 3:
-5
Sample Output 3:
Invalid Input
i have tried the following code but it didnt work
import java.util.Arrays;
import java.util.Scanner;
import java.lang.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n <= 0)
{
System.out.println("Invalid Input");
}
else
{
int[][] data = new int[n][4];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 4; j++)
{
data[i][j] = sc.nextInt();
}
}
Object[] array = new Object[4];
Object[] array1 = new Object[4];
int idx = 0;
int idx1 = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 4; j++)
{
array[idx] = data[i][j];
array1[idx1] = data[i + 1][j];
idx++;
idx1++;
if (Arrays.deepEquals(array, array1))
{
System.out.println(i + "-" + j);
}
else
System.out.println("None");
}
}
} int[][] data = new int[n][4];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 4; j++)
{
data[i][j] = sc.nextInt();
}
}
Object[] array = new Object[4];
Object[] array1 = new Object[4];
int idx = 0;
int idx1 = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 4; j++)
{
array[idx] = data[i][j];
array1[idx1] = data[i + 1][j];
idx++;
idx1++;
if (Arrays.deepEquals(array, array1))
{
System.out.println(i + "-" + j);
}
else
System.out.println("None");
}
}
}}}
you are taking only the 1st elements in array and array1 in each step not the entire row.
what you need to do is you have to take entire 1 row and compare with the other rows more 'for' loops

How can I make a border to a matrix in java?

I have this matrix :
So 2 line and 2 columns.
1 2
3 4
I have the reading function
for (int i = 0; i < m; i++) {
for (int j = 1; j < n; j++) {
try {// System.out.println("number is ");
a[i][j] = scanner.nextInt();
} catch (java.util.NoSuchElementException e) {
// e.printStackTrace();
}
}
} //print the input matrix
How can I make a border to a matrix? I've seen that in java there is no index -1.
I want a border with a number. For example :
0 0 0 0
0 0 0 0
0 1 2 0
0 3 4 0
0 0 0 0
How should I make that border function?
So, you need a (m+2) x (n+2) matrix:
// initialize m and n
...
// initialize the matrix with 0s
int a[][] = new int[m+2][n+2];
Then ignore the first elements (i and j should skip 0) and the last elements (i should skip m+1, j should skip n+1):
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
try {
a[i][j] = scanner.nextInt();
} catch (java.util.NoSuchElementException e) {
// e.printStackTrace();
}
}
}
First you need to create the matrix with extra rows and columns, for example in your case a 4x4 matrix. and them put 0 on the borders
when i==0 or i==n-1, j==0 or j==n-1,
int a[][] = new int[4][4];
int n,m;
n=4;
m=4;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(i==0 || j==0 || i==m-1 || j==n-1){
//a[i][j] = 0;
a[i][j] = 1;
}
System.out.print(a[i][j]+" ");
}
System.out.println();
}
then
for (int i = 1; i < m-1; i++) {
for (int j = 1; j < n-1; j++) {
try {// System.out.println("number is ");
a[i][j] = scanner.nextInt();
} catch (java.util.NoSuchElementException e) {
// e.printStackTrace();
}
}
} //print the input matrix
Elaborating on my comment:
If you first put in words what creating a border means, then translating it from English to Java is a simple task.
Let's take a look at the the two matrices that you have given:
Original one:
1 2
3 4
With border:
0 0 0 0
0 1 2 0
0 3 4 0
0 0 0 0
We see that when creating a border, we increase the width and height of the matrix by 2 respectively (to have an empty row of 0s on the top-bottom, and left-right), so instead of a 2x2 matrix we now have 4x4, and the indices of the elements are incremented by 1 (every element is pushed one step to the right, and one step downwards).
Putting this is in code:
int[][] createBorder(int[][] matrix) {
//this is our 4x4 matrix
int[][] borderedMatrix = new int[matrix.length+2][matrix[0].length+2];
//fill the 4x4 matrix with 0's
for(int i = 0; i < borderedMatrix.length; i++) {
for(int j = 0; j < borderedMatrix[0].length; j++) {
borderedMatrix[i][j] = 0;
}
}
//copy the values of the 2x2 into the 4x4 matrix, but push them one step to the right, and one step downwards
for(int k = 0; k < matrix.length; k++) {
for(int l = 0; l < matrix[0].length; l++) {
borderedMatrix[k+1][l+1] = matrix[k][k];
}
}
return borderedMatrix;
}
The simplest way would be to create a matrix of n+1xm+1, populate the border with 0 then fill in the remainder with the original nxm matrix.
So if I had your example matrix
int[][] original = {{1, 2},
{3, 4}};
int borderWidth = original[0].length + 2;
int borderHeight = original.length + 2;
int borderArray = new int[borderHeight][borderWidth];
for (int i = 0; i < borderWidth; i++) { //border
borderArray[0][i] = 0; //populate top row
borderArray[borderHeight - 1][i] = 0; //populate bottom row
if (i == 0 || i == borderWidth - 1) { //populate left and right columns
for (j = 1; j < borderHeight - 1; j++) {
borderArray[j][i] = 0;
}
}
}
for (int i = 0; i < original.length; i++) { //populate middle with original
System.arraycopy(original[i], 0, borderArray[i+1], 1, original[].length);
}

How do I print the 0s and 1s automatically in the 2D array and count the 1s and 0s in it?

So far, my code is this one:
import java.util.Scanner;
public class PG1 {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter the length of matrix: ");
//array indicates
int i = input.nextInt();
int j = i;
//declaration,creation, initialization
double [][] matrix = new double [i][j];
//print element in row i
for (i = 0; i < matrix.length;i++){
//print element j in row i
for (j = 0; j < matrix[i].length; j++) {
System.out.print("The matrix is: " + matrix[i][j]);
}
System.out.println();
}
}
}
So basically, I want to print the 0s and 1s according to the user's input or row and column of the matrix. Your help will be much appreciated.
output:
Enter the length of the matrix: 4
The matrix:
0 1 1 1
0 0 0 0
0 1 0 0
1 1 1 1
All 0s on row 1
All 1s on row 3
No same numbers on a column
No same numbers on the diagona
you can achive like this:
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter the length of matrix: ");
//array indicates
int i = input.nextInt();
int j = i;
int count0s = 0;
int count1s = 0;
//declaration,creation,initialisation
double [][] matrix = new double [i][j];
//print element in row i
for ( i = 0; i < matrix.length;i++){
//print element j in row i
for ( j = 0; j < matrix[i].length; j++){
if(matrix[i][j] == 0){
count0s++
System.out.print("The matrix is: " + matrix[i][j]);
}
else if(matrix[i][j] == 1){
count1s++
System.out.print("The matrix is: " + matrix[i][j]);
}
}
System.out.println("Total no. of 0s="+count0s);
System.out.println("Total no. of 1s="+count1s);
}
}
}
If you dont want to print 0s and 1s :
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.print("Enter the length of matrix: ");
//array indicates
int i = input.nextInt();
int j = i;
int count0s = 0;
int count1s = 0;
//declaration,creation,initialisation
double [][] matrix = new double [i][j];
//print element in row i
for ( i = 0; i < matrix.length;i++){
//print element j in row i
for ( j = 0; j < matrix[i].length; j++){
if(matrix[i][j] == 0)
count0s++
else if(matrix[i][j] == 1)
count1s++
}
}
System.out.println("Total no. of 0s="+count0s);
System.out.println("Total no. of 1s="+count1s);
}
}

Categories