Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

Summary of loops

Thread view: 
claradona@hotmail.com - 22 Feb 2005 10:27 GMT
Hi,

I am stumped here. Been stuck doing this program and cant seem to debug
the errors! Can someone pls help me? How do I calculate the totals of
the computation in a loop and later display those averages as a summary
outside the loop?

much help is appreciated.

thanks in advance.
Tilman Bohn - 22 Feb 2005 10:54 GMT
> Hi,
>
> I am stumped here. Been stuck doing this program and cant seem to debug
> the errors! Can someone pls help me? How do I calculate the totals of
> the computation in a loop and later display those averages as a summary
> outside the loop?

1. Declare the sum outside the loop, possibly initializing it to
  what would be the correct result if the loop body never executes
  or nothing ever gets added within the loop
2. Update sum inside loop
3. Use correct sum after loop finishes

Signature

Cheers, Tilman

`Boy, life takes a long time to live...'      -- Steven Wright

Anthony Borla - 22 Feb 2005 11:12 GMT
> Hi,
>
[quoted text clipped - 5 lines]
>
> much help is appreciated.

Mmm ! This question seems familiar ;) !!! Here's a very rough example:

   double sum = 0.0;

   for (int i = 0; i < MAX_ITERATIONS; i++)
   {
        ...
        sum += ...;
        ...
   }

   double average = sum / i;

   // Display the average, and whatever else ...
   // ...

You're likely to learn much more if you try to solve the problem yourself.

I hope this helps.

Anthony Borla
Daniel Tahin - 22 Feb 2005 14:40 GMT
>>Hi,
>>
[quoted text clipped - 25 lines]
>
> I hope this helps.

There is only a bit problem:-) i is declared in the body of for(), so
it's accessible only in it.
If you want to reach it outside for(), you should declare it above for()!

> Anthony Borla
Tilman Bohn - 22 Feb 2005 15:19 GMT
[...]
>>     for (int i = 0; i < MAX_ITERATIONS; i++)
>>     {
[quoted text clipped - 4 lines]
>>
>>     double average = sum / i;
[...]
> There is only a bit problem:-) i is declared in the body of for(), so
> it's accessible only in it.
> If you want to reach it outside for(), you should declare it above for()!

 I suspect he left the error as an exercise for the reader (because he
didn't want to do someone else's homework) -- however, you've spotted
the wrong error (or the wrong fix if you prefer).

Signature

Cheers, Tilman

`Boy, life takes a long time to live...'      -- Steven Wright

Anthony Borla - 22 Feb 2005 23:32 GMT
> [...]
> >>     for (int i = 0; i < MAX_ITERATIONS; i++)
[quoted text clipped - 11 lines]
> > If you want to reach it outside for(), you should declare
> > it above for()!

Sshhh  - don't spoil the surprise ;) !

> I suspect he left the error as an exercise for the reader
> (because he didn't want to do someone else's homework)
> -- however, you've spotted the wrong error (or the wrong
> fix if you prefer).

I'm hoping the post works like this:

* Student Googles for 'Calculating an Average' [the
  reason behind changing the subject name was to
  make it more easily found in archive searches]

* Post found - contains relevant Java code

* Attempt to utilise code is made, *but*, it doesn't
  quite work ! Hopefully *now* the learning begins

Who said homework was meant to be easy ;) !

Cheers,

Anthony Borla
Clara - 23 Feb 2005 11:58 GMT
> > [...]
> > >>     for (int i = 0; i < MAX_ITERATIONS; i++)
[quoted text clipped - 35 lines]
>
> Anthony Borla

Haha thanks a lot but it wasn't a piece of homework. I'm helping my
dad write and test out a program though I'm not trained in this
language. I thought I'll ask this qn cos I needed info on this as well
:)
Anthony Borla - 23 Feb 2005 15:07 GMT
<SNIP>

> > Sshhh  - don't spoil the surprise ;) !
>
> Haha thanks a lot but it wasn't a piece of homework.
> I'm helping my dad write and test out a program though
> I'm not trained in this language. I thought I'll ask this qn
> cos I needed info on this as well :)

I'm sure your intentions were noble ;) !

However I'm pretty confident that the post *will* be accessed [via Google]
by those wanting quick homework solutions [it's just such a typical homework
question !]. Thus, it will have served it's purpose.

Cheers,

Anthony Borla
Clara - 24 Feb 2005 22:41 GMT
> > "Anthony Borla" <ajborla@bigpond.com> wrote in message
> news:<qiPSd.171631$K7.29083@news-server.bigpond.net.au>...
[quoted text clipped - 17 lines]
>
> Anthony Borla

hmm do i notice a hint of sarcasm here? homework? geez, I haven't had
any homework in like 300 years
Anthony Borla - 24 Feb 2005 23:49 GMT
> > > "Anthony Borla" <ajborla@bigpond.com> wrote in message
> > news:<qiPSd.171631$K7.29083@news-server.bigpond.net.au>...
[quoted text clipped - 5 lines]
> hmm do i notice a hint of sarcasm here? homework? geez,
> I haven't had any homework in like 300 years

Yeah, I was having some [benign] fun being a little sarcastic. I hope you
weren't offended.

As I explained it's the type of question that constitutes a typical
'homework' question. The questions are usually easy enough to directly
answer. However, regulars in the various 'comp.lang.*' newgroups tend to be
sensitive to such questions [i.e. easily spot them because they are often
found in introductory programming courses], and rather than just hand over
an answer, encourage the poster [most often a high school or college
student] to be more active in answering the question(s) themselves.

That was what motivated my responses to you. In the absence of any other
information, I assumed you were a student, and answered in a way that I
thought would encourage learning [even if by stealth :)] rather than simply
handing an answer over.

Actually, now that I think of it, you could say *my* intentions were noble
;) !

Cheers,

Anthony Borla

P.S.

It's easy to forget, I think, that in written communications such as
informal USENET postings, that:

* A respondent is not privy to any information other than what
  appears in the message

* Cues such as tone of voice, facial expresions, or gestures
  are not available to elucidate the communication

thus the respondent has to 'fill in the blanks' by making assumptions.
Naturally these could be quite erroneous, and where they are, it's prudent,
I think, the original poster not themselves assume that there may have been
any malice or ill will behind the response


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.