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.
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.
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.
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..
1st # | 2nd # | how many even #s between 1st & 2nd #s |
---|---|---|
odd | odd | ??? |
odd | even | ??? |
even | odd | ??? |
even | even | ??? |
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.
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?
Jacob Sousie wrote:(...)
There's 8 even between 15 and 30, so do I make this then?
There are three kinds of actuaries: those who can count, and those who can't.
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 |
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.
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.
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.
I'd argue on that.Jacob Sousie wrote:here's my finished code
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 evenProblem 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?
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.
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.
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.
Naveen Narsagalla wrote:I have come up with a below code for your requirement.
Wanna see my flashlight? How about this tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|