I have to make 2D array [10][10] to be filled like this:
(https://i.stack.imgur.com/WCRGu.png)
Please, can someone help me with this?
`
int j, i;
int n=1;
for (j=9; j>=0;j--) {
for (i=11-j; i>=9-j; i++) {
if(i<10) {
a[i][j]=n;
n++;
}else{
break;
}
}
}
for (i=0;i<=9;i++) {
for(j=0;j<=9;j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
`
Here the program for your problem. I've also added the printing of the 2d array. It accepts any 1:1 size of the array, you can modify the size of your wanting using the variable final int ARRAY_SIZE.
public class StackOverflow {
public static void main(String[] args) {
final int ARRAY_SIZE = 10;
int[][] array = new int[ARRAY_SIZE][ARRAY_SIZE];
//filling of values for 2d array
for (int i = 0, k = 1; i < array.length ; i++) {
for (int j = i + 3; j > i; j--, k++) {
if (j > array.length) {
k--;
continue;
}
array[j - 1][array.length - i - 1] = k;
}
}
//printing of 2d array
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
if (array[i][j] == 0)
System.out.print(array[i][j] + " ");
else if (array[i][j] > 9)
System.out.print(array[i][j] + " ");
else
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}
Here is my current code:
private static int primeFinder(int max) {
ArrayList<Integer> nums = new ArrayList<Integer>();
for (int i = 0; i < max; i++) {
nums.add(i + 1);
}
for (int currentLoc = 0; currentLoc < nums.size(); currentLoc++) {
for (int testLoc = 1; testLoc < currentLoc - 1; testLoc++) {
if (nums.get(currentLoc) % nums.get(testLoc) == 0) {
System.out.println(nums.get(currentLoc) + " " + nums.get(testLoc));
nums.remove(currentLoc);
}
}
}
for (int i : nums) {
System.out.print(i + " ");
}
return 0;
}
When I run it between zero and 50, it prints out this:
1 2 3 5 7 11 13 16 17 19 23 27 29 31 35 37 41 43 47 50
Why are there extraneous solutions (16, 27, 35, 50)?
As khelwood explained the inner testLoc loop doesn't exit after it's removed a number, you could implement a break into the loop. An example being the following code:
private static int primeFinder(int max) {
ArrayList<Integer> nums = new ArrayList<Integer>();
for (int i = 0; i < max; i++) {
nums.add(i + 1);
}
for (int currentLoc = 0; currentLoc < nums.size(); currentLoc++) {
boolean removed = false;
for (int testLoc = 1; testLoc < currentLoc - 1 && !removed; testLoc++) {
if (nums.get(currentLoc) % nums.get(testLoc) == 0) {
System.out.println(nums.get(currentLoc) + " " + nums.get(testLoc));
nums.remove(currentLoc);
removed = true;
}
}
}
for (int i : nums) {
System.out.print(i + " ");
}
return 0;
}
However, I believe a better way would be to avoid using .remove() all together. Such as the following:
private static int primeFinder(int max) {
ArrayList<Integer> primes = new ArrayList<Integer>();
for (int i= 0; i< max; i++) {
boolean prime = true;
for (int primeLoc= 1; primeLoc < i - 1; primeLoc++) {
if (i % primes.get(testLoc) == 0) {
prime = false;
}
}
if(prime) {
primes.add(i);
}
}
for (int i : primes) {
System.out.print(i + " ");
}
return 0;
}
Although unrelated to the question, I don't see any point of having the function return an integer.
Also storing the prime numbers as ArrayList<Integer> is not the best method.
I am using this function to generate and check prime numbers with a BitSet. This is one of the fastest java methods I know.
import java.util.*;
public class PrimeNumbersSieve{
public static void main(String []args){
System.out.println(computePrimes(50));
}
public static BitSet computePrimes(int limit) {
final BitSet primes = new BitSet(limit);
primes.clear(0);
primes.clear(1);
primes.set(2, limit);
for (int d = 2; d < Math.sqrt(limit); d++) {
if (primes.get(d)) {
for (int m = d * d; m < limit; m += d) {
primes.clear(m);
}
}
}
return primes;
}
}
I am trying to make a program that outputs
So far I have done:
public class printPattern {
public static void main(String[] args) {
int a = 6;
int i, j;
int max = 1;
int num;
for(i = 1; i <= a; i++){
num = 1;
System.out.println("0");
for(j = 1; j <= max; j++){
System.out.print(num);
System.out.print(" ");
num++;
}
max++;
}
}
}
But the output I am getting is
The "0" is there to show the spaces, but I want to remove the entire line which contains the first "0" so that the output starts with a "1". I am unsure what to change. Any help would be much appreciated. Thank You.
I suggest adding conditions (if we need to print out delimiters):
for (int i = 1; i <= a; ++i) {
if (i > 1)
System.out.println(); // more than 1 line, need delimiter (new line)
for (int j = 1; j <= i; ++j) {
if (j > 1)
System.out.print(" "); // more than 1 column, need delimiter (space)
System.out.print(j);
}
}
Most shortest form:
String str = "";
for (int i = 1; i <= 6; i++) {
str = str + " " + i;
System.out.println(str);
}
Output:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
Here what I've got
Here you can check https://code.sololearn.com/c9ALHSGAa6ZZ
class printPattern {
public static void main(String[ ] args) {
int a = 6;
int i, j;
int max = 1;
int num;
for (i = 1; i <= a; i++) {
num = 1;
for (j = 1; j <= max; j++) {
System.out.print(num);
System.out.print(" ");
num++;
}
System.out.println();
max++;
}
}
}
public class printPattern {
public static void main(String[] args) {
int a = 6;
int i, j;
int max = 1;
int num;
for(i = 1; i <= a; i++){
num = 1;
for(j = 1; j <= max; j++){
System.out.print(num);
System.out.print(" ");
num++;
}
System.out.println(" ");
max++;
}
}
}
this is working as you asked. just remove 0 from print statement
How about this ?
public void pyramid(int size) {
for(int i = 1; i <= size; i++) {
for(int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println("");
}
}
I need to use recursion that takes in array of integers and has the even integers appear before the odd integers i.e an array of [1,2,3,4,5] should look like [2,4,1,3,5]. Please note that the integers do not need to be in any specific order just even before odd. Here is what I have so far, any help would be appreciated!
public class EBOMain {
static int[] Array = { 1, 2, 3, 4, 5, 6};
static int n;
public static void main(String[] args) {
System.out.print("Array before sort: " );
for (int i = 0; i < Array.length; i++)
System.out.print(Array[i] +" ");
n = Array.length;
rearrange(Array, n);
System.out.print("\nArray after sort: " );
for (int i = 0; i < Array.length; i++)
System.out.print(Array[i] +" ");
}
public static void rearrange(int []Array,int n){
if (n==0)
return;
else if(Array[n-1]%2==0) {
for(int i=0;i<n-1;i++) {
if(Array[i]%2!=0) {
int temp = Array[i];
Array[i]= Array[n-1];
Array[n-1] = temp;
rearrange(Array,n-1);
}
}
}
else
rearrange(Array,n-1);
}
}
You forgot to initialize n with the length of the array in the first call to rearrange. See this corrected code. Make sure you use Java code conventions; I renamed the Arrayvariable to array.
public class EBOMain {
static int[] array = { 1, 2, 3, 4, 5, 6 };
public static void main(String[] args) {
System.out.print("array before sort: ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
rearrange(array, array.length); // <-- Change is here
System.out.print("\narray after sort: ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
public static void rearrange(int[] array, int n) {
if (n == 0) {
return;
} else if (array[n - 1] % 2 == 0) {
for (int i = 0; i < n - 1; i++) {
if (array[i] % 2 != 0) {
int temp = array[i];
array[i] = array[n - 1];
array[n - 1] = temp;
rearrange(array, n - 1);
}
}
} else {
rearrange(array, n - 1);
}
}
}
Output:
array before sort: 1 2 3 4 5 6
array after sort: 6 2 4 1 3 5
Suppose I have an array
A[][] = {{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9,10,11,12}};
And I want to print a wave so that the output comes out like this
{1,5,9,10,6,2,3,7,11,12,8,4}
How can I do this??
here is my code but it is giving me ArrayIndexOutOfBound
public class Wave1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int [][] a={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}} ;
System.out.println("Row "+a.length);
for(int i=0;i<a.length;i++){
System.out.println("Column "+i+"th "+a[i].length);
}
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
System.out.print(a[i][j]+" ");
}
}
System.out.println();
for(int i=0;i<a.length+1;i++){
if(i%2==0){
for(int j=0;j<a.length;j++){
System.out.print(a[j][i]+" ");
}
}
else{
for(int j=a.length-1;j>=0;j--){
System.out.print(a[j][i]+" ");
}
}
}
Thanks in advance
It seems what you want is to print each column of the matrix, where the even indexed columns are printed in ascending order and the odd indexed columns are printed in descending order.
for (int col = 0; col < a[0].length; col++) {
if (col % 2 == 0) {
for (int row = 0; row < a.length; row++)
System.out.print(a[row][col] + " ");
System.out.println();
} else {
for (int row = a.length - 1; row >= 0; row--)
System.out.print(a[row][col] + " ");
System.out.println();
}
}
Output :
1 5 9 13 17
18 14 10 6 2
3 7 11 15 19
20 16 12 8 4
You can do it easily by taking two for loops, one for forward scanning and another for backward scanning.
Here is the code snippet:
public static void main (String[] args)
{
int [][] a={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}};
for(int k = 0;k < 4;k++) {
for(int i = 0;i < a.length;i++) {
System.out.print(a[i][k] + " ");
}
k++;
for(int i = a.length - 1;i >=0 ;i--) {
System.out.print(a[i][k] + " ");
}
}
}
Output:
1 5 9 13 17 18 14 10 6 2 3 7 11 15 19 20 16 12 8 4
That's like traversing an array similiar to traversing a tree in zigzag traversal, but in this case we do not need stacks just modular arithmetich will be enough, here is your solution;
Method #1 - dummyPrint
private static void dummyPrint(int[][] array) {
System.out.print("Dummy Print: ");
for(int j = 0; j < array[0].length; j++) {
if(j%2 == 0) {
for(int i = 0; i < array.length; i++)
System.out.printf("%2d ", array[i][j]);
} else {
for(int i = array.length-1; i >= 0; i--)
System.out.printf("%2d ", array[i][j]);
}
}
System.out.println();
}
Method #2 - prettyPrint
private static void prettyPrint(int[][] array) {
System.out.println("Pretty Print;");
System.out.println("*************");
for(int j = 0; j < array[0].length; j++) {
if(j%2 == 0) {
for(int i = 0; i < array.length; i++)
System.out.printf("%2d ", array[i][j]);
} else {
for(int i = array.length-1; i >= 0; i--)
System.out.printf("%2d ", array[i][j]);
}
System.out.println();
}
}
Demonstration Code
public class ArrayDemo {
public static void main(String[] args) {
int [][] array = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
dummyPrint(array);
System.out.println();
prettyPrint(array);
}
private static void dummyPrint(int[][] array) {
System.out.print("Dummy Print: ");
for(int j = 0; j < array[0].length; j++) {
if(j%2 == 0) {
for(int i = 0; i < array.length; i++)
System.out.printf("%2d ", array[i][j]);
} else {
for(int i = array.length-1; i >= 0; i--)
System.out.printf("%2d ", array[i][j]);
}
}
System.out.println();
}
private static void prettyPrint(int[][] array) {
System.out.println("Pretty Print;");
System.out.println("*************");
for(int j = 0; j < array[0].length; j++) {
if(j%2 == 0) {
for(int i = 0; i < array.length; i++)
System.out.printf("%2d ", array[i][j]);
} else {
for(int i = array.length-1; i >= 0; i--)
System.out.printf("%2d ", array[i][j]);
}
System.out.println();
}
}
}
The Output
Dummy Print: 1 5 9 10 6 2 3 7 11 12 8 4
Pretty Print;
*************
1 5 9
10 6 2
3 7 11
12 8 4
There is a discrepancy with the code you print and the desired output. In your code, there is a line that prints "row" and another that prints "column".
This code
public class Wave1 {
public static void main(String[] args) {
int [][] a={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16},{17,18,19,20}} ;
String separator="";
System.out.print("{");
for (int[] row: a) {
for (int cell: row) {
System.out.print(separator+cell);
separator=",";
}
}
System.out.println("}");
}
}
prints
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}
I was not able to figure out the order in which the numbers should be printed. I assumed it did not matter.
public class Wave1 {
public static void main(String args[])
{
int [][] wave={{1,2,3,4},{5,6,7,8},{8,9,10,11},{12,13,14,15}};
for(int i=0;i<wave.length;i++){
if(i%2==0){
for(int j=0;j<wave[0].length;j++){
System.out.print(wave[i][j]+" ");
}
System.out.println();
}
else{
for(int j=wave[0].length-1;j>=0;j--){
System.out.print(wave[i][j]+" ");
}
System.out.println();
}
}
}
}
public static void wave(int[][]input)
{
int row=0;
int col=0;
int steps=1;
System.out.print(input[0][0]+" ");
while(steps<=input.length*(input[0].length)-1)
{
if(col%2==1)
{
row--;
}
else
{
row++;
}
if(row<0)
{
row++;
if(col%2==1)
{
col++;
}
}
if(row>input.length-1)
{
row--;
if(col%2==0)
{
col++;
}
}
System.out.print(input[row][col]+" ");
steps++;
}
}
Check this one:
Use this function it will take 2D Array as its input and would print a zigzag Path
public static void wavePrint(int input[][]){
int count = 1;
int rows = input.length;
int cols = input[0].length;
for(int i = 0; i < cols; i++){
if(count % 2 != 0){ //odd column
int sine_e = 0;
while(sine_e < rows){
System.out.print(input[sine_e][i]+" ");
sine_e++;
}
count++;
}else{
int sine_e = rows - 1; //even column
while(sine_e >= 0){
System.out.print(input[sine_e][i]+" ");
sine_e--;
}
count++;
}
}//end of for loop
}