- 1

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

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.

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

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

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.

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

- 1

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.

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

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:

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?

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, 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).

Practice mindfully by doing the right things and doing things right.

[How to Ask Questions] [How to Answer Questions]

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: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

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: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 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?

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.

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?

Have a Cow!

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 imaginesize.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!

- 1

Practice mindfully by doing the right things and doing things right.

[How to Ask Questions] [How to Answer Questions]

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!

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.

Practice mindfully by doing the right things and doing things right.

[How to Ask Questions] [How to Answer Questions]

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

Practice mindfully by doing the right things and doing things right.

[How to Ask Questions] [How to Answer Questions]

Junilu Lacar wrote:The requirement is to create an array that can hold

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

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.

I was referring to OP's formulae. You'll still have the special case of a single odd number range.

Practice mindfully by doing the right things and doing things right.

[How to Ask Questions] [How to Answer Questions]

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.

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.

Practice mindfully by doing the right things and doing things right.

[How to Ask Questions] [How to Answer Questions]