• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding even numbers in an array  RSS feed

 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I've been trying to learn how arrays work.  But I've ran into a problem...  I don't know how to get this code to work!  I've been at it for about an hour, and have been reading my book and looking online, but I can't find anything that is close to what I'm trying to do.


Here's my problem:



1. Read in two integers from the user, stored in int1 and int2
2. Store all the even numbers between int1 and int2, inclusive, in an array
3. Make the array exactly large enough to store the numbers
4. Display the size of the array
5. Display all the values from the array on a single line

Sample output from program runs:

Please enter the starting number.
15
Please enter the ending number
30
The Array size is 8
The contents of the array are: 16 18 20 22 24 26 28 30







Here's my code so far.  It's a mess right now from experimenting.....




 
Fred Kleinschmidt
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After reading in the two numbers, the first thing you need to do is determine how many even numbers there are between them (including them if they are even). That is the size of the int array you need to create.
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:After reading in the two numbers, the first thing you need to do is determine how many even numbers there are between them (including them if they are even). That is the size of the int array you need to create.


How would I do that?
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:After reading in the two numbers, the first thing you need to do is determine how many even numbers there are between them (including them if they are even). That is the size of the int array you need to create.







Is this any closer?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your example, 15 and 30 are the two numbers that bound the range that you're going to work with. Why would you put these numbers into arrays?

You want to think: How many even numbers are between 15 and 30 (inclusive)?

It doesn't seem logical to try to answer that by putting 15 and 30 into arrays, does it? If you agree, then how would you use 15 and 30 to determine how big of an array you need to create to hold all the even numbers that fall between 15 and 30?  Hint: Do the math.
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:In your example, 15 and 30 are the two numbers that bound the range that you're going to work with. Why would you put these numbers into arrays?

You want to think: How many even numbers are between 15 and 30 (inclusive)?

It doesn't seem logical to try to answer that by putting 15 and 30 into arrays, does it? If you agree, then how would you use 15 and 30 to determine how big of an array you need to create to hold all the even numbers that fall between 15 and 30?  Hint: Do the math.



Still learning how arrays work.  I wouldn't know if putting them in different ones would work or not.  Still new to Java..
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Sousie wrote:Still learning how arrays work.  I wouldn't know if putting them in different ones would work or not.  Still new to Java..

It's got nothing to do with how arrays work or being new to Java.  If you had to solve this problem by hand, with paper and pencil, without a computer, without Java, how would you do it? You'd still have to do the math, right? And figure out the logic. That's what it's all about. Once you figure out what the logic is, translating to Java is easier.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll give you another hint.

There are four cases you need to consider in figuring out the math.
1st #2nd #how many even #s between 1st & 2nd #s
oddodd???
oddeven???
evenodd???
eveneven???

Once you figure the above out, you'll know how big of an array you need to create to hold all those even #s, right? Then you're halfway done. All you need to do is fill that array with all the even #s you calculated that it would need to hold.
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:I'll give you another hint.

There are four cases you need to consider in figuring out the math.
1st #2nd #how many even #s between 1st & 2nd #s
oddodd???
oddeven???
evenodd???
eveneven???

Once you figure the above out, you'll know how big of an array you need to create to hold all those even #s, right? Then you're halfway done. All you need to do is fill that array with all the even #s you calculated that it would need to hold.


There's 8 even between 15 and 30,  so do I make this then?


 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Sousie wrote:
Junilu Lacar wrote:I'll give you another hint.

There are four cases you need to consider in figuring out the math.
1st #2nd #how many even #s between 1st & 2nd #s
oddodd???
oddeven???
evenodd???
eveneven???

Once you figure the above out, you'll know how big of an array you need to create to hold all those even #s, right? Then you're halfway done. All you need to do is fill that array with all the even #s you calculated that it would need to hold.


There's 8 even between 15 and 30,  so do I make this then?






 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Sousie wrote:(...)
There's 8 even between 15 and 30,  so do I make this then?


Yes, but how did you determine that there are 8 even numbers between 15 and 30? Can you apply that method (if that is what you used) to find the number of evens between say, 15 and 110?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to think about how to get from specifics to general.  It helps to take the simplest cases possible then try to expand to more complicated cases:

oddodd ## even #s between
131 2 3 → 1
151 2 3 4 5 → 2
171 2 3 4 5 6 7 → 3


