Numbering Array items numerically - java

Alright, so I tried implementing the bubble sort algorithm into my code, but now my output for the second array (in my code) is giving me a ton of zeros. Can anybody tell me what is wrong with my code and how I can fix it so the zeros are removed and the only thing that remains in the output for my second array are the fixed numerically?
public static void main(String[] args) {
System.out.println("Input up to '10' numbers for current array: ");
int[] array1 = new int[10];
int i;
Scanner scan = new Scanner(System.in);
for (i = 0; i < 10; i++) {
System.out.println("Input a number for " + (i + 1) + ": ");
int input = scan.nextInt();
if (input == -9000) {
break;
} else {
array1[i] = input;
}
}
System.out.println("\n" + "Original Array: ");
for (int j = 0; j < i; j++) {
System.out.println((j + 1) + ": " + array1[j]);
}
int[] array2 = new int[i];
System.out.println("\n" + "Organized Array: ");
for (int j = 0; j < i; j++) {
int temp;
boolean organized = false;
while (organized == false) {
organized = true;
for (i = 0; i < array1.length - 1; i++) {
if (array1[i] > array1[i + 1]) {
temp = array1[i + 1];
array1[i + 1] = array1[i];
array1[i] = temp;
organized = false;
}
}
}
for (i = 0; i < array1.length; i++) {
System.out.println(array1[i]);
}
scan.close();
}
}
}

Copy your array1 to an array2 of the correct length before sorting, something like
for (i = 0; i < 10; i++) {
System.out.println("Input a number for " + (i + 1) + ": ");
int input = scan.nextInt();
if (input == -9000) {
break;
}
array1[i] = input;
}
int[] array2 = Arrays.copyOfRange(array1, 0, i);
System.out.println("Before sorting: " + Arrays.toString(array2));
Arrays.sort(array2); // <-- How I would sort.
System.out.println("After sorting: " + Arrays.toString(array2));
The reason this is necessary is because i might not be 10 in which case your array contains 0(s) to fill the other positions.
Is it possible to move all my numbers from Array 1 to Array 2 using a for-loop?
Yes. You could implement a copyOfRange function with a for loop,
private static int[] copyOfRange(int[] arr, int start, int end) {
int pos = 0;
int[] out = new int[end - start];
for (int i = start; i < end; i++) {
out[pos] = arr[i];
pos++;
}
return out;
}
the built-in version is almost certainly better.

1) You are printing the array multiple times, I think you might be giving 0 as input and thats the reason you are seeing 0's everywhere.
2) You have created array2 which is not necessary.
Move the printing logic out of for loop as in the below snippet. Otherwise your logic looks fine except fot the wrong looping of print statement.
public static void main(String args[]) {
System.out.println("Input up to '10' numbers for current array: ");
int[] array1 = new int[10];
int i;
Scanner scan = new Scanner(System.in);
for (i = 0; i < 10; i++) {
System.out.println("Input a number for " + (i + 1) + ": ");
int input = scan.nextInt();
if (input == -9000) {
break;
} else {
array1[i] = input;
}
}
System.out.println("\n" + "Original Array: ");
for (int j = 0; j < i; j++) {
System.out.println((j + 1) + ": " + array1[j]);
}
int[] array2 = new int[i];
System.out.println("\n" + "Organized Array: ");
for (int j = 0; j < i; j++) {
int temp;
boolean organized = false;
while (organized == false) {
organized = true;
for (i = 0; i < array1.length - 1; i++) {
if (array1[i] > array1[i + 1]) {
temp = array1[i + 1];
array1[i + 1] = array1[i];
array1[i] = temp;
organized = false;
}
}
}
scan.close();
}
for (i = 0; i < array1.length; i++) {
System.out.println(array1[i]);
}
}

Related

Java, 2D array specified organization

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();
}
}
}

How to get one result out of the for statement?

