Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
John Ellsworth
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...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: 12196
35
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).

Back to your problem:
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.

Not bad as far as it goes, but WHY are you doing it? You're clearly thinking about the problem, which is good; but you've already decided on a solution (a for loop) before you've explained why - there are any number of ways you can code a loop with predetermined start and end values.
I forget the academic term for it, but in the real world it's called "putting the cart before the horse".

So: follow Fred's advice. He is a very smart bloke, and he knoweth of which he speak.

And believe me, you'll thank us both in a day or two when you can explain this problem to a child, and your resulting code is simple, clear and correct.

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic