Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a question and I will provide my code below. I need to target sequences to be as much as positive or negative members of the array. Can anyone tell me what I need to change in my code to do so?
import java.util.Scanner;
class DynamicArray {
public static void main(String[] args) {
int i,a,b;
System.out.println("Enter the limit of array :" );
Scanner s = new Scanner(System.in);
int limit = s.nextInt();
int [] array1 = new int[limit];//{12,23, -22, 0, 43,545, -4, -55,43, 12,0, -999, -87
System.out.println("Enter the numbers");
for(i=0; i<limit; i++)
{
array1[i] = s.nextInt();
}
int [] arrayPlus = new int[limit];
int [] arrayMinus = new int[limit];
a=b=0;
for (i = 0; i < limit; i++) {
if (array1[i] > 0 || array1[i] == 0) {
arrayPlus[a] = array1[i];
a++;
} else {
arrayMinus[b] = array1[i];
b++;
}
}
System.out.println("Positive array numbers");
for (i = 0; i < a; i++) {
System.out.println(arrayPlus[i]);}
System.out.println("");
System.out.println("Negative array numbers");
for (i = 0; i < b; i++) {
System.out.println(arrayMinus[i]);}
}
}
first of all you can simplify this:
if (array1[i] > 0 || array1[i] == 0) {
to
if (array1[i] >= 0) {
second one remove this variables. there are not necessary:
int i, a, b;
in each for loop define the type of i:
for (int i = 0;
next one you dont know the number of positive and negative numbers so I suggest you to use a List instead of arrays:
change
int [] arrayPlus = new int[limit];
int [] arrayMinus = new int[limit];
to
List<Integer> arrayPlus = new ArrayList<>();
List<Integer> arrayMinus = new ArrayList<>();
add elements with:
arrayPlus.add(array1[i]);
arrayMinus.add(array1[i]);
and read elements with:
System.out.println(arrayPlus.get(i));
System.out.println(arrayMinus.get(i));
iterate over the lists with:
for (int i = 0; i < arrayPlus.size(); i++) {
OVERALL:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class DynamicArray {
public static void main(String[] args) {
System.out.println("Enter the limit of array :");
Scanner s = new Scanner(System.in);
int limit = s.nextInt();
int[] array1 = new int[limit];//{12,23, -22, 0, 43,545, -4, -55,43, 12,0, -999, -87
System.out.println("Enter the numbers");
for (int i = 0; i < limit; i++) {
array1[i] = s.nextInt();
}
List<Integer> arrayPlus = new ArrayList<>();
List<Integer> arrayMinus = new ArrayList<>();
for (int i = 0; i < limit; i++) {
if (array1[i] >= 0) {
arrayPlus.add(array1[i]);
} else {
arrayMinus.add(array1[i]);
}
}
System.out.println("Positive array numbers");
for (int i = 0; i < arrayPlus.size(); i++) {
System.out.println(arrayPlus.get(i));
}
System.out.println("");
System.out.println("Negative array numbers");
for (int i = 0; i < arrayMinus.size(); i++) {
System.out.println(arrayMinus.get(i));
}
}
}
OVERALL(without Lists)
import java.util.Scanner;
class DynamicArray {
public static void main(String[] args) {
int p=0,n=0;
System.out.println("Enter the limit of array :");
Scanner s = new Scanner(System.in);
int limit = s.nextInt();
int[] array1 = new int[limit];//{12,23, -22, 0, 43,545, -4, -55,43, 12,0, -999, -87
System.out.println("Enter the numbers");
for (int i = 0; i < limit; i++) {
int lInput = s.nextInt();
if (lInput >= 0) {
p++;
} else {
n++;
}
array1[i] = lInput;
}
int[] arrayPlus = new int[p];
int[] arrayMinus = new int[n];
int indexPlus = 0;
int indexMinus = 0;
for (int i = 0; i < limit; i++) {
if (array1[i] >= 0) {
arrayPlus[indexPlus] = array1[i];
indexPlus++;
} else {
arrayMinus[indexMinus] = array1[i];
indexMinus++;
}
}
System.out.println("Positive array numbers");
for (int i = 0; i < arrayPlus.length; i++) {
System.out.println(arrayPlus[i]);
}
System.out.println("");
System.out.println("Negative array numbers");
for (int i = 0; i < arrayMinus.length; i++) {
System.out.println(arrayMinus[i]);
}
}
}
Related
I have just started to do programming ...I am trying to sort an array in ascending order.. but not getting desired result , please point where i am doing wrong..
public static void main(String[] args) {
int count, temp;
// User inputs the array size
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of elements you want in the array: ");
count = scan.nextInt();
int num[] = new int[count];
System.out.println("Enter array elements:");
for (int i = 0; i < count; i++) {
num[i] = scan.nextInt();
}
scan.close();
{
int i = 0;
while (i <= count) {
for (int j = i + 1; j < count; j++) {
if (num[i] > num[j]) {
temp = num[i];
num[i] = num[j];
num[j] = temp;
i++;
}
}
}
}
System.out.print("Array Elements in Ascending Order: ");
for (int i = 0; i < count - 1; i++) {
System.out.print(num[i] + ", ");
}
System.out.print(num[count - 1]);
}
you should increment the i outside the for loop or you can get rid of the while loop and use for loop too, you can find the code below :
import java.util.Scanner;
public class Ascending _Order
{
public static void main(String[] args)
{
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++)
{
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Ascending Order:");
for (int i = 0; i < n - 1; i++)
{
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}
Output:
Enter no. of elements you want in array:5
Enter all the elements: 4 3 2 6 1
Ascending Order:1,2,3,4,6
or you can use the method sort :
import java.util.Scanner;
import java.util.Arrays;
public class Ascending_Order {
public static void main(String[] args) {
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
}
Arrays.sort(a);
System.out.print("Ascending Order:");
for (int i = 0; i < n - 1; i++) {
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}
Output:
Enter no. of elements you want in array:5
Enter all the elements: 4 3 2 6 1
Ascending Order:1,2,3,4,6
I have this professor that wants us to make a using the method header
Public static int[] eliminateDuplicates(int[] arr)
The program uses a randomly generated array to see if there are duplications but the user gives a limit on how many indexes's the random array has. Here is what I have but it's not working.
import java.util.Random;
import java.util.Scanner;
public class Dublicates
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Random generator = new Random();
System.out.println("Enter array length: ");
int[] a = new int[input.nextInt()];
for (int i = 0; i<a.length; i++)
{
a[i] = generator.nextInt(a.length*2);
}
int[] result = eliminateDuplicates(a);
System.out.println("The new numbers are: " + result.length);
System.out.println("The double numbers were:");
for (int b : result)
{
System.out.println(b + " ");
}
}
public static int[] eliminateDuplicates(int[] arr)
{
int[] temp = new int[arr.length];
int size = 0;
for (int i = 0; i < arr.length; i++) {
if (linearSearch(temp, arr[i]) == -1) {
temp[size] = arr[i];
size++;
}
int[] result = new int[size];
for (int i = 0; i < size; i++) {
result[i] = temp[i];
}
{
return result;
}
}
}
public static int linearSearch(int[] arr, int key)
{
for(int i = 0; i<arr.length; i++)
{
if (key == arr[i])
return i;
}
return -1;
}
}
As your statements making the final result is inside the for loop, the statements inside for will only run once and will not give the right answer.
So you have to change your code as follows.
public static int[] eliminateDuplicates(int[] arr)
{
int[] temp = new int[arr.length];
int size = 0;
for (int i = 0; i < arr.length; i++) {
if (linearSearch(temp, arr[i]) == -1) {
temp[size] = arr[i];
size++;
}
}
int[] result = new int[size];
for (int i = 0; i < size; i++) {
result[i] = temp[i];
}
return result;
}
The default values in the integer array is 0, The Random.nextInt() can generate 0 random value, When you run linear search then 0 will not be included in the final resultant array.
I have modify Random.nextInt() so that it will not generate 0 random number:
import java.util.Random;
import java.util.Scanner;
public class HelloCodiva
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
Random generator = new Random();
System.out.println("Enter array length: ");
int[] a = new int[input.nextInt()];
for (int i = 0; i<a.length; i++){
a[i] = generator.nextInt(a.length*2)+1;//So that 0 is not generated
}
int[] result = eliminateDuplicates(a);
for (int originalValue: a){
System.out.println(originalValue+ " ");
}
System.out.println("The new numbers are: " + result.length);
System.out.println("The double numbers were:");
for (int b : result){
System.out.println(b + " ");
}
}
public static int[] eliminateDuplicates(int[] arr)
{
int[] temp = new int[arr.length];
int size = 0;
for (int i = 0; i < arr.length; i++) {
if (linearSearch(temp, arr[i]) == -1) {
temp[size] = arr[i];
size++;
}
}
int[] result = new int[size];
System.arraycopy(temp, 0, result, 0, size);
return result;
}
public static int linearSearch(int[] arr, int key)
{
for(int i = 0; i<arr.length; i++)
{
if (key == arr[i])
return i;
}
return -1;
}
}
Constraints:
0≤t≤500
0≤a,b≤50
1≤n≤15
Sample Input:
2
0 2 10
5 3 5
Sample Output
2 6 14 30 62 126 254 510 1022 2046
8 14 26 50 98
This works in my IDE but when I try it in an online editor in hackerrank.com it throws an exception:
Exception in thread "main" java.util.NoSuchElementException: No line foundat
java.util.Scanner.nextLine(Scanner.java:1585)at
Solution.main(Solution.java:24)
Please explain why this happens.Thanks!
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
int[] a = new int[10];
int[] b = new int[10];
int[] n = new int[10];
int t;
int sum;
StringBuilder sb =new StringBuilder();
Scanner iput = new Scanner(System.in);
t = Integer.parseInt(iput.nextLine());
if (t <= 500) {
for (int i = 0; i < t; i++) {
a[i] = Integer.parseInt(iput.next());
b[i] = Integer.parseInt(iput.next());
n[i] = Integer.parseInt(iput.next());
iput.nextLine();
}
} else
System.out.println("Enter value less than 500");
if (t <= 500) {
for (int i = 0; i < t; i++) {
if (a[i] <= 50 && b[i] <= 50 && n[i] <= 15 && n[i] != 0) {
for (int j=0;j<n[i];j++) {
sum = a[i];
for (int k = j;k >=0; k--) {
sum+=Math.pow(2,k)*b[i];
}
sb=sb.append(Integer.toString(sum)).append(" ");
}
System.out.println(sb);
sb.delete(0,sb.toString().length());
} else
System.out.println("Enter the values within the allowed limits");
}
}
}
}
Remove iput.nextLine(); on line 24 so that no extra reading will happen.
I think the below should work. Because value of T can be up to 500 So the array can be either int[] a = new int[t] or new int[500].
Note if you are using int[t] the get the value of t before defining a,b and n.
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
int[] a = new int[500];
int[] b = new int[500];
int[] n = new int[500];
int t;
int sum;
StringBuilder sb =new StringBuilder();
Scanner iput = new Scanner(System.in);
t = Integer.parseInt(iput.nextLine());
if (t <= 500) {
for (int i = 0; i < t; i++) {
a[i] = Integer.parseInt(iput.next());
b[i] = Integer.parseInt(iput.next());
n[i] = Integer.parseInt(iput.next());
}
} else
System.out.println("Enter value less than 500");
if (t <= 500) {
for (int i = 0; i < t; i++) {
if (a[i] <= 50 && b[i] <= 50 && n[i] <= 15 && n[i] != 0) {
for (int j=0;j<n[i];j++) {
sum = a[i];
for (int k = j;k >=0; k--) {
sum+=Math.pow(2,k)*b[i];
}
sb=sb.append(Integer.toString(sum)).append(" ");
}
System.out.println(sb);
sb.delete(0,sb.toString().length());
} else
System.out.println("Enter the values within the allowed limits");
}
}
}
}
import java.util.Scanner;
public class hackerrank_loop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int j = sc.nextInt();
for (int i = 0; i < j; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
int n = sc.nextInt();
int x = 1;
int o = a;
for (int k = 0; k < n; k++) {
o = o + x * b;
System.out.print(o);
x = x << 1;
System.out.print(' ');
}
System.out.println();
}
}
}
I've solved in this way the problem.
For sure it can be done better.
Scanner in = new Scanner(System.in);
int q=in.nextInt();
int[][] app;
if(q<0 && q>500){
System.out.println("ERROR = Select a valid value o q");
}else{
app= new int[q][3];
for(int i=0;i<q;i++){
app[i][0] = in.nextInt();
app[i][1] = in.nextInt();
app[i][2]= in.nextInt();
}
for (int one=0;one<q;one++){
StringBuffer sb = new StringBuffer();
int a = app[one][0];
int b = app[one][1];
int n = app[one][2];
int sum = a;
for (int two=0; two < n; two++){
sum = sum + b*(int)Math.pow(2D,new Double(two));
sb.append(sum);
sb.append(" ");
}
System.out.println(sb);
}
}
in.close();
What is the problem with this BigDecimalSorting? The code takes in numbers as string and then converts it to BigDecimal and then sort and print the sorted BigDecimals.
import java.math.BigDecimal;
import java.util.*;
class Solution {
public static void main(String []argh){
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
String []s=new String[n];
BigDecimal a[] = null;
for(int i = 0; i < n ; i++){
s[i]=sc.next();
a[i] = new BigDecimal(s[i]);
}
for(int i = 0; i < n-1; i++){
for(int j = 1; j < n; j++){
if(a[i].compareTo(a[j]) == -1){
BigDecimal temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
//Output
for(int i=0;i<n;i++){
s[i] = a[i].toString();
System.out.println(s[i]);
}
}
}
Sample Input:
9
-100
50
0
56.6
90
0.12
.12
02.34
000.000
Expected Output:
90
56.6
50
02.34
0.12
.12
0
000.000
-100
You can use the comparator to sort the BigDecimal
import java.math.BigDecimal;
import java.util.*;
class Solution{
public static void main(String []args){
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
String []s=new String[n+2];
for(int i=0;i<n;i++){
s[i]=sc.next();
}
sc.close();
Arrays.sort(s, 0, n, new Comparator<Object>() {
public int compare(Object a1, Object a2) {
BigDecimal bigDec1 = new BigDecimal((String) a1);
BigDecimal bigDec2 = new BigDecimal((String) a2);
return bigDec2.compareTo(bigDec1);
}
});
//Output
for(int i=0;i<n;i++)
{
System.out.println(s[i]);
}
}
import java.math.BigDecimal;
import java.util.*;
class Solution{
public static void main(String []argh)
{
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
String []s=new String[n+2];
for(int i=0;i<n;i++)
{
s[i]=sc.next();
}
for(int i=0;i<n;i++)
{
BigDecimal max=new BigDecimal(s[i]);
int idx=i;
for(int j=i+1;j<n;j++)
{
BigDecimal curr=new BigDecimal(s[j]);
if(curr.compareTo(max)==1)
{
max=curr;
idx=j;
}
}
String temp=s[i];
s[i]=s[idx];
s[idx]=temp;
}
for(int i=0;i<n;i++)
{
System.out.println(s[i]);
}
}
}
Looks like you're throwing an NPE because you're trying to access a null array.
BigDecimal a[] = null; // <---- null array
for (int i = 0; i < n; i++) {
s[i] = sc.next();
a[i] = new BigDecimal(s[i]); // <---- accessing null array a
}
Try initializing your array with the n length used on input
BigDecimal a[] = new BigDecimal[n];
Edit
in response to Mariano's answer, your
if (a[i].compareTo(a[j]) == -1)
is correct, as is. See BigDecimal javadoc
Returns:
-1, 0, or 1 as this BigDecimal is numerically less than, equal to, or greater than val.
You have a couple of errors in your code.
First you are not initializing the a array, BigDecimal a[] = null should be replaced by BigDecimal a[] = new BigDecimal[n] to avoid the NullPointerException.
Finally you are implementing the sort algorithm wrong, you should replace the inner for (int j = 1; j < n; j++) with for (int j = i + 1; j < n; j++)
Here is how your code should look like:
import java.math.BigDecimal;
import java.util.Scanner;
public class Solution {
public static void main(final String[] argh) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] s = new String[n];
BigDecimal a[] = new BigDecimal[n];
for (int i = 0; i < n; i++) {
s[i] = sc.next();
a[i] = new BigDecimal(s[i]);
}
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i].compareTo(a[j]) == -1) {
BigDecimal temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
// Output
for (int i = 0; i < n; i++) {
s[i] = a[i].toString();
System.out.println(s[i]);
}
}
}
This question already has answers here:
Variable length (Dynamic) Arrays in Java
(7 answers)
Closed 7 years ago.
I will post my code and hope someone can tell what to change to make a "dynamic array"? The current code works but they told me it is not the correct way and instead of this I need to make a dynamic array. Thanks in advance.
public static void main(String[] args) {
int i,a,b;
int [] array1 = new int[20];//{12,23, -22, 0, 43,545, -4, -55,43, 12,0, -999, -87
array1[0] = 12;
array1[1] = 23;
array1[2] = -22;
array1[3] = 0;
array1[4] = 43;
array1[5] = 545;
array1[6] = -4;
array1[7] = -55;
array1[8] = 43;
array1[9] = 12;
array1[10] = 0;
array1[11] = -999;
array1[12] = -87;
int [] arrayPlus = new int[20];
int [] arrayMinus = new int[20];
a=b=0;
for (i = 0; i < 13; i++) {
if (array1[i] > 0 || array1[i] == 0) {
arrayPlus[a] = array1[i];
a++;
} else {
arrayMinus[b] = array1[i];
b++;
}
}
System.out.println("Positive array numbers");
for (i = 0; i < a; i++) {
System.out.println(arrayPlus[i]);}
System.out.println("");
System.out.println("Negative array numbers");
for (i = 0; i < b; i++) {
System.out.println(arrayMinus[i]);}
}
}
Please check the below code
import java.util.Scanner;
class DynamicArray {
public static void main(String[] args) {
int i,a,b;
System.out.println("Enter the limit of array :" );
Scanner s = new Scanner(System.in);
int limit = s.nextInt();
int [] array1 = new int[limit];//{12,23, -22, 0, 43,545, -4, -55,43, 12,0, -999, -87
System.out.println("Enter the numbers");
for(i=0; i<limit; i++)
{
array1[i] = s.nextInt();
}
int [] arrayPlus = new int[limit];
int [] arrayMinus = new int[limit];
a=b=0;
for (i = 0; i < limit; i++) {
if (array1[i] > 0 || array1[i] == 0) {
arrayPlus[a] = array1[i];
a++;
} else {
arrayMinus[b] = array1[i];
b++;
}
}
System.out.println("Positive array numbers");
for (i = 0; i < a; i++) {
System.out.println(arrayPlus[i]);}
System.out.println("");
System.out.println("Negative array numbers");
for (i = 0; i < b; i++) {
System.out.println(arrayMinus[i]);}
}
}