Related
i just created an array have 100 elements, so now i want get 10 elements print first, secondly i want continuing 10 elements print out, and 10 elements third keep going. My code is below:
int[] array = { 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,..., 100 };
int count = 0;
for (int i = 0; i < array.length; i++) {
count++;
if (count == 10) {
System.out.println(array[i]);
count = 0;
}
}
Your logic is correct, only issue is with the printing of array values.
int[] array = { 0, 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,....,100};
int count = 0;
for (int i = 0; i < array.length; i++) {
count++;
System.out.print(array[i]+" ");
if (count == 10) {
System.out.println();
count = 0;
}
}
This can be done fairly easily by iterating through the array with a double for-loop.
One loop will iterate through the modulo index and the other will print the 10 elements with that modulo value.
int[] array = {1,2,3,4,5,6,7,8,9,10,...,100};
for (int offset = 0; offset < array.length/10; offset++)
for (int i = 0 + offset; i < array.length; i+= array.length/10)
System.out.println(array[i]);
I need to find the max and min for every 3 elements of a data using java array.
For double data[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14} , what is the max and min in every 3 elements? In other words, what is the max and min for {1,2,3}, {4, 5, 6}, {7, 8, 9}, etc.
I have the following method, but it is not getting the right results.
for (int i = 0; i < data.length; i+=3) {
for (int r = i; r < i + 3 && r < data.length; r++) {
if (data[r] < lowest) { lowest = data[r]; }
if (data[r] > highest) { highest = data[r]; }
}
lowest = highest;
highest = 0;
How about this?
int[] data = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
int processed = 0;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < data.length; i++) {
if (data[i] < min) {
min = data[i];
}
if (data[i] > max) {
max = data[i];
}
processed++;
if (processed == 3) {
System.out.println("Min is: " + min);
System.out.println("Max is: " + max);
System.out.println("--------");
processed = 0;
min = Integer.MAX_VALUE;
max = Integer.MIN_VALUE;
}
}
Output:
Min is: 1
Max is: 3
--------
Min is: 4
Max is: 6
--------
Min is: 7
Max is: 9
--------
Min is: 10
Max is: 12
--------
It's unclear what to do with the numbers 13 and 14, as they don't form a block of 3, but I guess you get the idea of the algorithm.
My goal is to insert array B[] into array A[] after the element with index K.
I don't need to lengthen the A[], the last 5 elements should just disappear.
This is what I got so far. Dont really mind the beginning of the program, thats just some extra requirements I had to do determine the arrays.
For example :
If I insert Kas 2, than array A is
0 2 4 6 8 10 12 14 16 18 0 0 0 0 0, and array B is 20 40 60 80 100.
The final array A should look like this:
0 2 4 20 40 60 80 100 6 8 10 12 14 16 18
public static void main(String[] args) {
int A[] = new int [15];
int B[] = new int [5];
int K, i, j;
Scanner sc = new Scanner(System.in);
Random r = new Random();
for (i=10; i<=14; i++) {
A[i] = 0;
}
System.out.println("Matīss Lavrinovičs RDBD0 171RDB075");
System.out.print("K=");
if (sc.hasNextInt())
K = sc.nextInt();
else {
System.out.println("input-output error");
sc.close();
return;
}
sc.close();
if (K<0 || K>9) {
for (i=0; i<=9; i++)
A[i] = r.nextInt(50);
for (j=0; j<=4; j++)
B[j] = r.nextInt(100 - 50) + 50; }
else
for (i=0; i<=9; i++)
A[i] = i*K;
for (j=0;j<=4;j++)
B[j] = 10*(j+1)*K;
System.out.print("A: ");
i = 0;
while (i < 15) {
System.out.print(" " + A[i]);
if (i==14) System.out.println();
i = i + 1;
}
System.out.print("B: ");
j = 0;
while (j < 5) {
System.out.print(" " + B[j]);
j = j + 1;
}
do {
} while;
You can use System.arrayCopy:
int[] a = new int[] { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 0, 0, 0, 0, 0 };
int[] b = new int[] { 20, 40, 60, 80, 100 };
int k = 2;
System.arraycopy(a, k + 1, a, k + 1 + b.length, a.length - b.length - k - 1);
System.arraycopy(b, 0, a, k + 1, b.length);
What happens is that we first copy the values after index #2 b.length places to the right (which is five). Then we copy the values of b into array a at the correct positions.
Alternative approach:
List<Integer> list = asList(a).subList(0, a.length - b.length);
list.addAll(k + 1, asList(b));
And a little helper method:
private static List<Integer> asList(int... ints) {
return IntStream.of(ints)
.boxed()
.collect(Collectors.toList());
}
You should take the following into consideration:
You should stick to the Java Naming Conventions: variable names start with a lowercase letter.
Omitting curling brackets {} like that often leads to bugs in the code. You should always use them.
One way to use System.arrayCopy to copy from array b[] to array a[] from position k
int[] a = new int[] { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 0, 0, 0, 0, 0 };
int[] b = new int[] { 20, 40, 60, 80, 100 };
int k = 1;
int length = (a.length - k) > b.length ? b.length : (a.length - k);
System.arraycopy(b, 0, a, k, length);
Take care: In case array b[] does not fit in the remaining space in array a[] and as you said you don't want to lengthen array a[], only copy until there is space left (calculate int length with the ternary conditional operator).
Basically i have to return an array of n numbers repeated in an array into another array of length m.
my code so far:
public class Histogram {
public static int[] frequency(int[] a, int M) {
int[] m = new int[M];
int count = 0;
for(int j = 0; j < a.length; j++)
for(int i = 0; i < m.length; i++)
if( i == a[j]){
m[i] = count++;
}
}
return m;
} public static void main(String[] args) {
int[] a = {7, 4, 9, 1, 10, 11, 11, 1, 5, 8, 4, 2, 9, 4, 3, 9,
2, 10, 11, 7, 7, 1, 11, 3, 8, 8, 10, 4, 10, 5};
int[] b = frequency(a, 12);
for (int i = 0; i < b.length; i++) {
Std.Out.println(i + "->" + b[i]);
} } }
this is the output im supposed to get
0->0
1->3
2->2
3->2
4->4
5->2
6->0
7->3
8->3
9->3
10->4
11->4
but im getting
0->0
1->21
2->16
3->23
4->27
5->29
6->0
7->20
8->25
9->15
10->28
11->22
what am i doing wrong?
I am not going to give you a different solution, but rather to show the mistakes in your code. If we go with the logic in your code, you have few mistakes:
1) count should be reset to 0 after first loop. Otherwise you will sum up the counts from the previous iterations.
2) count++ should be before the assignment, otherwise it will first assign and then increment. Or you could use ++count.
3) The first loop should be over i.
public static int[] frequency(int[] a, int M) {
int[] m = new int[M];
int count;
for(int i = 0; i < m.length; i++) {
count =0;
for(int j = 0; j < a.length; j++)
if( i == a[j]){
count++;
m[i] = count;
}
}
return m;
}
Regards.
This should solve it and do the job!
public class Histogram {
public static int[] frequency(int[] a, int M) {
int[] m = new int[M];
for(int j = 0; j < a.length; j++) {
m[a[j]] += 1;
}
return m;
}
public static void main(String[] args) {
int[] a = {7, 4, 9, 1, 10, 11, 11, 1, 5, 8, 4, 2, 9, 4, 3, 9,
2, 10, 11, 7, 7, 1, 11, 3, 8, 8, 10, 4, 10, 5};
int[] b = frequency(a, 12);
for (int i = 0; i < b.length; i++) {
System.out.println(i + "->" + b[i]);
}
}
}
public class Histogram {
public static int[] frequency(int[] a, int M) {
int[] m = new int[M];
for(int i = 0; i < a.length; i++) //loop through a
if( i < M) //number check
m[a[i]]++; //add one to m at index a[i]
return m;
}
}
this is the right code for the method frequency, also checks to see if the number is in range.
I'm trying to find the evens and odds of an array for practice.
Sample Data :
2 4 6 8 10 12 14
1 2 3 4 5 6 7 8 9
2 10 20 21 23 24 40 55 60 61
Sample Output :
Odds - []
Evens - [2, 4, 6, 8, 10, 12, 14]
Odds - [1, 3, 5, 7, 9]
Evens - [2, 4, 6, 8]
Odds - [21, 23, 55, 61]
Evens - [2, 10, 20, 24, 40, 60]
And here is my code so far:
import java.util.Scanner;
public class OddsAndEvens
{
private static int countEm(int[] array, boolean odd)
{
int count = 0;
int dum = 0;
for(int i=0; i< array.length; i++)
{
dum = array[i] / 2;
if(dum == 0 )
{
count++;
}
}
return count;
}
public static int[] getAllEvens(int[] array)
{
int numberEvens=0;
for(int i =0; i<array.length; i++)
{
if(array[i]%2 ==0)
{
numberEvens++;
}
}
int[] evens = new int[array.length - countEm(array,false)];
int count=0;
for(int i=0; i<array.length; i++)
{
if(array[i]%2==0)
{
evens[count] = array[i];
count++;
}
}
return evens;
}
public static int[] getAllOdds(int[] array)
{
int numberEvens = 0;
for (int i = 0; i < array.length; i++)
{
if (array[i] % 2 == 0)
{
numberEvens++;
}
}
return null;
}
}
But I been getting errors in my output. These errors are the fact I am getting zeros in my output .
I'm just starting out and I'm hoping anyone can help me.
Your main problem comes from your countEm method. It is also the reason for all the extra zeroes behind your array. By doing minimal changes can solve these problems and make your program work.
One of the major careless mistake is that you used / instead of % for checking evens and odds in your countEm method.
Your countEm method is incomplete
Using wrong operator to count even/odd
Your incomplete countEm method now only counts the number of even number no matter the Boolean value is true or false. I've helped you implemented the complete countEm:
private static int countEm(int[] array, boolean odd)
{
int numEven = 0;
int numOdd = 0;
for(int i=0; i< array.length; i++)
if(array[i] % 2 == 0 ) //You used / instead of % earlier!
numEven++;
else
numOdd++;
if(odd)
return numOdd;
else
return numEven;
}
Make the above changes, I am sure it will work. Of course, you haven't complete your method for getting getAllOdds yet. Above changes will make your getAllEvens work properly.
Alternatively, for myself, I like to write it this way. I prefer shorter codes:
private static int countEm(int[] array, boolean odd)
{
int numEven = 0;
for(int i=0; i< array.length; i++)
if(array[i] % 2 == 0 )
numEven++;
return odd?array.length-numEven:numEven;
}
I prevent using data structures like array list because when I see such questions, I know what are the only things you are allowed to use.
I think this would be the best way to do that:
import java.util.ArrayList;
import java.util.List;
public class EvensAndOdds {
public static List<Integer> getOdds(int[] numbers){
List<Integer> odds = new ArrayList<>();
for(int i = 0; i < numbers.length; i++){
if((numbers[i]%2) != 0){
odds.add(numbers[i]);
}
}
return odds;
}
public static List<Integer> getEvens(int[] numbers){
List<Integer> evens = new ArrayList<>();
for(int i = 0; i < numbers.length; i++){
if((numbers[i]%2) == 0){
evens.add(numbers[i]);
}
}
return evens;
}
public static void main(String[] args){
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
List<Integer> evens = getEvens(numbers);
List<Integer> odds = getOdds(numbers);
System.out.println("Evens: ");
for(Integer even: evens){
System.out.print(even.toString() + " ");
}
System.out.println("");
System.out.println("Odds: ");
for(Integer odd: odds){
System.out.print(odd.toString() + " ");
}
}
}
What about ;):
public class EvenOddSorter {
public void sort(int[] numbers) {
Arrays.sort(numbers,new Comparator<int>() {
#Override
public int compare(int i, int y) {
if(i % 2 == 0)
return -1;
if(y % 2 == 0)
return 1;
return 0;
}
});
}
}
then extract odds and evens from your array by finding the first odd number and using
int[] evens = Arrays.copyOfRange(numbers, 0, numberOfFirstOdd);
int[] odds = Arrays.copyOfRange(numbers, numberOfFirstOdd, numbers.length)
;)
You are creating a new array with too many initial items - hence the zeros at the end. Your getAllEvens() method could look more like:
public static int[] getAllEvens(int[] array)
{
int numberEvens = 0;
for (int i = 0; i < array.length; i++)
{
if (array[i] % 2 == 0)
{
numberEvens++;
}
}
int[] evens = new int[numberEvens]; // This has changed
int count = 0;
for (int i = 0; i < array.length; i++)
{
if (array[i] % 2 == 0)
{
evens[count] = array[i];
count++;
}
}
return evens;
}
This creates a new array with exactly the number of items you are about to populate it with.
Edit: To address your comment, here is my main() method and the output from the method above.
public static void main(String[] args)
{
int[] input =
{
2, 4, 6, 8, 10, 12, 14,
1, 2, 3, 4, 5, 6, 7, 8, 9,
2, 10, 20, 21, 23, 24, 40, 55, 60, 61
};
int[] evens = getAllEvens(input);
System.out.println(Arrays.toString(evens));
}
Which gives
[2, 4, 6, 8, 10, 12, 14, 2, 4, 6, 8, 2, 10, 20, 24, 40, 60]