#array

1 messages ยท Page 1 of 1 (latest)

spice tree
#

I was given 2 arrays:
array1 = {6, 13, 18, 1, 9, 7, 16, 11, 2}
array2 = {4, 10, 5, 12, 3, 8, 14}
I have to move the values in the two arrays so that all even values are in array 1 and all odd values are in array 2. However I can only use the 2 given arrays and cant use any additional data structures(part of the assignment).
I know I have to use a for loop and inside it i need an if statement but for some reason im forgetting how I should write the if statement having not coded in a minute.

for(int i = 0; i < array1.length; i++) {
  int value = i;
}

This is what I have so far

oak crownBOT
#

<@&987246399047479336> please have a look, thanks.

spice tree
#

I also know I will need a while loop but I need to figure out the if statement first

#

like for each value
get current value in array1 at index i
if the current value is odd then search array2 for the 1st even value to swap with the odd value

#

thats the psuedocode but I cant remember how to put it into code

turbid fjord
#

You can use modulus % to know if a value is odd or even.

spice tree
#

Oh I have forgotten all about %

#

how would I use it in this sense

#

like if (value != %)

#

or is it different

turbid fjord
#

Remember, what is modulus ๐Ÿ™‚

#

Its definitions should remind you

spice tree
#

the remainder when dividing

#

so value %2

#

?

turbid fjord
#

yes

spice tree
#

or am I still missing something

turbid fjord
#

No, you are on the right track!

spice tree
#

ok do I need the !=

turbid fjord
#

Test , see what happens

        int a = 5;
        int b = 2;

        System.out.println(a % 2);
        System.out.println(b % 2);
spice tree
#

i get an odd number (1) for a
and even (0) for b

turbid fjord
#

Try construct an if statement based on a, and b above.

spice tree
#

if( a % 2) {
a = odd
}

#

something like that

#

?

turbid fjord
#

You need a comparator too

spice tree
#

boolean?

#

or like

#

|| b % 2

turbid fjord
#

yes, that yields an boolean

spice tree
#

ok

#

would i need to declare the boolean before the if statement

turbid fjord
#

if (value % 2 == 0)
if (value % 2 == 1)

spice tree
#

ahh I see

#

I think I get it

turbid fjord
#

So you cannot create a new array? You must use those you have correct?

spice tree
#

yeah correct

turbid fjord
#

That could be challenging ๐Ÿ™‚

spice tree
#

after I write the if statement inside of it I have to search through array 2 for the first even value to swap with the odd value

#

which is a little confusing haha

#

I imagine maybe another for loop and create an index for array 2

#

but im not sure

turbid fjord
#

What I don't understand, what happens when them arrays do not have the same size, and you cannot move an odd number from the even array to the odd array due to the lack of space.

#

In your example above, they are not even

spice tree
#

Yeah I'm not really sure either, it's just what the assignment says

#

lemme snag a pic

turbid fjord
#

I see, if i calculated correctly, there are 6 odd numbers. So they fit perfectly in array2. So, array2 is doomed to be the odd array.

spice tree
#

Yes

turbid fjord
#

OK, so it is just to stimulate your brain cells.

spice tree
#

Yeah, its our first assignment back from summer break

#

so we are trying to remember arrays and such

#

but I hated arrays before the summer break LOL

lime ruin
#

you can't use a temporary array to keep track of all content in both array ?

spice tree
#

no

#

I am not allowed

#

they will dock me points

#

my professor is very specifc

turbid fjord
#

Mean proffessor ๐Ÿ˜›

#

I have to think myself on this

spice tree
#

maybe I dont need a for loop

#

either way I need to create another index for array 2

#

array 1 being i and 2 being j

#

just not sure how to go about it

#

then create a boolean and write a while loop probs

lime ruin
#

hmm

#

I need to try but if we assign all the content of 1 array in the other array; I believe we can arrive at some points

#

like say array2 now have all the contents, loop through it, if a particular index has an odd value, then add to array1

spice tree
#

I see

lime ruin
#

