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

# Finding even numbers in an array

Ranch Hand
Posts: 53
1
• 1
• Number of slices to send:
Optional 'thank-you' note:
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:

15
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.....

Bartender
Posts: 732
10
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:

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?

Sheriff
Posts: 17648
300
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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: 17648
300
• Number of slices to send:
Optional 'thank-you' note:

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: 17648
300
• 1
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:

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?

Bartender
Posts: 5466
212
• Number of slices to send:
Optional 'thank-you' note:

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: 17648
300
• Number of slices to send:
Optional 'thank-you' note:
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: 732
10
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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: 732
10
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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: 732
10
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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?

Marshal
Posts: 79263
377
• Number of slices to send:
Optional 'thank-you' note:
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.

Marshal
Posts: 8869
637
• Number of slices to send:
Optional 'thank-you' note:

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?

Creator of Enthuware JWS+ V6
Posts: 3411
320
• Number of slices to send:
Optional 'thank-you' note:
Congratulations Jacob Sousie  your question has made it to our CodeRanch Journal - February 2017

Have a Cow!

Jacob Sousie
Ranch Hand
Posts: 53
1
• Number of slices to send:
Optional 'thank-you' note:

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?

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

Example output:

1
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: 17648
300
• 1
• Number of slices to send:
Optional 'thank-you' note:
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
• Number of slices to send:
Optional 'thank-you' note:

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.

Junilu Lacar
Sheriff
Posts: 17648
300
• Number of slices to send:
Optional 'thank-you' note:
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.

Ranch Hand
Posts: 36
• Number of slices to send:
Optional 'thank-you' note:
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: 17648
300
• Number of slices to send:
Optional 'thank-you' note:
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
Ranch Hand
Posts: 36
• Number of slices to send:
Optional 'thank-you' note:

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.

Saloon Keeper
Posts: 10747
86
• Number of slices to send:
Optional 'thank-you' note:
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: 17648
300
• Number of slices to send:
Optional 'thank-you' note:
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: 79263
377
• Number of slices to send:
Optional 'thank-you' note:

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: 17648
300
• Number of slices to send:
Optional 'thank-you' note:

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.

 Don't get me started about those stupid light bulbs.