 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
claradona@hotmail.com Guest
|
Posted: Tue Feb 22, 2005 10:27 am Post subject: Summary of loops |
|
|
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.
|
|
| Back to top |
|
 |
Tilman Bohn Guest
|
Posted: Tue Feb 22, 2005 10:54 am Post subject: Re: Summary of loops |
|
|
In message <1109068051.789292.213810 (AT) g14g2000cwa (DOT) googlegroups.com>,
[email]claradona (AT) hotmail (DOT) com[/email] wrote on 22 Feb 2005 02:27:31 -0800:
| Quote: | 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
--
Cheers, Tilman
`Boy, life takes a long time to live...' -- Steven Wright
|
|
| Back to top |
|
 |
Anthony Borla Guest
|
Posted: Tue Feb 22, 2005 11:12 am Post subject: Calculating an average [was Re: Summary of loops] |
|
|
<claradona (AT) hotmail (DOT) com> wrote
| Quote: | 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.
|
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
|
|
| Back to top |
|
 |
Daniel Tahin Guest
|
Posted: Tue Feb 22, 2005 2:40 pm Post subject: Re: Calculating an average [was Re: Summary of loops] |
|
|
Anthony Borla wrote:
| Quote: | claradona (AT) hotmail (DOT) com> wrote in message
news:1109068051.789292.213810 (AT) g14g2000cwa (DOT) googlegroups.com...
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.
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.
|
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()!
|
|
| Back to top |
|
 |
Tilman Bohn Guest
|
Posted: Tue Feb 22, 2005 3:19 pm Post subject: Re: Calculating an average [was Re: Summary of loops] |
|
|
In message <421b445a$0$12384$3b214f66 (AT) tunews (DOT) univie.ac.at>,
Daniel Tahin wrote on Tue, 22 Feb 2005 15:40:26 +0100:
| Quote: | Anthony Borla wrote:
[...]
for (int i = 0; i < MAX_ITERATIONS; i++)
{
...
sum += ...;
...
}
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).
--
Cheers, Tilman
`Boy, life takes a long time to live...' -- Steven Wright
|
|
| Back to top |
|
 |
Anthony Borla Guest
|
Posted: Tue Feb 22, 2005 11:32 pm Post subject: Re: Summary of loops |
|
|
"Tilman Bohn" <myfirstname (AT) gmx (DOT) net> wrote
| Quote: | In message <421b445a$0$12384$3b214f66 (AT) tunews (DOT) univie.ac.at>,
Daniel Tahin wrote on Tue, 22 Feb 2005 15:40:26 +0100:
Anthony Borla wrote:
[...]
for (int i = 0; i < MAX_ITERATIONS; i++)
{
...
sum += ...;
...
}
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()!
|
Sshhh - don't spoil the surprise !
| Quote: |
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
|
|
| Back to top |
|
 |
Clara Guest
|
Posted: Wed Feb 23, 2005 11:58 am Post subject: Re: Summary of loops |
|
|
"Anthony Borla" <ajborla (AT) bigpond (DOT) com> wrote
| Quote: | "Tilman Bohn" <myfirstname (AT) gmx (DOT) net> wrote in message
news:slrnd1mjcf.8o5.myfirstname (AT) urizen (DOT) tilmanbohn.com...
In message <421b445a$0$12384$3b214f66 (AT) tunews (DOT) univie.ac.at>,
Daniel Tahin wrote on Tue, 22 Feb 2005 15:40:26 +0100:
Anthony Borla wrote:
[...]
for (int i = 0; i < MAX_ITERATIONS; i++)
{
...
sum += ...;
...
}
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()!
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
|
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
:)
|
|
| Back to top |
|
 |
Anthony Borla Guest
|
Posted: Wed Feb 23, 2005 3:07 pm Post subject: Re: Summary of loops |
|
|
"Clara" <claradona (AT) hotmail (DOT) com> wrote
| Quote: | "Anthony Borla" <ajborla (AT) bigpond (DOT) com> wrote
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
|
|
| Back to top |
|
 |
Clara Guest
|
Posted: Thu Feb 24, 2005 10:41 pm Post subject: Re: Summary of loops |
|
|
"Anthony Borla" <ajborla (AT) bigpond (DOT) com> wrote
| Quote: | "Clara" <claradona (AT) hotmail (DOT) com> wrote in message
news:a1de1d9c.0502230358.51970a3e (AT) posting (DOT) google.com...
"Anthony Borla" <ajborla (AT) bigpond (DOT) com> wrote in message
news:<qiPSd.171631$K7.29083 (AT) news-server (DOT) bigpond.net.au>...
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
|
hmm do i notice a hint of sarcasm here? homework? geez, I haven't had
any homework in like 300 years
|
|
| Back to top |
|
 |
Anthony Borla Guest
|
Posted: Thu Feb 24, 2005 11:49 pm Post subject: Re: Summary of loops |
|
|
"Clara" <claradona (AT) hotmail (DOT) com> wrote
| Quote: | "Anthony Borla" <ajborla (AT) bigpond (DOT) com> wrote
"Clara" <claradona (AT) hotmail (DOT) com> wrote in message
news:a1de1d9c.0502230358.51970a3e (AT) posting (DOT) google.com...
"Anthony Borla" <ajborla (AT) bigpond (DOT) com> wrote in message
news:<qiPSd.171631$K7.29083 (AT) news-server (DOT) bigpond.net.au>...
SNIP
I'm sure your intentions were noble !
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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|