well no I don't think this will work because the array should be of fixed size

turbid fjord
#

Oh there are an easy take on this, if efficient is of no cocern, in terms of algorithm used ^^

spice tree
#

as long as the code stays in main

#

I cant like create methods

#

or such

lime ruin
spice tree
#

// Search array2 for 1st even value to swap with the odd value
create an index into array2 (j)
create a boolean named found and set to false
while (not at end of array2 && have not found even value)
if array2[j] is even
found = true
else
increment j
end while
I think this could work

#

but idk how to put it into code

#

this being in the if statement thats in the for loop

turbid fjord
#

There are multiple solution to this problem.
Telling mine would ruin the professors stimulated test.

#

Are you allowed to write methods?

spice tree
#

im assuming we can

#

just not required

turbid fjord
#

You can do it without, but it makes it easier if you do write them

spice tree
#

she never really says we cant

#

this is all she says about methods

turbid fjord
#

My take on this, keep it simple, at the cost of efficiency in terms of time complexcity :P, as it is not a requirement.

spice tree
#

mkay

#

so what should I do then

turbid fjord
#

I would create one main while loop, that will finish when all elements have been moved to the array1 and array2, basically complete!
In this loop, I would simply find (loop) the first index of an odd value in array1, and find (loop) the first index of an even value in array2.
Switch them!
Repeat

spice tree
#

with this I dont need a method right

#

this is in main?

turbid fjord
#

You dont need, but it makes it MUCH easier for you to think if you do.

#

yes in main

spice tree
#

what would go in the parenthesis right after the while loop

turbid fjord
#

abstract away code, give it a nice little method name, gives you extra brain cells to work with,

turbid fjord
spice tree
#

when all odds are in one and evens are in another

turbid fjord
#

Im trying to be vauge as possible ๐Ÿ˜›

spice tree
#

oh so while complete is false if array2 at index whatever is even then complete is false and if array2 at index whatever is odd then complete is true

#

and increment?

turbid fjord
#

More like, when we search for an index with an even value in the array2 (odd array), but there are none.
Vice versa for array1 (even array).

#

This is why it is easier to make a method regarding the search for index part, like

int indexA = findFirstEvenIndexOf(someArray)
int indexB = findFirstOddIndexOf(someArray)

spice tree
#

gotcha

#

lemme try sum

#

ok so im actually trying this from a different approach because I think itll be easier for me, I think I almost have it but my while loop is off

#

Instead of putting them into 1 array im just swapping each number

#
for(int i = 0; i < array1.length; i++) {
  int value = i;
  if(value % 2 == 1) {
    int j = 0;
    boolean found = false;
    while(array2[j] != 0 && found) {
      if(array2[j] % 2 == 0) {
      found = true;
      }//close if
      else {
      j++;
      }//close else
    }//close while
    if(found == true) {
      array1[i] = array2[j];
    }//close if
  }//close if
}//close for
#

the indentation on here is a lil off

#

but you get it

turbid fjord
#

int value = i;
You are checking if index i is odd

#

int value = array1[i]

spice tree
#

oh

turbid fjord
#

Your while loop condition also need some love

spice tree
#

yeah im so bad at while loops

#

they are probs my worst thing

#

like figuring out conditions

#

I just cant I have no clue why

turbid fjord
#

You are almost there.

#

Think of when the while loop should stop. Which conditions are there.
One is for example when 'j' is out of bounds to array2 for example, and as long as an even value has NOT been found.

#

I gotta go to my bed now.
You can solve it, very close.

spice tree
#

kk thank you!

turbid fjord
#

Are you using intellij?

spice tree
#

No im using eclipse

turbid fjord
#

No matter, my question should have been more like, are you using a real IDE. Some guys in here use some weird school IDEs.
USE DEBUG! Set a breakpoint and step through your code and analyze the result. You will learn alot that way, and find them mistakes.

spice tree
#

debugger so grosss but yes Ill use it haha

#

thank you so much!

turbid fjord
#

np, nnight

spice tree
#

nightt