Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# Arrays: Setting up a loop where the beginning and ending values are known

John Ellsworth
Greenhorn
Posts: 10
Hi everyone,

I'm having trouble with a practice assignment I'm working on. Here it is:

"5-5 Practice Problems: Arrays
Using the scenario below, answer the following questions:

You are preparing for the Boston Marathon. In order to prepare, you need to train for 10 weeks, running an increasing number of miles per week, starting at running at least 2 miles your first week up to 26 miles by week 10."

This is only the first part of the problem. I'm thinking that a "for" loop is the way to go. I know that I can only have 10 numbers, the first must be 2 and the last must be 26. I've run an early version of the program a few times. Depending on the statement that I write, I either start with 2 and end with 20, or start with -1 and end with 26. Can I get some help regarding what statement will output the proper numbers?

Here is an example of one of the versions. Remember, I'm a greenhorn, so my code is, shall we say' less than elegant. (Note: this is not the whole assignment. The rest involves, summing up the values of the array, finding the average, and searching for a particular value. It's only this part that is stopping me.)

manish ghildiyal
Ranch Hand
Posts: 136
...what actually is the objective of your program?
Currently you are just populating your array, imparting value as i+2 and then printing that value...
hence output is 2 3 4 5 6 7 8 9 10 11 which exactly matches the formula you use.

Manish

fred rosenberger
lowercase baba
Bartender
Posts: 12090
29
• 1
My advice would be: StopCoding (<---click that). first you have to figure out WHAT you want to do..only then do you figure out HOW you want to do it.

Winston Gutkowski
Bartender
Posts: 10111
56
• 1
John Ellsworth wrote:I'm having trouble with a practice assignment I'm working on. Here it is:

Before I go into any detail, John, let me tell you this: I gave Fred's post a +1 without even looking at your post.

Why? Because it's a fundamental premise of programming: you cannot write code for a problem you don't understand; and an oft-used benchmark is this:
If you can't explain to a child in English (or your native language) how to solve the problem, in a way that they understand, then you still don't.
And you shouldn't write one line of Java until you can.

Now this is probably the last thing you want to hear because you're just itching to get coding.

DON'T.

The result is called "gorilla code": Problem.....code.....Uuurgh.
And the byproducts are broken keyboards and bloody foreheads from banging your head against the screen, because it doesn't work (although these days, you young 'uns have these soft flat-screen things).