• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading all even numbers in array and move them in the starting of an array.

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Java users,
I am new to java programming. I have programming in MatLAB before, but its my first time when I am using Java.. I am trying to solve following problem
Question : Given an array of integers move all even numbers to the beginning of the array.)



I will appreciate any help
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Describe the problem you are having. Does it compile? Does it work? What output do you get?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code doesn't compile. Take a look at where the braces are. Also, you reference a variable k that you didn't define. Did you mean j?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the problem isn't incredibly clear...are you supposed to move each integer as you find it to the front, which would reverse them, or are you supposed to find all of them, and move them en mass?  in other words...if you start with this:

{ 1, 3, 3, 4, 5, 8, 0 }

when you find 4, do you move it, giving this:

{ 4, 1, 3, 3, 5, 8, 0 }

then you find 8, so you do this:

{ 8, 4 1, 3, 3, 5, 0 }  and then 0, so you get this:

{ 0, 8, 4 1, 3, 3, 5 }

----OR--------

Should you find 4, 8, and 0, and move them all, giving you this:

( 4, 8, 0, 1, 3, 3, 5}



I would approach these two in different ways.  
 
John Hicks
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:That code doesn't compile. Take a look at where the braces are. Also, you reference a variable k that you didn't define. Did you mean j?


Yes, that supposed to be j.. Is there any way that I can place the braces properly? if you can share a link from where I can learn about braces and how to fix them..


It gives the following error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error, insert "}" to complete Block
Syntax error, insert "}" to complete Statement
j cannot be resolved to a variable
j cannot be resolved to a variable
Syntax error on token "}", { expected

at AA2Q2.main(AA2Q2.java:14)
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I will appreciate any help


@OP
Is it the logic you don't understand or how to achieve it in Java?
In case of latter, please explain what logic you chose to use so we could help to accomplish it in Java.
 
John Hicks
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:the problem isn't incredibly clear...are you supposed to move each integer as you find it to the front, which would reverse them, or are you supposed to find all of them, and move them en mass?  in other words...if you start with this:

{ 1, 3, 3, 4, 5, 8, 0 }

when you find 4, do you move it, giving this:

{ 4, 1, 3, 3, 5, 8, 0 }

then you find 8, so you do this:

{ 8, 4 1, 3, 3, 5, 0 }  and then 0, so you get this:

{ 0, 8, 4 1, 3, 3, 5 }

----OR--------

Should you find 4, 8, and 0, and move them all, giving you this:

( 4, 8, 0, 1, 3, 3, 5}



I would approach these two in different ways.  



oh ok thanks ! I am really new to programming so did't know there can be different ways to approach this.. would you explain the first way ( 0,8,4,1,3,3,5} Thanks
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This actually gets kind of tricky...you're stepping through the array, looking at each element, but at the same time, you're re-arranging the elements...i've not thought through this too deep, but here are some of my thoughts...

1) you need a way to step through each element in a given array.
2) you need to determine if the "current element" is one that needs to move
3) you need a way to move an element from some arbitrary position to the first position in the array, and then "slide" all the other down, up to the position of the original element.

notice how each of these are completely independent of all the other items.  It doesn't matter how i find the position of an element...i just need to pass that in to whatever does #3.  So each of these to me imply a separate method.  

So, if i were doing this, I'd start with #3.  Write a method that takes and array, and a position in the array, that does all this moving around.  Once you know that method works (which you'd know by writing some temp code that sends it an array and a position), then you can write one of the other methods.
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the order of the odd numbers need to be preserved?
If not, you can swap the next even element with the next odd element and the advance the pointers to the next odd and next even etc
 
Carey Brown
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see anything in the requirements that stipulates the order of the final array other than all evens must come before all odds. Seems like a modified bubble sort that swaps odd numbers with even numbers would do the trick without a loop for shifting the array contents.
IN: [1, 3, 3, 4, 5, 8, 0]
OUT: [4, 8, 0, 1, 5, 3, 3]
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:. . . . are you supposed to move each integer as you find it  . . . .  

Or, if you find an odd number at the beginning, are you supposed to swap it with an odd number to its right?

I think the swapping of two elements in an array seems to be a common exercise, and I often suggest people write themselves a utility class with a swapTwoElementsInArray method.
 
reply
    Bookmark Topic Watch Topic
  • New Topic