Printing the even numbers in the columns of 2D array - java

hello guys I am struggling to find the right way to print only the even numbers of the array.
I made a 1 dimensionnal array to save the element of the columns and then make sure if the index of the col%2==0 put that number in the output.
import java.util.Scanner;
public class Matrix {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//array with 3 row in 5 col
int[][] matrix = new int[3][5];
//int []y = new int[5];
// to impalement th array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
System.out.print("Enter matrix[" + i + "][" + j + "]: ");
matrix[i][j] = input.nextInt();
}
System.out.print("\n");
}
System.out.print("matrix values \n");
// to show up the originally array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.print("\n");
}
//////the new array to display only the even numbers in the col
System.out.print("\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
int[] y = matrix[j];
for (int k = 0; i < y.length; i++) {
if (y[k] % 2 == 0)
System.out.println(y[k]);
}
}
}
}
}
the output doesn't print the new array
matrix values
1 2 3 4 5
6 7 8 9 10
3 2 4 5 9
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at Matrix.main(Matrix.java:43)

Replace i by k here :
for (int j = 0; j < 5; j++) {
int[] y = matrix[j];
for (int k = 0; i < y.length; i++) {
if (y[k] % 2 == 0)
System.out.println(y[k]);
}
}

Related

How can i make this matrix example with jagged array in java

I've this codes:
public class matrixExample {
public static void main(String[] args) {
int m[][] = new int[5][5];
int count = 1;
for (int i=0; i<m.length; i++)
for(int j=0; j<i+1; j++)
m[i][j] = count++;
for (int i=0; i<m.length; i++)
{
for (int j=0; j<m[i].length; j++)
System.out.print(m[i][j] + " ");
System.out.println();
}
}
}
And output:
How can I do as in the screenshot below?
int size = 5;
int[][] m = new int[size][size];
int count = 1;
for (int i = 0; i < size; ++i)
for (int j = size - i - 1; j < size; ++j)
m[i][j] = count++;
You may populate the matrix as suggested in saka1029's answer above and then print it using formatted output:
for (int i = 0; i < m.length; i++) {
for (int j = 0; j < m[i].length; j++) {
System.out.printf(" %2d ", m[i][j]);
}
System.out.print('\n');
}
Or, it is possible to keep "normal" matrix, and change only its output:
public static void main(String []args){
int m[][] = new int[5][5];
int count = 1;
// keeping initial array as is
for (int i=0; i<m.length; i++)
for(int j=0; j<i+1; j++)
m[i][j] = count++;
for (int i = 0; i < m.length; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < m[i].length; j++) {
if (m[i][j] > 0) {
sb.append(String.format(" %2d ", m[i][j]));
} else {
sb.insert(0, String.format(" %2d ", m[i][j]));
}
}
System.out.println(sb.toString());
}
}
Output in both cases is identical:
0 0 0 0 1
0 0 0 2 3
0 0 4 5 6
0 7 8 9 10
11 12 13 14 15

Transposing a 2D Array Only Works For Rectangular Arrays - Java

I have a class with a method that transposes an array given the array, rows, and columns
public class Transpose {
public static void main(String[] args) {
int[][] array = new int[6][5];
System.out.println("Original:");
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
array[i][j] += i+1;
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println();
transpose(array, 6, 5);
}
public static void transpose(int[][] array, int arrayRows, int arrayColumns)
{
int[][] transposedArray = new int[arrayRows][arrayColumns];
System.out.println("Transposed:");
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
transposedArray[i][j] = array[j][i];
System.out.print(transposedArray[i][j] + " ");
}
System.out.println();
}
}
}
The output I get looks like this:
Original:
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
6 6 6 6 6
Transposed:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
I realized that the method only works when the values of arrayRows and arrayColumns are the same value, for example: 6, 6 or 5, 5. I tried to place the values opposite of each other, the rows in columns and vice versa, however it did not work. How can I get the method to work for non-rectangular/square arrays?
See line comments for explanation. Those are the only lines I modified.
public static void transpose(int[][] array, int arrayRows, int arrayColumns) {
int[][] transposedArray = new int[arrayColumns][arrayRows]; //swap number of rows and columns
System.out.println("Transposed:");
for (int i = 0; i < transposedArray.length; i++) { //take the length of transposedArray, not array
for (int j = 0; j < transposedArray[i].length; j++) { //take the length of transposedArray, not array
transposedArray[i][j] = array[j][i];
System.out.print(transposedArray[i][j] + " ");
}
System.out.println();
}
}
You need to swap the places of arrayRows and arrayColumns in the transposed matrix, because the new matrix is supposed to be a [5][6] instead of a [6][5].
So your line of
int[][] transposedArray = new int[arrayRows][arrayColumns];
becomes
int[][] transposedArray = new int[arrayColumns][arrayRows];
We also need to swap i and j in the following statement, because the loops are following the indices of the original matrix:
transposedArray[i][j] = array[j][i];
to
transposedArray[j][i] = array[i][j];
And lastly, you can't print the transposed matrix while you're creating it as you're doing now, because you're just re-printing the original matrix that way. I suggest printing the matrix after you're done creating it.
With these changes, your code ends up like this:
public static void main(String[] args) {
int[][] array = new int[6][5];
System.out.println("Original:");
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
array[i][j] += i+1;
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println();
transpose(array, 6, 5);
}
public static void transpose(int[][] array, int arrayRows, int arrayColumns)
{
int[][] transposedArray = new int[arrayColumns][arrayRows];
System.out.println("Transposed:");
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
transposedArray[j][i] = array[i][j];
}
}
for(int i = 0; i < transposedArray.length; i++) { //print the transposed matrix
for(int j = 0; j < transposedArray[i].length; j++) {
System.out.print(transposedArray[i][j] + " ");
}
System.out.println();
}
}
You can see a working example here.
for(int i = 0; i < array.length; i++)
// should count the number of rows. = 6
iterating from 0 ... 5
for(int j = 0; j < array[i].length; j++) // should count the number of columns = 5
iterate from 0..4
transposedArray[i][j] = array[j][i];
to access 6 ... you need j=5 ; i = 0...4
your loops are not able to access the values

