0
Comparison two array in java
I need to compare between the two arrays below: Double [ ] a = {0.123 , 0.342 , 0.422 , 0.834}; Double [ ] b = {0.321 , 0.637 , 0.765 , 0.244}; int [ ] c; output =: a[0] < b[0] < a[1] => c[0]= 1; a[2] < b[1] < a[3] => c[1]= 3; a[2] < b[2] < a[3] => c[2]= 3; a[0] < b[3] < a[1] => c[3]= 1; The numbers of arrays a and b are random and change each time. I need a code that can do the above comparison. b [i] are placed between each of the numbers of array a. The index of the most delicate number is transferred to array c.
16 odpowiedzi
+ 4
Here you go:
https://code.sololearn.com/cuAWF4fomuiF/#java
But to be fair, you should do your homework by yourself.
Edit: code fixed.
+ 2
Please post your code attempt
Question like this is great if it meets the following criteria:
- The question hasn't already been asked and answered.
- You can share the code showing your attempt.
Otherwise, it appears as if you are posting your assignment questions which are designed to be exercises for learning.
The best way to learn is to make an attempt, then seek help based specifically on where you are stuck.
+ 2
Here is the demo
The argument sets range of random number
https://code.sololearn.com/cBxS6KP7Ty0y/?ref=app
+ 2
OK, so we jump to the comparison part
It will be nested for loop
The inner for loop will be for comparing each b against other a
Because you only need to use the larger side
So we only need to compare which a is larger than b
So we declare a temp variable to store the index.
When a 0 is larger than b , we store 0 in the temp variable
Then if a 1 is larger than b, we check a 1 and a 0, if a 1 smaller, we update the temp variable
See the logic?
But warn you, if there is no a larger than b, no value will be stored.
+ 2
Code fixed, should work now. Just had to turn ((a[i] < b[j]) && (i < a.length)) into ((i < a.length) && (a[i] < b[j])). My bad.
+ 1
but it not work for this
https://code.sololearn.com/ch0KyznalrcS/?ref=app
0
I can not write code because I do not know
I only have the array code
https://code.sololearn.com/cVel9q71V9sj/?ref=app
0
But then where do you come across this question? Is it school homework?
0
I simulate the TSP with the genetic algorithm for school homework
0
Let's do this step by step then.
Firstly for random the number
import java.util.*;
Random random = new Random();
random.nextInt()
You'll need this for random
0
ok
0
i do this
0
i
I have the code for producing random arrays
They are in eclipse.
I just need a code to do the comparison
0
ok
Yes that's right.
0
my God .
It looks right.
Thanks a lot
❤❤❤❤❤
0
I din't notice