Freeman Chain Code in Java - java
Okay, hopefully this makes more sense. I have an array hard coded with only 1s and 0s. I am trying to write a function that reads each element to see if it is a 0 or 1. If it is a 1, it will execute another function and then change that 1 to a 0 so that it is not read again. I have it printing simply as a placeholder for the other function I will be implementing later. I'm having trouble getting the findfirst1 function to check every element in the array. I have tried putting the incrementors for i and k in different places within the flow of the code but nothing I have tried gets me the correct output.
public static void main(String[] args)
{
int[][] testarray = {{1,0,0,0,0,0,0,1},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{1,0,0,0,0,0,0,1}};
findfirst1(testarray);
}
public static void findfirst1(int[][] array1)
{
int value = 0;
for(int i = 0; i < 6;i++)
{
for(int k = 0; k < 7;k++)
{
value = array1[i][k];
if(value == 1)
{
System.out.println(value);
array1[i][k] = 0;
}
else
{
System.out.println(value);
}
}
}
}
Okay, so after starting completely over and writing it all from scratch I figured it out. The array.length was right all along. I had trouble wrapping my head around it because I was so focused on the idea of the "image".
Edit: I just found an error where it wouldn't print the last line for array1, so I just added an extra row of 0s and it worked.
public class ChainCodeClass {
public static void main(String[] args)
{
int[][] array1 = {{0,1,0,0,0,0,0,0},{0,1,0,0,0,1,0,0},{0,1,1,1,0,0,1,0},
{0,0,0,1,1,0,0,1},{0,0,0,0,1,0,0,1},{0,0,0,0,0,1,0,0},
{0,0,0,0,0,0,1,0}**,{0,0,0,0,0,0,0,0}**};
int[][] array2 = {{0,0,0,0,0,0,0,0},{0,1,1,1,1,0,1,0},{0,0,0,0,1,0,1,0},
{0,0,1,1,1,0,1,0},{0,0,1,0,0,0,1,0},{0,0,1,0,0,0,1,0},{0,0,0,0,0,0,1,1},
{0,0,0,0,0,0,0,0}};
System.out.print("First Image");
print(array1);
findfirst1(array1);
print(array1);
System.out.print("Second Image");
print(array2);
outline8(array2);
}
public static void findfirst1(int[][] array)
{
int value = 0;
for(int i = 0; i < array.length; i++)
{
for(int k = 0; k < array.length; k++)
{
value = array[i][k];
if(value == 1)
{
System.out.print(value + " ");
array[i][k] = 0;
}
else
{
System.out.println(value);
}
}
}
}
public static void print(int[][] array)
{
for(int i = 0; i < array.length; i++) // print function for the array using incrementors
{
System.out.print("\n");
for(int k = 0; k < array.length; k++)
{
System.out.print(array[i][k] + " ");
}
}
System.out.println();
}
}
Related
Counting Sort implementation
Hello I am having difficulty implementing a counting sort method in java. I believe the problem comes from the last two loops I have in the method. I am getting an ArrayIndexOutOfBounds exception : 8. I believe this comes from my second to last for loop when at index 5 the value is 8 but I am not sure how to resolve this. Any help is appreciated. Thank you! In my code k is the highest value in the input array. Code: public static void main(String[] args) { int [] arrayOne = {0,1,1,3,4,5,3,0}; int [] output = Arrays.copyOf(arrayOne, arrayOne.length); System.out.println(Arrays.toString(arrayOne)); countingSort(arrayOne, output, 5); System.out.println(Arrays.toString(output)); } public static void countingSort(int[] input, int[] output , int k){ int [] temp = Arrays.copyOf(input, k+1); for (int i = 0; i <= k; i++){ temp[i] = 0; } for (int j = 0; j <= input.length - 1; j++){ temp[input[j]] = temp[input[j]] + 1; } for (int i = 1; i <= k; i++){ temp[i] = temp[i] + temp[i-1]; } for (int j = input.length; j >= 1; j--){ output[temp[input[j]]] = input[j]; temp[input[j]] = temp[input[j]] - 1; } }
The problem is in the first loop because the array temp lenght is 6 and you are doing 7 interations in there. So at the end of the for it is trying to do temp[6]=0 and the last position of your array is temp[5]. To fix this change your first loop to: for (int i = 0; i < k; i++){ In the last loop you will get the same exception cause input[8] doesn't exist.
import java.util.Arrays; public class CountingSort { public static void main(String[] args) { int[] input = {0,1,1,3,4,5,3,0}; int[] output = new int[input.length]; int k = 5; // k is the largest number in the input array System.out.println("before sorting:"); System.out.println(Arrays.toString(input)); output = countingSort(input, output, k); System.out.println("after sorting:"); System.out.println(Arrays.toString(output)); } public static int[] countingSort(int[] input, int[] output, int k) { int counter[] = new int[k + 1]; for (int i : input) { counter[i]++; } int ndx = 0; for (int i = 0; i < counter.length; i++) { while (0 < counter[i]) { output[ndx++] = i; counter[i]--; } } return output; } } Above code is adapted from: http://www.java67.com/2017/06/counting-sort-in-java-example.html
this may help but try using the Arraya.sort() method. e.g: //A Java program to sort an array of integers in ascending order. // A sample Java program to sort an array of integers // using Arrays.sort(). It by default sorts in // ascending order import java.util.Arrays; public class SortExample { public static void main(String[] args) { // Our arr contains 8 elements int[] arr = {13, 7, 6, 45, 21, 9, 101, 102}; Arrays.sort(arr); System.out.printf("Modified arr[] : %s", Arrays.toString(arr)); } } example is a snippet from https://www.geeksforgeeks.org/arrays-sort-in-java-with-examples/
As per algorithm following implementation, I have prepared for the count sort technique public static int[] countSort(int elements[]) { int[] sorted = new int[elements.length+1]; int[] range = new int[getMax(elements)+1]; for(int i=0;i<range.length;i++) { range[i] = getCount(i, elements); try { range[i] = range[i]+range[i-1]; }catch(ArrayIndexOutOfBoundsException ae) { continue; } } for(int i=0;i<elements.length;i++) { sorted[range[elements[i]]] = elements[i]; range[elements[i]] = range[elements[i]]-1; } return sorted; } public static int getCount(int value,int[] elements) { int count = 0; for(int element:elements) { if(element==value) count++; } return count; } public static int getMax(int elements[]) { int max = elements[0]; for(int i=0;i<elements.length;i++) { if(max<elements[i]) { max = elements[i]; } } return max; } Please review and let me know if any feedback and it is more helpful. Note : Non-negative no won't support in the above implementation. don't use 0th index of the sorted array.
Tower of Hanoi using 2D arrays issue
I'm working on a Tower of Hanoi project for school which needs to ask the user how many disks there are and then it needs to create and then solve the tower with a visual included. How I decided to do it is by using 2D arrays and for the most part its working, my only problem is that I don't know how to move the disks while keeping it modular. Here is my code. public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("Enter number of disks"); int num = scan.nextInt(); int temp = num-(num-1); int measure = num; //initializing the towers int[][] towers = new int[num][num]; for(int i = 0 ; i < num; i++) { for(int j=0; j <3; j++) { } } createRings(towers, num, temp); moveDisk(towers,num); } // creating the rings private static void createRings (int[][]towers, int num, int temp) { for(int i = 0; i<num; i++) { for(int j=0; j<3;j++) { towers[i][0] = temp; } temp = temp+1; } displayTower(towers, num); } // prints the array for display purposes private static void displayTower (int[][] towers, int num) { for(int i = 0; i<num; i++) { for(int j = 0; j<3; j++) { System.out.print(towers[i][j]+"\t"); } System.out.println(); } } //moves the numbers in the array that represents disks private static void moveDisk(int[][]towers, int num) { System.out.println(); displayTower(towers, num); } Does anyone have any suggestions on what I could do?
I've changed your code a bit to make it more readable for me. I changed the towers array. Now each tower is its own array inside the towers array (makes more sense IMHO). Arrays in Java know their size. So you don't have to pass the length of the array as a parameter to every method. I added a method getHighestIdx() which returns the index before the first 0 value in the array I don't know, how the function moveDisk() was intended to work. I changed the declaration so that it makes sense to me. It now moves a disk from tower i to tower j This should help you to implement the algorithm from the linked question. Here is the changed code: public static void main(String[] args) { int numberOfRings = 6; int[][] towers = new int[3][numberOfRings]; createRings(towers); displayTowers(towers); moveDisk(towers, 0, 2); displayTowers(towers); } private static void createRings(int[][] towers) { for (int j = 0; j < towers[0].length; j++) { towers[0][j] = j + 1; } } private static void displayTowers(int[][] towers) { for (int i = 0; i < towers[0].length; i++) { for (int j = 0; j < towers.length; j++) { System.out.print(towers[j][i] + " "); } System.out.println(""); } } private static void moveDisk(int[][] towers, int fromIdx, int toIdx) { int valToMove = towers[fromIdx][getHighestIdx(towers[fromIdx])]; towers[fromIdx][getHighestIdx(towers[fromIdx])] = 0; towers[toIdx][getHighestIdx(towers[toIdx]) + 1] = valToMove; } private static int getHighestIdx(int[] tower) { int i = 0; while (i < tower.length && tower[i] != 0) { i++; } return i - 1; }
drawing Diamond of numbers with 2D array in java
So I need to make a diamond_look of numbers using 2D array in Java. I got my results but with null before the diamond. For drawNumDiamond(9) I have to get a diamond look that goes until 5 and back. I know I can make it without using array, but I want to learn more about 2D arrays :this is how it should look like and what are my results public class Example1{ private static void drawNumDiamond(int h) { if(h%2 != 0) { int size = h/2 +1; int count = 1; int loop = 1; String[][] dijamant = new String[h][]; for(int row = 0; row < dijamant.length; row++) { dijamant[row] = new String[row+1]; for(int kolona=0; kolona<=row; kolona++) { dijamant[0][0] = "1"; for(int i=0; i< loop;i++) { dijamant[row][kolona]+= count; } } count++; loop+=2; } for (int k = 0; k < size; k++) { System.out.printf("%" + h + "s", dijamant[k]); h++; System.out.println(); } h--; for (int q = size - 2; q>=0; q--) { h--; System.out.printf("%" + h + "s", dijamant[q]); System.out.println(); } } } public static void main(String[] args) { drawNumDiamond(9); } }
The issue is in this line : dijamant[row][kolona] += count; if dijamant[row][kolona] is null and count is 2, the result of the string concatenation will be "null2". Try adding the following if statement before to initialize with an empty string : if (dijamant[row][kolona] == null) { dijamant[row][kolona] = ""; } This will get your code working, but there are still things to think about. E.g. you keep setting dijamant[0][0] = "1"; in the loop.
Make a print method with for loop Java
The task of exercise is to make a method that will work like in Example.( We must use for loop. But if you know how to do it in another way, it will be also very interesting.) Input number can be any. Example: Input: 3 Output: **1** *121* 12321 *121* **1** My example: public static void main(String[] args) { printMatrix(5); } public static void printMatrix (int n) { int d = n +(n-1); for (int i = 0; i < n ; i++) { for(int j = 1; j <=d; j++){ int abs = Math.abs(j-n); System.out.print(abs>i ? "*" : i-abs+1); } System.out.println(""); } My output: ****1**** ***121*** **12321** *1234321* 123454321 I can't make the next step, to turn it upside down. Dose anybody hava an ideas?
I have solved this exercise, but I think it is not a perfect solution. Does anybody have another way to solve this task? public static void main(String[] args) { printMatrix(5); } public static void printMatrix (int n) { int d = n +(n-1); int k = n*2; int g = -1; for (int i = 0; i < d ; i++) { if(i<n){ g++; } else{k=n*2; g--;} for(int j = 1; j <=d; j++){ if (i < n){ int abs = Math.abs(j-n); System.out.print(abs>i ? "*" : i-abs+1); } else{ k--; int abs = Math.abs(k-n); System.out.print(abs>g ? "*" : g-abs+1); } } System.out.println(""); } } } Output: ****1**** ***121*** **12321** *1234321* 123454321 *1234321* **12321** ***121*** ****1****
printing an array in multiple lines
I am taking a programming class, and things just aren't clicking for me. I have an assignment that asks to: Write a program to assign the integer values 1 through 25 to a 25 element integer array. Then, print the array as five separate lines each containing five elements separated by commas. The last element on each line should be followed by a newline instead of a comma. The output of your program should appear exactly as follows: 1,2,3,4,5 6,7,8,9,10 11,12,13,14,15 16,17,18,19,20 21,22,23,24,25 Hints: One way to determine every 5th element is to use the modules operator (%). If you divide the subscript by 5 and the remainder is 0, it is the 5th number. You can use System.out.print() to print a value without a newline following it. This will allow you to print multiple things on the same line. I have a little bit of code but I don't know where to go from here: public class Program4 { public static int[] array; public static void main(String[] args); { int[] numbers = new int [25] for(int i=0; i<25; i++) array[i] = i + 1;} public static void printArray() { for(int i=1; i<=25; i++); { System.out.print(array[i - 1]); if (i % 5 == 0) System.out.printIn(); } } I just have a mental block about programming-can anyone help point me to some helpful examples?
Try this, import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static int[] array; public static void main(String[] args) { array = new int[25]; for(int i=0; i<25; i++) array[i] = i + 1; printArray(); } public static void printArray() { int i; for(i=1; i<=25; i++){ if (i % 5 != 0) System.out.print(array[i-1]+","); else System.out.println(array[i-1]); } } }
public class Foo { public static int[] nums; public static void main(String[] args) { nums = new int[25]; for (int i = 0; i < nums.length; i++) { nums[i] = i + 1; } printArray(nums); } public static void printArray(int[] myArray) { for (int i = 0; i < myArray.length; i++) { System.out.print(String.valueOf(myArray[i]); if (i % 5 == 0) { System.out.println(); } else if (i % 5 != 4){ System.out.println(", "); } } }
public class Test { public static void main(String[] args) { int[] array = new int [25]; for(int i=0; i<25; i++) { array[i] = i + 1; } for (int i=1; i<=25; i++) { System.out.print(array[i - 1]); if (i % 5 == 0) { System.out.println(); } else { System.out.print(", "); } } } } And try to learn Java syntax first of all.
Here is an enhanced version of your code. public class Program4 { public static int[] array = new int[25];//instantiate the array to its default values public static void main(String[] args) { //calling the methods from main addToArray(); printArray(); } //add the numbers to the array public static void addToArray(){ for(int i=0; i<25; i++) array[i] = i + 1; } //print the numbers from the array public static void printArray() { for(int i = 1; i <= 25; i++){ if(i % 5 == 0){ System.out.print(i); System.out.println(); } else{ System.out.print(i + ","); } } } }