Finding even numbers in an array
posted 3 weeks ago
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.....
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: 492
3
posted 3 weeks ago
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.
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.
Junilu  [How to Ask Questions] [How to Answer Questions]
posted 3 weeks ago
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 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..
posted 3 weeks ago
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.
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  [How to Ask Questions] [How to Answer Questions]
posted 3 weeks ago
 1
I'll give you another hint.
There are four cases you need to consider in figuring out the math.
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 are four cases you need to consider in figuring out the math.
1st #  2nd #  how many even #s between 1st & 2nd #s 

odd  odd  ??? 
odd  even  ??? 
even  odd  ??? 
even  even  ??? 
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.
Junilu  [How to Ask Questions] [How to Answer Questions]
posted 3 weeks ago
There's 8 even between 15 and 30, so do I make this then?
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 odd odd ??? odd even ??? even odd ??? even even ???
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?
posted 3 weeks ago
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 odd odd ??? odd even ??? even odd ??? even even ???
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
Rancher
Posts: 1686
41
posted 3 weeks ago
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:
... now work out the last one, eveneven. 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).
odd  odd #  # even #s between 

1  3  1 2 3 → 1 
1  5  1 2 3 4 5 → 2 
1  7  1 2 3 4 5 6 7 → 3 
odd  even #  # even #s between 

1  2  1 2 → 1 
1  4  1 2 3 4 → 2 
1  6  1 2 3 4 5 6 → 3 
even  odd #  # even #s between 

2  3  2 3 → 1 
2  5  2 3 4 5 → 2 
2  7  2 3 4 5 6 7 → 3 
... now work out the last one, eveneven. 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).
Junilu  [How to Ask Questions] [How to Answer Questions]
Fred Kleinschmidt
Bartender
Posts: 492
3
posted 3 weeks ago
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 iftest altogether, if you start at int1 if it is even, and int1+1 if it is odd, and incrementing by 2.
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 iftest altogether, if you start at int1 if it is even, and int1+1 if it is odd, and incrementing by 2.
posted 3 weeks ago
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 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 iftest 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: 492
3
posted 3 weeks ago
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.
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.
posted 3 weeks ago
Yeah! That's crazy how you have to do that. lol here's my finished code:
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: 492
3
posted 3 weeks ago
Oh no! I didn't realize that! How do I fix it without breaking the old one?
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=(71)/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: 53094
124
posted 3 weeks ago
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.
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.
posted 3 weeks ago
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?
I'd argue on that.Jacob Sousie wrote:here's my finished code
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?
posted 3 weeks ago
Congratulations Jacob Sousie your question has made it to our CodeRanch Journal  February 2017
Have a Cow!
Have a Cow!
posted 3 weeks ago
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!
Liutauras Vilda wrote:I'd argue on that.Jacob Sousie wrote:here's my finished code
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!
posted 3 weeks ago
 1
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.
Junilu  [How to Ask Questions] [How to Answer Questions]
posted 3 weeks ago
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 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!
posted 3 weeks ago
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 ifstatement 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.
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 ifstatement 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.
Junilu  [How to Ask Questions] [How to Answer Questions]
Declan Barrett
Greenhorn
Posts: 23
posted 3 weeks ago
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.
Junilu  [How to Ask Questions] [How to Answer Questions]
Declan Barrett
Greenhorn
Posts: 23
posted 3 weeks ago
Fair enough.
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.
posted 3 weeks ago
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.
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.
Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
posted 3 weeks ago
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.
I was referring to OP's formulae. You'll still have the special case of a single odd number range.
Junilu  [How to Ask Questions] [How to Answer Questions]
Campbell Ritchie
Marshal
Posts: 53094
124
posted 3 weeks ago
int[] evenNumbers = new int[0];
or
int[] evenNumbers = {};
but not both.
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.Junilu Lacar wrote:. . . You'll still have the special case of a single odd number range.
int[] evenNumbers = new int[0];
or
int[] evenNumbers = {};
but not both.
posted 3 weeks ago
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.
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.
Junilu  [How to Ask Questions] [How to Answer Questions]
Happiness is not a goal ... it's a byproduct of a life well lived  Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/ThreadBoostfeature