import java.util.*;
public class Combination {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
String input = sc.next();
System.out.printf("You entered: %d\n", Integer.parseInt(input));
sc.close();
int[] arr = new int[100];
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}
for (int i = 0; i <= 97; i++) {
int num_i = arr[i];
for (int j = i + 1; j <= 98; j++) {
int num_j = arr[j];
for (int k = j + 1; k <= 99; k++) {
int num_k = arr[k];
if (num_i + num_j + num_k == Integer.parseInt(input))
System.out.printf("(%d, %d, %d)", num_i, num_j, num_k);
}
}
}
}
}
When I get a number input, I want to make a code that represents this number as the sum of three numbers.
The code is complete, but there are several combinations. I want to print out only one combination. How can I edit it?
First, some important suggestions:
Do not parse input inside the nested loop as it will hit the performance. Do it once outside the nested loops.
Do not close Sacnner for System.in as it also closes System.in and there is no way to open it again without restarting JVM. It means that if it is being used in some other part of your application, your application will crash.
Always follow Java naming conventions e.g. you could name numJ instead of num_j.
Coming back to your problem, there are many ways to solve it and I have listed below just a couple of them:
Use break <<label>> to exit the nested loops:
import java.util.Scanner;
public class Combination {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
String input = sc.next();
System.out.printf("You entered: %d\n", Integer.parseInt(input));
int num = Integer.parseInt(input);
int[] arr = new int[100];
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}
start: for (int i = 0; i <= 97; i++) {
int numI = arr[i];
for (int j = i + 1; j <= 98; j++) {
int numJ = arr[j];
for (int k = j + 1; k <= 99; k++) {
int numK = arr[k];
if (numI + numJ + numK == num) {
System.out.printf("(%d, %d, %d)", numI, numJ, numK);
break start;
}
}
}
}
}
}
A sample run:
Enter a number : 123
You entered: 123
(1, 22, 100)
Put the logic in a method and return:
import java.util.Scanner;
public class Combination {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
String input = sc.next();
System.out.printf("You entered: %d\n", Integer.parseInt(input));
int num = Integer.parseInt(input);
printFirstCombination(num);
}
static void printFirstCombination(int num) {
int[] arr = new int[100];
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}
for (int i = 0; i <= 97; i++) {
int numI = arr[i];
for (int j = i + 1; j <= 98; j++) {
int numJ = arr[j];
for (int k = j + 1; k <= 99; k++) {
int numK = arr[k];
if (numI + numJ + numK == num) {
System.out.printf("(%d, %d, %d)", numI, numJ, numK);
return;
}
}
}
}
}
}
You can create a seperate function for that and after you find a combination, print it and return there and then to the main function. In case you didn't find a combination you return 1 which can be handled in the main function,
public class Combination {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number : ");
String input = sc.next();
System.out.printf("You entered: %d\n", Integer.parseInt(input));
int res = printCombination(input);
if(res == 1) {
// Do something
}
sc.close();
}
private static int printCombination(String input) {
int[] arr = new int[100];
for (int i = 0; i < arr.length; i++) {
arr[i] = i + 1;
}
for (int i = 0; i <= 97; i++) {
int num_i = arr[i];
for (int j = i + 1; j <= 98; j++) {
int num_j = arr[j];
for (int k = j + 1; k <= 99; k++) {
int num_k = arr[k];
if (num_i + num_j + num_k == Integer.parseInt(input)) {
System.out.printf("(%d, %d, %d)", num_i, num_j, num_k);
return 0;
}
}
}
}
return 1;
}
}

why do I get -1 when i try to delete an element in an array

I checked out the questions that were already posted, but I still couldn't find a solution.
My output for the code is:
Enter the number of integers: 5
Enter 5 integers: 1
2
3
4
5
Enter the number to be deleted: 2
-1
package array;
import java.util.*;
//import java.util.ArrayLists;
public class DeleteFromArray {
public static void main(String[] args) {
int n = 0; // number of integers
int d = 0; // the number to be deleted
int count = 0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number of integers: ");
n = scan.nextInt();
if (n <= 0) {
System.out.println("Invalid input");
System.exit(-1);
}
int[] buffer = new int[n];
System.out.print("Enter " + n + " integers: ");
for (int k = 0; k < buffer.length; k++) {
buffer[k] = scan.nextInt();
}
System.out.print("Enter the number to be deleted: ");
d = scan.nextInt();
for (int i = 0; i < buffer.length; i++) {
if (buffer[i] == d) {
for (int j = 0; j < (buffer.length) - 1; j++) {
buffer[j] = buffer[j + 1];
}
count++;
break;
}
}
if(count ==0) {
System.out.println("Element not found!");
}
else {
System.out.print("Element Deleted Successfully..!!");
System.out.print("\nNow the New Array is :\n");
for (int i = 0; i < (buffer.length)-1; i++) {
System.out.println(buffer[i]+ " ");
}
}
scan.close();
}
}
Your for loop
for (int j = 0; j < (buffer.length) - 1; j++) {
buffer[j] = buffer[j + 1];
}
will not work properly because it will replace the value at 0 index with the value at index 1 and so on. What you want to do is just intialize the j=i where i is the index of d. and it will replace this value with the next.
for (int j = i; j < (buffer.length) - 1; j++) {
buffer[j] = buffer[j + 1];
}
Try this loop it will work.

Java: Removing duplicates from a user inputted integer array

