File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Arrays: Setting up a loop where the beginning and ending values are known Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays: Setting up a loop where the beginning and ending values are known" Watch "Arrays: Setting up a loop where the beginning and ending values are known" New topic
Author

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

John Ellsworth
Greenhorn

Joined: Jun 24, 2013
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

Joined: Jan 12, 2013
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

Joined: Oct 02, 2003
Posts: 10916
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

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

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays: Setting up a loop where the beginning and ending values are known
 
Similar Threads
Running a marathon
Sun Cirtification
Pass By Value
can someone help me with this program
array?