Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #65259 > unrolled thread
| Started by | Alla _ <modelling.data@gmail.com> |
|---|---|
| First post | 2015-07-11 06:31 -0700 |
| Last post | 2015-07-11 08:45 -0700 |
| Articles | 20 on this page of 24 — 7 participants |
Back to article view | Back to comp.lang.c
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
Page 1 of 2 [1] 2 Next page →
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-07-11 06:31 -0700 |
| Subject | nested while loop computing triangular number - issues |
| Message-ID | <8e9f29f8-a7d0-42c4-a146-2a0d0b937051@googlegroups.com> |
Hello!
I have tried to write a program to prompt the user to name
triangular number 5 times, compute the number and display
the result.
I would be grateful for your insight on why the program would
compute the correct number only once. I am attaching 3 versions
(replacing counter++) and results for each one.
VERSION 1
// nested while loop to prompt the user to name 5 ∆numbers
#include <stdio.h>
int main (void)
{
int n, number, triangular_number, counter;
n = 1;
counter = 1;
triangular_number = 0;
while ( counter <= 5 )
{
printf ("Please, name your triangular number: ");
scanf ("%i", &number);
while ( n <= number )
{
triangular_number += n;
++n;
}
++counter;
printf ("The triangular number %i is %i\n", number, triangular_number);
}
return 0;
}
Here I have placed ++counter right after inner while loop.
I thought this would tell the program: ask the user for the number,
compute that number (inner while loop), add 1 to the counter, display
the computed number.
These are two versions of the result I get:
1) Please, name your triangular number: 10
The triangular number 10 is 55
Please, name your triangular number: 23
The triangular number 23 is 276
Please, name your triangular number: 2
The triangular number 2 is 276
Please, name your triangular number: 10
The triangular number 10 is 276
Please, name your triangular number: 23
The triangular number 23 is 276
2) Please, name your triangular number: 10
The triangular number 10 is 55
Please, name your triangular number: 1
The triangular number 1 is 55
Please, name your triangular number: 2
The triangular number 2 is 55
Please, name your triangular number: 23
The triangular number 23 is 276
Please, name your triangular number: 5
The triangular number 5 is 276
What is really strange is that it does somehow detect that
23 triangular number is 276. In the first version the program
computes the first one correctly, then repeats the result
no matter which number the user enters; but if the user enters 23,
the program switches to 276 and goes on with that number.
VERSION 2
/ nested while loop to prompt the user to name 5 ∆numbers
#include <stdio.h>
int main (void)
{
int n, number, triangular_number, counter;
n = 1;
counter = 1;
triangular_number = 0;
while ( counter <= 5 )
{
printf ("Please, name your triangular number: ");
scanf ("%i", &number);
++counter;
while ( n <= number )
{
triangular_number += n;
++n;
}
printf ("The triangular number %i is %i\n", number, triangular_number);
}
return 0;
}
Here I have put ++counter after scanf.
Again rather strange results (276 seems to be a magical number!)
1) Please, name your triangular number: 4
The triangular number 4 is 10
Please, name your triangular number: 5
The triangular number 5 is 15
Please, name your triangular number: 1
The triangular number 1 is 15
Please, name your triangular number: 23
The triangular number 23 is 276
Please, name your triangular number: 1
The triangular number 1 is 276
2) Please, name your triangular number: 10
The triangular number 10 is 55
Please, name your triangular number: 5
The triangular number 5 is 55
Please, name your triangular number: 2
The triangular number 2 is 55
Please, name your triangular number: 23
The triangular number 23 is 276
Please, name your triangular number: 1
The triangular number 1 is 276
Thank you very much for your help!
[toc] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-07-11 06:52 -0700 |
| Message-ID | <7e96390a-9a7e-48a5-b5f8-224712c7b95d@googlegroups.com> |
| In reply to | #65259 |
Just as an addition, here is a short program I wrote before trying the nested while loop, and this first program works fine.
// prompt the user to name triangular number
#include <stdio.h>
int main (void)
{
int n, number, triangular_number;
n = 1;
triangular_number = 0;
printf ("Please, name which triangular number you want: \n");
scanf ("%i", &number);
while ( n <= number )
{
triangular_number += n;
++n;
}
printf ("Triangular number %i is %i\n", number, triangular_number);
return 0;
}
[toc] | [prev] | [next] | [standalone]
| From | Siri Cruz <chine.bleu@yahoo.com> |
|---|---|
| Date | 2015-07-11 07:07 -0700 |
| Message-ID | <chine.bleu-E4C090.07071411072015@88-209-239-213.giganet.hu> |
| In reply to | #65259 |
In article <8e9f29f8-a7d0-42c4-a146-2a0d0b937051@googlegroups.com>,
Alla _ <modelling.data@gmail.com> wrote:
> Hello!
>
> I have tried to write a program to prompt the user to name
> triangular number 5 times, compute the number and display
> the result.
>
> I would be grateful for your insight on why the program would
> compute the correct number only once. I am attaching 3 versions
> (replacing counter++) and results for each one.
Because you don't set the inner loop index with loop initiation. Use for for
this kind of loop: it reminds you what to do.
>
> VERSION 1
>
> // nested while loop to prompt the user to name 5 ∆numbers
> #include <stdio.h>
>
> int main (void)
> {
> int n, number, triangular_number, counter;
> //n = 1;
> //counter = 1;
> //triangular_number = 0;
>
> //while ( counter <= 5 )
for (counter=1; counter<=5; counter++)
> {
> printf ("Please, name your triangular number: ");
> scanf ("%i", &number);
triangular_number = 0;
>
> //while ( n <= number )
for (n=1; n<=number; n++)
> {
> triangular_number += n;
> //++n;
> }
>
> //++counter;
> printf ("The triangular number %i is %i\n", number,
> triangular_number);
> }
>
> return 0;
> }
--
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted.
'I desire mercy, not sacrifice.'
When is a Kenyan not a Kenyan? When he's a Canadian.
That's People's Commissioner Siri Cruz now. Punch!
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-07-11 07:27 -0700 |
| Message-ID | <a0167080-bc1a-4bd6-ac0f-f1f560c45225@googlegroups.com> |
| In reply to | #65264 |
On Saturday, July 11, 2015 at 6:07:31 PM UTC+4, Siri Cruz wrote:
> In article <8e9f29f8-a7d0-42c4-a146-2a0d0b937051@googlegroups.com>,
> Alla _ <modelling.data@gmail.com> wrote:
>
> > Hello!
> >
> > I have tried to write a program to prompt the user to name
> > triangular number 5 times, compute the number and display
> > the result.
> >
> > I would be grateful for your insight on why the program would
> > compute the correct number only once. I am attaching 3 versions
> > (replacing counter++) and results for each one.
>
> Because you don't set the inner loop index with loop initiation. Use for for
> this kind of loop: it reminds you what to do.
>
> >
> > VERSION 1
> >
> > // nested while loop to prompt the user to name 5 ∆numbers
> > #include <stdio.h>
> >
> > int main (void)
> > {
> > int n, number, triangular_number, counter;
> > //n = 1;
> > //counter = 1;
> > //triangular_number = 0;
> >
> > //while ( counter <= 5 )
> for (counter=1; counter<=5; counter++)
> > {
> > printf ("Please, name your triangular number: ");
> > scanf ("%i", &number);
> triangular_number = 0;
> >
> > //while ( n <= number )
> for (n=1; n<=number; n++)
> > {
> > triangular_number += n;
> > //++n;
> > }
> >
> > //++counter;
> > printf ("The triangular number %i is %i\n", number,
> > triangular_number);
> > }
> >
> > return 0;
> > }
>
> --
> :-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted.
> 'I desire mercy, not sacrifice.'
> When is a Kenyan not a Kenyan? When he's a Canadian.
> That's People's Commissioner Siri Cruz now. Punch!
Hello!
Thank you for reply. I have this program with the for loop. The task is to rewrite it with the while loop.
[toc] | [prev] | [next] | [standalone]
| From | Siri Cruz <chine.bleu@yahoo.com> |
|---|---|
| Date | 2015-07-11 08:19 -0700 |
| Message-ID | <chine.bleu-063765.08192511072015@88-209-239-213.giganet.hu> |
| In reply to | #65271 |
In article <a0167080-bc1a-4bd6-ac0f-f1f560c45225@googlegroups.com>,
Alla _ <modelling.data@gmail.com> wrote:
> On Saturday, July 11, 2015 at 6:07:31 PM UTC+4, Siri Cruz wrote:
> > In article <8e9f29f8-a7d0-42c4-a146-2a0d0b937051@googlegroups.com>,
> > Alla _ <modelling.data@gmail.com> wrote:
> >
> > > Hello!
> > >
> > > I have tried to write a program to prompt the user to name
> > > triangular number 5 times, compute the number and display
> > > the result.
> > >
> > > I would be grateful for your insight on why the program would
> > > compute the correct number only once. I am attaching 3 versions
> > > (replacing counter++) and results for each one.
> >
> > Because you don't set the inner loop index with loop initiation. Use for
> > for
> > this kind of loop: it reminds you what to do.
> >
> > >
> > > VERSION 1
> > >
> > > // nested while loop to prompt the user to name 5 ∆numbers
> > > #include <stdio.h>
> > >
> > > int main (void)
> > > {
> > > int n, number, triangular_number, counter;
> > > //n = 1;
> > > //counter = 1;
> > > //triangular_number = 0;
> > >
> > > //while ( counter <= 5 )
> > for (counter=1; counter<=5; counter++)
> > > {
> > > printf ("Please, name your triangular number: ");
> > > scanf ("%i", &number);
> > triangular_number = 0;
> > >
> > > //while ( n <= number )
> > for (n=1; n<=number; n++)
> > > {
> > > triangular_number += n;
> > > //++n;
> > > }
> > >
> > > //++counter;
> > > printf ("The triangular number %i is %i\n", number,
> > > triangular_number);
> > > }
> > >
> > > return 0;
> > > }
> >
> > --
> > :-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted.
> > 'I desire mercy, not sacrifice.'
> > When is a Kenyan not a Kenyan? When he's a Canadian.
> > That's People's Commissioner Siri Cruz now. Punch!
>
> Hello!
>
> Thank you for reply. I have this program with the for loop. The task is to
> rewrite it with the while loop.
for (expr1; expr2; expr3) stmt
is equivalent to
{
expr1;
while (expr2) {
stmt;
expr3;
}
}
with the loop initialisation (expr1) executed every time the loop begins
executions.
--
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted.
'I desire mercy, not sacrifice.'
When is a Kenyan not a Kenyan? When he's a Canadian.
That's People's Commissioner Siri Cruz now. Punch!
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-07-11 15:28 +0100 |
| Message-ID | <87si8uu5tr.fsf@bsb.me.uk> |
| In reply to | #65259 |
Alla _ <modelling.data@gmail.com> writes:
> I have tried to write a program to prompt the user to name
> triangular number 5 times, compute the number and display
> the result.
>
> I would be grateful for your insight on why the program would
> compute the correct number only once. I am attaching 3 versions
> (replacing counter++) and results for each one.
>
> VERSION 1
>
> // nested while loop to prompt the user to name 5 ∆numbers
> #include <stdio.h>
>
> int main (void)
> {
> int n, number, triangular_number, counter;
> n = 1;
> counter = 1;
> triangular_number = 0;
Why not
int n = 1, triangular_number = 0, counter = 1, number;
or
int n = 1;
int triangular_number = 0;
int counter = 1;
int number;
?
> while ( counter <= 5 )
> {
> printf ("Please, name your triangular number: ");
> scanf ("%i", &number);
You need to check the result. Get into the habit of always checking
that input worked.
> while ( n <= number )
> {
> triangular_number += n;
> ++n;
> }
The second time round the outer loop, n has the value it had before.
That's not right for this tringular number.
> ++counter;
> printf ("The triangular number %i is %i\n", number, triangular_number);
> }
>
> return 0;
> }
Have you learned about functions yet?
<snip>
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-07-11 08:01 -0700 |
| Message-ID | <ad9fec5e-57e1-46cf-8a35-79ac4434171e@googlegroups.com> |
| In reply to | #65273 |
On Saturday, July 11, 2015 at 6:28:24 PM UTC+4, Ben Bacarisse wrote:
> Alla _ <modelling.data@gmail.com> writes:
>
> > I have tried to write a program to prompt the user to name
> > triangular number 5 times, compute the number and display
> > the result.
> >
> > I would be grateful for your insight on why the program would
> > compute the correct number only once. I am attaching 3 versions
> > (replacing counter++) and results for each one.
> >
> > VERSION 1
> >
> > // nested while loop to prompt the user to name 5 ∆numbers
> > #include <stdio.h>
> >
> > int main (void)
> > {
> > int n, number, triangular_number, counter;
> > n = 1;
> > counter = 1;
> > triangular_number = 0;
>
> Why not
>
> int n = 1, triangular_number = 0, counter = 1, number;
>
> or
>
> int n = 1;
> int triangular_number = 0;
> int counter = 1;
> int number;
>
> ?
>
> > while ( counter <= 5 )
> > {
> > printf ("Please, name your triangular number: ");
> > scanf ("%i", &number);
>
> You need to check the result. Get into the habit of always checking
> that input worked.
>
> > while ( n <= number )
> > {
> > triangular_number += n;
> > ++n;
> > }
>
> The second time round the outer loop, n has the value it had before.
> That's not right for this tringular number.
>
> > ++counter;
> > printf ("The triangular number %i is %i\n", number, triangular_number);
> > }
> >
> > return 0;
> > }
>
> Have you learned about functions yet?
>
> <snip>
> --
> Ben.
Hello!
Thank you!
"Why not
int n = 1, triangular_number = 0, counter = 1, number;
or
int n = 1;
int triangular_number = 0;
int counter = 1;
int number; "
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"? I thought that this is a legitimate option in C.
> while ( n <= number )
> {
> triangular_number += n;
> ++n;
> }
"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.
I read it as "until n reaches number triangular number = n + previous triangular number, thus producing the final triangular number
when n reaches number".
As to functions, no, those are in the next chapter :)
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;
printf ("The triangular number %i is %i\n", number, triangular_number);
}
return 0;
}
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-07-11 12:24 -0400 |
| Message-ID | <55A14351.6000405@verizon.net> |
| In reply to | #65280 |
On 07/11/2015 11:01 AM, Alla _ wrote:
> On Saturday, July 11, 2015 at 6:28:24 PM UTC+4, Ben Bacarisse wrote:
>> Alla _ <modelling.data@gmail.com> writes:
...
>>> int main (void)
>>> {
>>> int n, number, triangular_number, counter;
>>> n = 1;
>>> counter = 1;
>>> triangular_number = 0;
>>
>> Why not
>>
>> int n = 1, triangular_number = 0, counter = 1, number;
>>
>> or
>>
>> int n = 1;
>> int triangular_number = 0;
>> int counter = 1;
>> int number;
>>
>> ?
...
> 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"? I thought that this is a legitimate option in C.
No, it's not a mistake. It is perfectly legitimate. He's asking why you
didn't take advantage of one of the conveniences of C. The following code:
int n;
n = 1;
Has exactly the same effect as
int n = 1;
The second form is just a convenience feature - not using it isn't a
mistake, it just means that you typed more code than you needed to. But
you did type more code than you needed to, and that's what he was asking
about.
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-07-11 21:24 +0100 |
| Message-ID | <87io9qtpbs.fsf@bsb.me.uk> |
| In reply to | #65280 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-07-12 09:35 -0700 |
| Message-ID | <0ed07d72-c4f0-4408-a3f8-1d9d59dd846f@googlegroups.com> |
| In reply to | #65332 |
On Sunday, July 12, 2015 at 12:24:50 AM UTC+4, Ben Bacarisse wrote:
> 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.
It is a Programming in C. Thanks to this book I have learned about
this wonderful forum! :)
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-07-12 11:55 -0700 |
| Message-ID | <lnpp3xmcil.fsf@kst-u.example.com> |
| In reply to | #65418 |
Alla _ <modelling.data@gmail.com> writes:
> On Sunday, July 12, 2015 at 12:24:50 AM UTC+4, Ben Bacarisse wrote:
>> Alla _ <modelling.data@gmail.com> writes:
[...]
>> > 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?
[...]
> It is a Programming in C. Thanks to this book I have learned about
> this wonderful forum! :)
It would be very helpful if you'd trim any quoted text that's not
relevant to your followup (as I've done here).
Is this the book by Stephen G. Kochan?
http://www.amazon.com/Programming-4th-Edition-Developers-Library/dp/0321776410/
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-07-12 22:14 +0100 |
| Message-ID | <87k2u5oz8k.fsf@bsb.me.uk> |
| In reply to | #65441 |
Keith Thompson <kst-u@mib.org> writes: > Alla _ <modelling.data@gmail.com> writes: >> On Sunday, July 12, 2015 at 12:24:50 AM UTC+4, Ben Bacarisse wrote: >>> Alla _ <modelling.data@gmail.com> writes: > [...] >>> > 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? > [...] >> It is a Programming in C. Thanks to this book I have learned about >> this wonderful forum! :) > > It would be very helpful if you'd trim any quoted text that's not > relevant to your followup (as I've done here). > > Is this the book by Stephen G. Kochan? That one certainly contains the exercise about triangular numbers so it is very likely to be the one. > http://www.amazon.com/Programming-4th-Edition-Developers-Library/dp/0321776410/ -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | Alla _ <modelling.data@gmail.com> |
|---|---|
| Date | 2015-07-13 08:05 -0700 |
| Message-ID | <9b7904d6-ad31-491b-af9a-ad97bcd55c2a@googlegroups.com> |
| In reply to | #65441 |
On Sunday, July 12, 2015 at 10:55:45 PM UTC+4, Keith Thompson wrote: > Alla _ <modelling.data@gmail.com> writes: > > On Sunday, July 12, 2015 at 12:24:50 AM UTC+4, Ben Bacarisse wrote: > >> Alla _ <modelling.data@gmail.com> writes: > [...] > >> > 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? > [...] > > It is a Programming in C. Thanks to this book I have learned about > > this wonderful forum! :) > > It would be very helpful if you'd trim any quoted text that's not > relevant to your followup (as I've done here). > > Is this the book by Stephen G. Kochan? > > http://www.amazon.com/Programming-4th-Edition-Developers-Library/dp/0321776410/ yes, that is the one :)
[toc] | [prev] | [next] | [standalone]
| From | David Kleinecke <dkleinecke@gmail.com> |
|---|---|
| Date | 2015-07-11 10:51 -0700 |
| Message-ID | <5abf1a27-ebbf-4c5d-89d9-e0b8fc6efd04@googlegroups.com> |
| In reply to | #65273 |
On Saturday, July 11, 2015 at 7:28:24 AM UTC-7, Ben Bacarisse wrote:
> Alla _ <modelling.data@gmail.com> writes:
> > int main (void)
> > {
> > int n, number, triangular_number, counter;
> > n = 1;
> > counter = 1;
> > triangular_number = 0;
>
> Why not
>
> int n = 1, triangular_number = 0, counter = 1, number;
>
> or
>
> int n = 1;
> int triangular_number = 0;
> int counter = 1;
> int number;
I have recently taken to writing this way - I repeat in my style:
int main (void) {
int n;
int number;
int triangular_number;
int counter;
#
n = 1;
counter = 1;
triangular_number = 0;
The reason is that I am looking at the local variables as members of
an implict struct and that is the way I would write a struct, (I don't
ever define more than one member per line in a struct and usually
comment on what the member means on the same line.) I am thinking of
making the implicit struct overt. I could then initialize but I really
want to keep initialization in the code.
Given compiler control I could extend C to allow an assignment (not an
initialization
local_variables = {1, 0, 1, 0,};
Maybe this ia already a known extension.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-07-11 14:56 -0700 |
| Message-ID | <lnmvz2pdd0.fsf@kst-u.example.com> |
| In reply to | #65319 |
David Kleinecke <dkleinecke@gmail.com> writes:
[...]
> I have recently taken to writing this way - I repeat in my style:
>
> int main (void) {
> int n;
> int number;
> int triangular_number;
> int counter;
> #
> n = 1;
> counter = 1;
> triangular_number = 0;
The use of "#" (a null preprocessor directive) is odd. Perfectly legal,
but odd. I might use an ordinary comment like:
/******************/
> The reason is that I am looking at the local variables as members of
> an implict struct and that is the way I would write a struct, (I don't
> ever define more than one member per line in a struct and usually
> comment on what the member means on the same line.) I am thinking of
> making the implicit struct overt. I could then initialize but I really
> want to keep initialization in the code.
Individually declared objects can be can be initialized in place; struct
members can't. Thinking of a collection of objects as a struct might be
useful, but IMHO it's no reason to avoid the convenience of
in-place initialization.
> Given compiler control I could extend C to allow an assignment (not an
> initialization
> local_variables = {1, 0, 1, 0,};
> Maybe this ia already a known extension.
It's not an extension; it's a standard language feature, starting with
C99, called "compound literals".
local_variables = (struct foo){1, 0, 1, 0};
(Note that "(struct foo)" is not a cast; it's part of the syntax of the
compound literal.)
Of course if you insist on sticking to C90 you don't get to use things
like this.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | David Kleinecke <dkleinecke@gmail.com> |
|---|---|
| Date | 2015-07-12 09:32 -0700 |
| Message-ID | <7e623eb8-4156-443c-b637-ffe06d4e87de@googlegroups.com> |
| In reply to | #65348 |
On Saturday, July 11, 2015 at 2:56:52 PM UTC-7, Keith Thompson wrote:
> David Kleinecke <dkleinecke@gmail.com> writes:
> [...]
> > I have recently taken to writing this way - I repeat in my style:
> >
> > int main (void) {
> > int n;
> > int number;
> > int triangular_number;
> > int counter;
> > #
> > n = 1;
> > counter = 1;
> > triangular_number = 0;
>
> The use of "#" (a null preprocessor directive) is odd. Perfectly legal,
> but odd. I might use an ordinary comment like:
>
> /******************/
I want something that looks to the eye like a blank line but isn't.
A line like you show is too strong a divider. I want to write each
function without any genuine blank lines (for other reasons) but I
want to make a break equivalnt to a blank line. A single # resolves
this conflict nicely.
...
> > Given compiler control I could extend C to allow an assignment (not an
> > initialization
> > local_variables = {1, 0, 1, 0,};
> > Maybe this ia already a known extension.
>
> It's not an extension; it's a standard language feature, starting with
> C99, called "compound literals".
>
> local_variables = (struct foo){1, 0, 1, 0};
>
> (Note that "(struct foo)" is not a cast; it's part of the syntax of the
> compound literal.)
>
> Of course if you insist on sticking to C90 you don't get to use things
> like this.
I'll stay with C90.
That's an ugly syntax. Why is the non-cast necessary? What confict
is resolved?
> Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
> Working, but not speaking, for JetHead Development, Inc.
> "We must do something. This is something. Therefore, we must do this."
> -- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-07-12 13:19 -0400 |
| Message-ID | <55A2A1AF.8030406@verizon.net> |
| In reply to | #65416 |
On 07/12/2015 12:32 PM, David Kleinecke wrote:
> On Saturday, July 11, 2015 at 2:56:52 PM UTC-7, Keith Thompson wrote:
>> David Kleinecke <dkleinecke@gmail.com> writes:
...
>>> Given compiler control I could extend C to allow an assignment (not an
>>> initialization
>>> local_variables = {1, 0, 1, 0,};
>>> Maybe this ia already a known extension.
>>
>> It's not an extension; it's a standard language feature, starting with
>> C99, called "compound literals".
>>
>> local_variables = (struct foo){1, 0, 1, 0};
>>
>> (Note that "(struct foo)" is not a cast; it's part of the syntax of the
>> compound literal.)
>>
>> Of course if you insist on sticking to C90 you don't get to use things
>> like this.
>
> I'll stay with C90.
>
> That's an ugly syntax. Why is the non-cast necessary? What confict
> is resolved?
It's a special case use of feature with more general application. A
compound literal (6.5.2.5) has the form (type-name) {initializer-list},
and it creates a temporary unnamed object of the named type, initialized
using initializer list. In the general case, without the type-name, the
compiler would have no way of determining which type of object should be
initialized with those initializers. Keith's version of your code is
functionally equivalent to
struct foo _unnamed_temporary = {1, 0, 1, 0};
local_variables = _unnamed_temporary;
Note that while, in principle, this implies that storage is reserved for
two different struct foo objects, local_variables and
_unnamed_temporary, in practice a decent compiler could optimize the
extra storage away, since there's no way to refer to it after that line
of code is executed.
[toc] | [prev] | [next] | [standalone]
| From | David Kleinecke <dkleinecke@gmail.com> |
|---|---|
| Date | 2015-07-13 10:16 -0700 |
| Message-ID | <dfb7cf4f-783d-4359-9780-cc1ea07d5e3e@googlegroups.com> |
| In reply to | #65426 |
On Sunday, July 12, 2015 at 10:19:51 AM UTC-7, James Kuyper wrote:
> On 07/12/2015 12:32 PM, David Kleinecke wrote:
> > On Saturday, July 11, 2015 at 2:56:52 PM UTC-7, Keith Thompson wrote:
> >> David Kleinecke <dkleinecke@gmail.com> writes:
> ...
> >>> Given compiler control I could extend C to allow an assignment (not an
> >>> initialization
> >>> local_variables = {1, 0, 1, 0,};
> >>> Maybe this ia already a known extension.
> >>
> >> It's not an extension; it's a standard language feature, starting with
> >> C99, called "compound literals".
> >>
> >> local_variables = (struct foo){1, 0, 1, 0};
> >>
> >> (Note that "(struct foo)" is not a cast; it's part of the syntax of the
> >> compound literal.)
> >>
> >> Of course if you insist on sticking to C90 you don't get to use things
> >> like this.
> >
> > I'll stay with C90.
> >
> > That's an ugly syntax. Why is the non-cast necessary? What confict
> > is resolved?
>
> It's a special case use of feature with more general application. A
> compound literal (6.5.2.5) has the form (type-name) {initializer-list},
> and it creates a temporary unnamed object of the named type, initialized
> using initializer list. In the general case, without the type-name, the
> compiler would have no way of determining which type of object should be
> initialized with those initializers. Keith's version of your code is
> functionally equivalent to
>
> struct foo _unnamed_temporary = {1, 0, 1, 0};
> local_variables = _unnamed_temporary;
>
> Note that while, in principle, this implies that storage is reserved for
> two different struct foo objects, local_variables and
> _unnamed_temporary, in practice a decent compiler could optimize the
> extra storage away, since there's no way to refer to it after that line
> of code is executed.
Never seen that used so I am unable to comment. I was visualizing
a case where the aggregate was assigned to a variable of known
type - exactly as though the variable was being initialized.
As I understand it I could make such an extension to C because an
expression
x = { ...
will fail at the { and I am free to extend by giving it meaning.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-07-12 11:48 -0700 |
| Message-ID | <lntwt9mcuf.fsf@kst-u.example.com> |
| In reply to | #65416 |
David Kleinecke <dkleinecke@gmail.com> writes:
> On Saturday, July 11, 2015 at 2:56:52 PM UTC-7, Keith Thompson wrote:
>> David Kleinecke <dkleinecke@gmail.com> writes:
>> [...]
>> > I have recently taken to writing this way - I repeat in my style:
>> >
>> > int main (void) {
>> > int n;
>> > int number;
>> > int triangular_number;
>> > int counter;
>> > #
>> > n = 1;
>> > counter = 1;
>> > triangular_number = 0;
>>
>> The use of "#" (a null preprocessor directive) is odd. Perfectly legal,
>> but odd. I might use an ordinary comment like:
>>
>> /******************/
>
> I want something that looks to the eye like a blank line but isn't.
> A line like you show is too strong a divider. I want to write each
> function without any genuine blank lines (for other reasons) but I
> want to make a break equivalnt to a blank line. A single # resolves
> this conflict nicely.
Ok. It's not what I'd do. I might use "/**/", or "//" if I didn't
insist on C90. I personally find your use of "#" a little jarring; null
directives are rarely used in practice, and I had to check the standard
to verify that a null directive does nothing.
Actually I'd just use a blank line. You didn't say why you don't want
to do that; my best guess is that you want to use your editor's
paragraph movement commands. You might investigate whether your editor
has commands to jump past C functions (vi/vim has "[[" and "]]").
But as I said, it's perfectly legal.
> ...
>
>> > Given compiler control I could extend C to allow an assignment (not an
>> > initialization
>> > local_variables = {1, 0, 1, 0,};
>> > Maybe this ia already a known extension.
>>
>> It's not an extension; it's a standard language feature, starting with
>> C99, called "compound literals".
>>
>> local_variables = (struct foo){1, 0, 1, 0};
>>
>> (Note that "(struct foo)" is not a cast; it's part of the syntax of the
>> compound literal.)
>>
>> Of course if you insist on sticking to C90 you don't get to use things
>> like this.
>
> I'll stay with C90.
>
> That's an ugly syntax. Why is the non-cast necessary? What confict
> is resolved?
The type is necessary because without it the type of {1, 0, 1, 0}
would be indeterminate -- unless the type is inferred from the
context, but C expressions in general don't work that way. Enclosing
the type in parentheses is, I presume, deliberately reminiscent of
cast syntax, and the stuff between the braces is explicitly defined
in the grammar as an initializer-list. I don't disagree with your
statement that it's an ugly syntax, but it doesn't bother me much.
What alternative syntax would you suggest?
In any case, my main point stands; abandoning the advantage
of in-place initialization because you think of a collection of
local object declarations as a structure doesn't make much sense.
If you want to make it an actual structure, you can initialize
the structure.
(David, the following is unrelated to C90; you might want to stop
reading now.)
Incidentally, one thing I dislike about compound literals is that
the storage duration of the object depends only on the context in
which it appears. A string literal always creates an array object
with static storage duration. A compound literal creates an object
with static storage duration if it occurs outside a function,
or with automatic storage duration if it occurs inside a function.
It doesn't matter for the case we're discussing, but there are times
it would be convenient to have a block-scope compound literal that
refers to a static object.
I might suggest an enhancement that permits something like:
(static struct foo){1, 0, 1, 0};
at block scope. (Of course non-constant expressions would not
be permitted.)
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2015-07-12 15:23 -0400 |
| Message-ID | <55A2BE9E.1020707@verizon.net> |
| In reply to | #65440 |
On 07/12/2015 02:48 PM, Keith Thompson wrote:
> David Kleinecke <dkleinecke@gmail.com> writes:
>> On Saturday, July 11, 2015 at 2:56:52 PM UTC-7, Keith Thompson wrote:
>>> David Kleinecke <dkleinecke@gmail.com> writes:
...
>>>> Given compiler control I could extend C to allow an assignment (not an
>>>> initialization
>>>> local_variables = {1, 0, 1, 0,};
>>>> Maybe this ia already a known extension.
>>>
>>> It's not an extension; it's a standard language feature, starting with
>>> C99, called "compound literals".
>>>
>>> local_variables = (struct foo){1, 0, 1, 0};
>>>
>>> (Note that "(struct foo)" is not a cast; it's part of the syntax of the
>>> compound literal.)
>>>
>>> Of course if you insist on sticking to C90 you don't get to use things
>>> like this.
>>
>> I'll stay with C90.
>>
>> That's an ugly syntax. Why is the non-cast necessary? What confict
>> is resolved?
>
> The type is necessary because without it the type of {1, 0, 1, 0}
> would be indeterminate -- unless the type is inferred from the
> context, but C expressions in general don't work that way. Enclosing
> the type in parentheses is, I presume, deliberately reminiscent of
> cast syntax, and the stuff between the braces is explicitly defined
> in the grammar as an initializer-list. I don't disagree with your
> statement that it's an ugly syntax, but it doesn't bother me much.
>
> What alternative syntax would you suggest?
He's already given it, at the top of the quoted material above:
>>>> local_variables = {1, 0, 1, 0,};
Keep in mind that this is not alternative syntax for compound literals.
It's syntax currently allowed only in initialization, re-purposed for
assignment.
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.c
csiph-web