oddeven ## even #s between
121 2 → 1
141 2 3 4 → 2
161 2 3 4 5 6 → 3


evenodd ## even #s between
232 3 → 1
252 3 4 5 → 2
272 3 4 5 6 7 → 3


... now work out the last one, even-even. You have to find a way to generalize these patterns so that you can calculate how many even #s there are between any two given numbers.  Don't touch your computer, don't think about Java. Just think about the MATH and the pattern(s).
 
Fred Kleinschmidt
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have coded this:
Why do you think you need three arrays: You only need one array, whose size is equal to the number of evens in the specified interval. So this is what you need (everything inside <...> needs to be coded ):


And the loop can be done even better, eliminating the if-test altogether, if you start at int1 if it is even, and int1+1 if it is odd, and incrementing by 2.
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:You have coded this:
Why do you think you need three arrays: You only need one array, whose size is equal to the number of evens in the specified interval. So this is what you need (everything inside <...> needs to be coded ):


And the loop can be done even better, eliminating the if-test altogether, if you start at int1 if it is even, and int1+1 if it is odd, and incrementing by 2.





Here's what I've come up with.  Everything else works, but I don't know how to count the even elements of the array.  which should be 8 using 15 and 30


 
Fred Kleinschmidt
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you creating an array of size zero? later on, you write which will always print zero.

You need to pay attention to what Junilu wrote - figure out an equation to calculate the number of even values.
Here's a hint: if both ends are even,
ONly after you determine that number should you create the int array.

 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:Why are you creating an array of size zero? later on, you write which will always print zero.

You need to pay attention to what Junilu wrote - figure out an equation to calculate the number of even values.
Here's a hint: if both ends are even,
ONly after you determine that number should you create the int array.




Yeah!  That's crazy how you have to do that. lol  here's my finished code:





 
Fred Kleinschmidt
Bartender
Posts: 572
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Better, but you still have not taken into account what will happen if one or both of the input range ends might be odd.
For example, if you enter (1,7) then your current code will say n=(7-1)/2+1 which is 4, but there are only 3 evens in that range.
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:Better, but you still have not taken into account what will happen if one or both of the input range ends might be odd.
For example, if you enter (1,7) then your current code will say n=(7-1)/2+1 which is 4, but there are only 3 evens in that range.


Oh no!  I didn't realize that!  How do I fix it without breaking the old one?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you got a formula for counting even numbers? By formula I mean the difference between summating numbers between 1 and 10 like this:-and like thisThe latter is a formula. You probably learnt it yourself at school about the age of twelve.

The number of different integers between n and m inclusive (m ≥ n) is m − n + 1. If that is an even number then exactly half of them will be even. If that is an odd number, dividing by 2 will give you half, rounded towards 0 if you use integer arithmetic on your computer (all languages I have come across will give the same result for n ÷ 2 for all values of n). Now consider whether you need to add 1 depending on the values of the first and last numbers in your sequence.
 
Liutauras Vilda
Sheriff
Posts: 4929
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jacob Sousie wrote:here's my finished code
I'd argue on that.

Now there are lots of things to fix:
1. Class name. EvenArray? Maybe EvenNumbersArray? Former I don't think is even correct English formulation. Sounds like if you were have lots of arrays and the problem is about arrays which are even  Problem is not about the arrays, but rather about the content, which is even numbers.

2. Variable names. int1, int2. What is that? Maybe startingNumber1 and startingNumber2 at least, so it would be closer the actual intent you're trying to express in your program? [1] "Please enter the staring number: " - typo in prompt. [2] "please enter the ending number: " - typo again, sentences start with an upper case.

3. Variable name 'size'. Not looking to code have no clue what that might be. But trying right away to imagine size.length - odd on its own right.

4. Here is a lot confusion with initialisations. 'count' initialised outside loop, why? What is index? Why is the count2 starts being used and only then count, why not other way round? Do you see how many questions pops up only from few lines of code?
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Posts: 3046
247
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations Jacob Sousie   your question has made it to our CodeRanch Journal - February 2017

Have a Cow!
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:
Jacob Sousie wrote:here's my finished code
I'd argue on that.

