Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: nested while loop computing triangular number - issues |
| Date | 2015-07-11 21:24 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87io9qtpbs.fsf@bsb.me.uk> (permalink) |
| References | <8e9f29f8-a7d0-42c4-a146-2a0d0b937051@googlegroups.com> <87si8uu5tr.fsf@bsb.me.uk> <ad9fec5e-57e1-46cf-8a35-79ac4434171e@googlegroups.com> |
Alla _ <modelling.data@gmail.com> writes:
> On Saturday, July 11, 2015 at 6:28:24 PM UTC+4, Ben Bacarisse wrote:
> "Why not
>
> int n = 1, triangular_number = 0, counter = 1, number;
>
> or
>
> int n = 1;
> int triangular_number = 0;
> int counter = 1;
> int number; "
This style of quoting is not at all standard and it will eventually get
either confusing, annoying, or both! Have a look at how most of the
other posts here do quoting and see if you can get your news interface
(Google groups, I'm, guessing) to use that. If you want to post more
than occasionally, it might be worth while getting a Usenet news reading
program.
> Do I understand correctly that I have made a mistake here by declaring
> n, triangular_number, counter and number as int, and then have initialized
> all but "number"?
No, as James has said it's not a mistake. I was just wondering why you
did it that way.
<snip>
> "The second time round the outer loop, n has the value it had before.
> That's not right for this tringular number. "
>
> Sorry, this is not clear to me :) As in my previous simpler program,
> which I posted here, the same while loop technique works fine.
But that program did not have the outer loop and it is the outer loop
that is the problem.
> I read it as "until n reaches number triangular number = n + previous
> triangular number, thus producing the final triangular number when n
> reaches number".
And that is exactly what happens, but "until n reaches..." depends on
what n has to start with. Your for-loop version (below) sets n to 1
correctly (once for every triangle number), but the while loop version
sets it only once.
> As to functions, no, those are in the next chapter :)
I'd love to know the name of this book (or tutorial). What is it?
> In this program I have to rewrite the for loop program with the while loop one:
>
> // nested for loop to prompt the user to name 5 ∆numbers
> #include <stdio.h>
>
> int main (void)
> {
> int n, number, triangular_number, counter;
>
> for (counter = 1; counter <= 5; counter ++)
> {
> printf ("Please, name your triangular number");
> scanf ("%i", &number);
>
> triangular_number = 0;
>
> for (n = 1; n <= number; n++)
> triangular_number += n;
When you change this to a while loop you removed a key part: the n = 1 part!
> printf ("The triangular number %i is %i\n", number, triangular_number);
> }
>
> return 0;
> }
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
nested while loop computing triangular number - issues Alla _ <modelling.data@gmail.com> - 2015-07-11 06:31 -0700
Re: nested while loop computing triangular number - issues Alla _ <modelling.data@gmail.com> - 2015-07-11 06:52 -0700
Re: nested while loop computing triangular number - issues Siri Cruz <chine.bleu@yahoo.com> - 2015-07-11 07:07 -0700
Re: nested while loop computing triangular number - issues Alla _ <modelling.data@gmail.com> - 2015-07-11 07:27 -0700
Re: nested while loop computing triangular number - issues Siri Cruz <chine.bleu@yahoo.com> - 2015-07-11 08:19 -0700
Re: nested while loop computing triangular number - issues Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-11 15:28 +0100
Re: nested while loop computing triangular number - issues Alla _ <modelling.data@gmail.com> - 2015-07-11 08:01 -0700
Re: nested while loop computing triangular number - issues James Kuyper <jameskuyper@verizon.net> - 2015-07-11 12:24 -0400
Re: nested while loop computing triangular number - issues Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-11 21:24 +0100
Re: nested while loop computing triangular number - issues Alla _ <modelling.data@gmail.com> - 2015-07-12 09:35 -0700
Re: nested while loop computing triangular number - issues Keith Thompson <kst-u@mib.org> - 2015-07-12 11:55 -0700
Re: nested while loop computing triangular number - issues Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-12 22:14 +0100
Re: nested while loop computing triangular number - issues Alla _ <modelling.data@gmail.com> - 2015-07-13 08:05 -0700
Re: nested while loop computing triangular number - issues David Kleinecke <dkleinecke@gmail.com> - 2015-07-11 10:51 -0700
Re: nested while loop computing triangular number - issues Keith Thompson <kst-u@mib.org> - 2015-07-11 14:56 -0700
Re: nested while loop computing triangular number - issues David Kleinecke <dkleinecke@gmail.com> - 2015-07-12 09:32 -0700
Re: nested while loop computing triangular number - issues James Kuyper <jameskuyper@verizon.net> - 2015-07-12 13:19 -0400
Re: nested while loop computing triangular number - issues David Kleinecke <dkleinecke@gmail.com> - 2015-07-13 10:16 -0700
Re: nested while loop computing triangular number - issues Keith Thompson <kst-u@mib.org> - 2015-07-12 11:48 -0700
Re: nested while loop computing triangular number - issues James Kuyper <jameskuyper@verizon.net> - 2015-07-12 15:23 -0400
Re: nested while loop computing triangular number - issues Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-15 11:02 -0700
Re: nested while loop computing triangular number - issues Keith Thompson <kst-u@mib.org> - 2015-07-15 13:19 -0700
Re: nested while loop computing triangular number - issues Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-20 11:01 -0700
Re: nested while loop computing triangular number - issues Alla _ <modelling.data@gmail.com> - 2015-07-11 08:45 -0700
csiph-web