Need some help using Java with matrix

I am studying and have a little trouble.
I have to build matrix like this on the picture
Matrix
But i can't put a stars in that places.
public static void main(String[] args) {
int[][] twoD = new int[5][5];
int i, j, k = 1;
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++) {
twoD[i][j] = k;
k++;
}
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++)
System.out.print(twoD[i][j] + " ");
System.out.print("\n");
}
}
Here is my code, please help to find
You can take the answer from a previous question and slightly modify it like so
public static void printCross(int size, char display)
{
int count = 0; //add a counter
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
count++; //increment each index we come across
if (row == col || row + col == size - 1) {
//print out the X using teh given display character
System.out.print(String.format("%-3s", display));
} else {
//print out the current count
System.out.print(String.format("%-3s", count));
}
}
System.out.println();
}
}
Output
X 2 3 4 X
6 X 8 X 10
11 12 X 14 15
16 X 18 X 20
X 22 23 24 X
I have found the easiest way i think.
public static void main(String args[]){
String[][] Matrix = { {" *"," 2"," 3"," 4"," *"} , {" 6"," *"," 8"," *","10"} , {"11","12"," *","14","15"} , {"16"," *","18"," *","20"} , {" *","22","23","24"," *"}};
for(int i=0; i< Matrix.length; i++){
for(int j=0;j < Matrix.length; j++){
System.out.print(Matrix[i][j]+" ");
}
System.out.println("");
}
}

How to reverse a triangle jagged array?

I have already done with normal jagged array, but I don't understand how to reverse it upside down. Also I have a question, how to shift the side of triangle from left side to right? Can I do this with loops or I need to write different quantity of whitespaces for every line of my array?
static int[][] triangle(int lines){
int[][] arr = new int[lines][];
for(int i = 0; i < arr.length; i++){
arr[i] = new int[i + 1];
}
int count = 0;
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length; j++){
arr[i][j] = count++;
}
}
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length; j++){
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
return arr;
}
Some kind of result:
0
1 2
3 4 5
6 7 8 9
10 11 12 13 14
You can quickly create a reversed triangle by changing the way you initialize your arr array.
static int[][] revTriangle(int lines) {
int[][] arr = new int[lines][];
for (int i = 0; i < arr.length; i++) {
arr[i] = new int[lines - i]; // this line
}
int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j] = count++;
}
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
return arr;
}
I get the following output:
0 1 2 3 4
5 6 7 8
9 10 11
12 13
14
Hope this helps!
If you need a more compact solution you can group some loops (basing on anacron answer):
static int[][] triangle( int lines, boolean straight)
{
int[][] arr = new int[lines][];
int count = 0;
for ( int i = 0; i < arr.length; i++ )
{
int start = (straight? i : lines);
int step = (straight? 1 : -i);
arr[i] = new int[start + step ];
for ( int j = 0; j < arr[i].length; j++ )
{
arr[i][j] = count++;
System.out.print( arr[i][j] + " " );
}
System.out.println();
}
return arr;
}

Two Dimensional Array not printing values in matrix format [duplicate]

This question already has an answer here:
how to system.out.println on the same line(java) [duplicate]
(1 answer)
Closed 6 years ago.
I am trying to work on some 2 dimensional array examples. While I was trying to print the 2 dimensional array output in the following format
0 1 2
3 4 5
6 7 8
9 10 11
12 13 14
My output was showing up as like this
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Not exactly sure what's seems to be the problem
Here is my code:
public class TwoDArray {
public static void main(String[] args) {
int rows = 5;
int columns = 3;
int k = 0;
int[][] array = new int[rows][columns];
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++) {
array[i][j] = k;
k++;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.println(array[i][j] + " ");
}
System.out.println();
}
}
}
You have used :
System.out.println(number)
This will create a new line at the end of each number printed. To see output as you want it, you should use :
System.out.print(number)
CODE:
public class TwoDArray {
public static void main(String[] args) {
int rows = 5;
int columns = 3;
int k = 0;
int[][] array = new int[rows][columns];
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++) {
array[i][j] = k;
k++;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}
You are printing new line every time you print an element.
Use this code. It should solve your problem:
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}

Categories

Resources