Now there are lots of things to fix:
1. Class name. EvenArray? Maybe EvenNumbersArray? Former I don't think is even correct English formulation. Sounds like if you were have lots of arrays and the problem is about arrays which are even  Problem is not about the arrays, but rather about the content, which is even numbers.

2. Variable names. int1, int2. What is that? Maybe startingNumber1 and startingNumber2 at least, so it would be closer the actual intent you're trying to express in your program? [1] "Please enter the staring number: " - typo in prompt. [2] "please enter the ending number: " - typo again, sentences start with an upper case.

3. Variable name 'size'. Not looking to code have no clue what that might be. But trying right away to imagine size.length - odd on its own right.

4. Here is a lot confusion with initialisations. 'count' initialised outside loop, why? What is index? Why is the count2 starts being used and only then count, why not other way round? Do you see how many questions pops up only from few lines of code?


How about this?   this code works:






NOTE:  I mean for the evens to add the last number.  That's what my teacher wants.

Example output:



Please enter the staring number:
1
Please enter the ending number:
10
The array size is:
5
The contents of the array are:
2 4 6 8 10

That last 10 is supposed to be there intentionally.
This follows example I was given.




Thank you all so much for helping me solve this problem!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Congratulations on finding out how to make it work. Hopefully, you'll try to come back to this again in the future and try to make it a little less complicated. There are still a number of things you can do to make this code simpler and cleaner.
 
Jacob Sousie
Ranch Hand
Posts: 53
1
Eclipse IDE Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Congratulations on finding out how to make it work. Hopefully, you'll try to come back to this again in the future and try to make it a little less complicated. There are still a number of things you can do to make this code simpler and cleaner.


How should I make it simpler and cleaner?
I should learn how to make clean code from the beginning haha.  I don't want to keep doing things a more messy and difficult way than I need to.
Thanks in advance!
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a great attitude, wanting to learn how to write clean code while you're still learning and haven't formed too many habits that will be more difficult to unlearn later on.

You could start by following some of the advice you've been given throughout this entire thread.

Take the advice about names. You were told that int1 and int2 are not very good names, yet you continue to use them instead of trying out the alternative ones that were suggested.

If I remember correctly, someone also told you that a for loop isn't limited to incrementing its loop index variable by one all the time. The i++ is not the only thing you can do. You can use an expression like i += 2 for example, to increment i by 2 each time through the loop. That way you won't need an if-statement to see if you need to skip a number.

I told you about code duplication in your winOrTie method. You can find a way to use loops to eliminate that.

I told you very early on how the winOrTie() method didn't even make sense semantically. While I suppose this requires a little bit more sophistication to understand how to fix, you could start thinking about why it doesn't make logical sense. Go back over this thread and read what I posted about this. See if you can understand my reasoning. When you do get it, try to change this entire method so it does make sense. And by change, I really mean replace it.
 
Declan Barrett
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you need to work out the size of the array beforehand?

Is it not possible to use a For loop to add any even number to your array and once this is finished, get & output the contents and size of the array?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The requirement is to create an array that can hold exactly as many even numbers there are in a range. This makes it necessary to work out the size of the array before you start filling it with even numbers. Once you allocate an array, you can't change its size.
 
Declan Barrett
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:The requirement is to create an array that can hold exactly as many even numbers there are in a range. This makes it necessary to work out the size of the array before you start filling it with even numbers. Once you allocate an array, you can't change its size.


Fair enough.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than deal with four different odd/even scenarios I attempt to coerce the begin/end numbers to even values:
That then leaves only one special case where both begin and end are the exact same odd value, hence array length would be zero.
Then calculating the number of evens is trivial and creating a loop to populate the array is trivial.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could take the special case and coerce one of the bounds so that the range conforms with the other cases. The count of evens doesn't change.

I was referring to OP's formulae. You'll still have the special case of a single odd number range.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . You'll still have the special case of a single odd number range.
If you use my method of halving the range you get 0. Then you add 1 if the starting number is even, otherwise 0, and you have still got 0.
int[] evenNumbers = new int[0];
or
int[] evenNumbers = {};
but not both.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naveen Narsagalla wrote:I have come up with a below code for your requirement.

Welcome to the Ranch, Naveen Narsagalla!

Please don't post complete solution code if the original poster (OP) is still working on the problem. We encourage everyone to do their own homework here and we don't want to make this site a code mill.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!