Comparison two array in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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.

18th Jan 2019, 1:30 PM
Gafoor Ezzati
Gafoor Ezzati - avatar
16 Answers
+ 4
Here you go: https://code.sololearn.com/cuAWF4fomuiF/#java But to be fair, you should do your homework by yourself. Edit: code fixed.
18th Jan 2019, 3:32 PM
Zen
Zen - avatar
+ 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.
18th Jan 2019, 1:35 PM
Gordon
Gordon - avatar
+ 2
Here is the demo The argument sets range of random number https://code.sololearn.com/cBxS6KP7Ty0y/?ref=app
18th Jan 2019, 2:04 PM
Gordon
Gordon - avatar
+ 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.
18th Jan 2019, 2:23 PM
Gordon
Gordon - avatar
+ 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.
18th Jan 2019, 4:58 PM
Zen
Zen - avatar
18th Jan 2019, 4:34 PM
Gafoor Ezzati
Gafoor Ezzati - avatar
0
I can not write code because I do not know I only have the array code https://code.sololearn.com/cVel9q71V9sj/?ref=app
18th Jan 2019, 1:42 PM
Gafoor Ezzati
Gafoor Ezzati - avatar
0
But then where do you come across this question? Is it school homework?
18th Jan 2019, 1:46 PM
Gordon
Gordon - avatar
0
I simulate the TSP with the genetic algorithm for school homework
18th Jan 2019, 1:48 PM
Gafoor Ezzati
Gafoor Ezzati - avatar
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
18th Jan 2019, 1:56 PM
Gordon
Gordon - avatar
0
ok
18th Jan 2019, 2:02 PM
Gafoor Ezzati
Gafoor Ezzati - avatar
0
i do this
18th Jan 2019, 2:02 PM
Gafoor Ezzati
Gafoor Ezzati - avatar
0
i I have the code for producing random arrays They are in eclipse. I just need a code to do the comparison
18th Jan 2019, 2:06 PM
Gafoor Ezzati
Gafoor Ezzati - avatar
0
ok Yes that's right.
18th Jan 2019, 2:25 PM
Gafoor Ezzati
Gafoor Ezzati - avatar
0
my God . It looks right. Thanks a lot ❤❤❤❤❤
18th Jan 2019, 3:47 PM
Gafoor Ezzati
Gafoor Ezzati - avatar
0
I din't notice
18th Jan 2019, 5:27 PM
Gafoor Ezzati
Gafoor Ezzati - avatar