Im trying to remove duplicates from a user inputted array using java and am getting an error for a duplicate variable, here is what i have so far:
public class sortedArray {
static int alter(int array[], int n) {
if (n == 0 || n == 1)
return n;
int[] arr = new int[n];
int r = 0;
for (int i = 0; i < n - 1; i++)
if (array[i] != array[i + 1])
arr[r++] = array[i];
arr[r++] = array[n - 1];
for (int i = 0; i < r; i++)
array[i] = arr[i];
return r;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] array = new int[49];
int n = array.length;
System.out.print("enter some integers (enter -9999 to stop): ");
for (int i = 0; i < array.length; i++) {
array[i] = input.nextInt();
if (array[i] == -9999) {
break;
}
n = alter(array, n);
for (int i = 0; i < n; i++) //getting error here on the i
System.out.print(array[i] + " ");
}
}
}
Any help would be appreciated.
Your problem is that you are trying to redeclare i inside the first for loop. You need to use another counter variable in your second loop:
for (int i = 0; i < array.length; i++) {
array[i] = input.nextInt();
if (array[i] == -9999) {
break;
}
n = alter(array, n);
for (int j = 0; j < n; j++) //getting error here on the i
System.out.print(array[j] + " ");
}

Already sorted array

I have an array including user's inputs. The program is about Bubble sort, Selection sort and Insertion sort. First Bubble, Second Selection and then Insertion sort comes.
I couldn't manage to solve a problem. When the code run into selection sort, the array is already sorted by bubble sort.
I tried to make 2 temporary arrays at first to use the "source array" at selection and insertion sorting but those arrays re-arranged by bubble sort again. ( Which I don't understand why )
Is there any way to sort my array seperately or I have to make them methods ? I'm counting the swaps and comparisons also BTW. Thanks !
System.out.println("• Please enter the number of elements in the Sorting Bag:");
length = input.nextInt();
System.out.println("• The number of elements: " + length);
int[] SorBag = new int[length];
int[] SorBag2 = new int[length];
int[] SorBag3 = new int[length];
System.out.println("• Please enter the elements of Sorting Bag:");
for (int i = 0; i < SorBag.length ; i++) {
SorBag[i] = input.nextInt();
}
SorBag2 = SorBag;
SorBag3 = SorBag;
System.out.print("• Elements in the Sorting Bag are:");
for (int j = 0; j < SorBag.length; j++) {
System.out.print(" " + SorBag[j]);
}
System.out.println("");
System.out.println("");
//Bubble Sort
for (int i = 1; i < SorBag.length; i++) {
for (int j = 0; j < SorBag.length - i; j++) {
BComparison++;
if (SorBag[j] > SorBag[j + 1]) {
BSwaps++;
temp1 = SorBag[j + 1];
SorBag[j + 1] = SorBag[j];
SorBag[j] = temp1;
}
}
}
System.out.print("• Bubble Sort:");
for (int k = 0; k < SorBag.length; k++) {
System.out.print(" " + SorBag[k] + " ");
}
System.out.print("Comparisons: " + BComparison + " Swaps: " + BSwaps);
System.out.println(" ");
//Selection Sort
for (int i = 0; i < SorBag2.length; i++) {
min = i;
for (int j = i + 1; j < SorBag2.length; j++) {
SComparison++;
if (SorBag2[j] < SorBag2[min]) {
min = j;
}
if (min != i) {
temp2 = SorBag2[i];
SorBag2[i] = SorBag2[min];
SorBag2[min] = temp2;
SSwaps++;
}
}
}
System.out.print("• Selection Sort:");
for (int k = 0; k < SorBag2.length; k++) {
System.out.print(" " + SorBag2[k] + " ");
}
System.out.print("Comparisons: " + SComparison + " Swaps: " + SSwaps);
System.out.println(" ");
//Insertion Sort
for (int i = 1; i < SorBag3.length; i++) {
int j = 0;
while (j > i && SorBag3[j] < SorBag3[j - 1]) {
temp3 = SorBag3[j];
SorBag3[j] = SorBag3[j - 1];
SorBag3[j - 1] = temp3;
ISwaps++;
j--;
}
IComparison++;
}
System.out.print("• Insertion Sort:");
for (int k = 0; k < SorBag3.length; k++) {
System.out.print(" " + SorBag3[k] + " ");
}
System.out.print("Comparisons: " + IComparison + " Swaps: " + ISwaps);
System.out.println(" ");
}
}
SorBag2 = SorBag and SorBag3 = SorBag copies the reference of SorBag to the other two arrays, instead of only copying the data. So instead of:
System.out.println("• Please enter the elements of Sorting Bag:");
for (int i = 0; i < SorBag.length ; i++) {
SorBag[i] = input.nextInt();
}
SorBag2 = SorBag;
SorBag3 = SorBag;
Try this:
System.out.println("• Please enter the elements of Sorting Bag:");
for (int i = 0; i < SorBag.length ; i++) {
int nextInt = intput.nextInt();
SorBag[i] = nextInt;
SorBag2[i] = nextInt;
SorBag3[i] = nextInt;
}

Categories