• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pass By Value

 
Marvin Porte
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I'm practicing to code in java and here's one of the problem I'm trying to solve:
Given an array of positive ints, return a new array of length "count" containing the first even numbers from the original array. The original array will contain at least "count" even numbers.

copyEvens({3, 2, 4, 5, 8}, 2) → {2, 4}
copyEvens({3, 2, 4, 5, 8}, 3) → {2, 4, 8}
copyEvens({6, 1, 2, 4, 5, 8}, 3) → {6, 2, 4}

Here is my solution:


Now my problem is this:
1. If I run the code, it returns {0, 0, 0 ... (length)} // I think it has something to do with "pass by value" I think?
2. To solve this number 1 problem, I tried to return the output inside the loop. To do this, I replaced the 'break' by 'return output' and erase the 'return output' outside the loop. But it doesn't compile and here's the error message:
Error: public int[] copyEvens(int[] nums, int count) {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This method must return a result of type int[]

Possible problem: the if-statement structure may theoretically
allow a run to reach the end of the method without calling return.
Consider using a final else {... to ensure that return is always called.

Can you help me improve my code and can you clarify me some rules regarding pass by value, return, and if else statement? I'm confused. Thanks!
 
Ranji Sura
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this buddy...
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranjith Suranga wrote:Try this buddy...

@Marvin: Please DontWriteLongLines (←click). It makes your thread very hard to read.

@Ranjith: If you see long lines in a post, please don't copy/quote them.

I've broken both of yours up this time, but please re-read the UseCodeTags page carefully.

Thanks

Winston
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marvin Porte wrote:1. If I run the code, it returns {0, 0, 0 ... (length)} // I think it has something to do with "pass by value" I think?

Nope. Absolutely nothing whatsoever.

It has to do with the way you've written your loop.

Look at it again and ask yourself this: What is 'length' for, and does that gel with how I'm using it?
If you need to, get out a pencil and paper and write down its values for each pass through the loop.

Winston
 
Marvin Porte
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@winston
oops sorry sir i thought that if i write code here, it will look the same when i submit it.
yeah i get it now. "i<count" is wrong in the code as well as "if(nums[i]/2==0)". must be i<nums.length and % instead of / like what ranjith did. thank you! now the code is working just fine when i test it. =D
 
Ranji Sura
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Ranjith Suranga wrote:
@Ranjith: If you see long lines in a post, please don't copy/quote them.


Got it.. It is extremely thank you for showing my faults..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic