Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #9730 > unrolled thread
| Started by | Jack <spamtrap@trashcan.invalid> |
|---|---|
| First post | 2011-08-30 21:01 +0000 |
| Last post | 2011-09-02 08:32 -0700 |
| Articles | 20 on this page of 67 — 20 participants |
Back to article view | Back to comp.lang.c
I think this is an impossible assignment...could you help? Jack <spamtrap@trashcan.invalid> - 2011-08-30 21:01 +0000
Re: I think this is an impossible assignment...could you help? Ian Collins <ian-news@hotmail.com> - 2011-08-31 09:18 +1200
Re: I think this is an impossible assignment...could you help? Jack <spamtrap@trashcan.invalid> - 2011-08-30 21:20 +0000
Re: I think this is an impossible assignment...could you help? Ian Collins <ian-news@hotmail.com> - 2011-08-31 10:34 +1200
Re: I think this is an impossible assignment...could you help? John Gordon <gordon@panix.com> - 2011-08-30 21:29 +0000
Re: I think this is an impossible assignment...could you help? Jack <spamtrap@trashcan.invalid> - 2011-08-30 21:43 +0000
Re: I think this is an impossible assignment...could you help? John Gordon <gordon@panix.com> - 2011-08-30 22:16 +0000
Re: I think this is an impossible assignment...could you help? William Ahern <william@wilbur.25thandClement.com> - 2011-08-30 16:26 -0700
Re: I think this is an impossible assignment...could you help? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-30 21:44 -0400
Re: I think this is an impossible assignment...could you help? John Gordon <gordon@panix.com> - 2011-08-31 02:03 +0000
Re: I think this is an impossible assignment...could you help? Keith Thompson <kst-u@mib.org> - 2011-08-31 08:56 -0700
Re: I think this is an impossible assignment...could you help? John Gordon <gordon@panix.com> - 2011-08-31 16:15 +0000
Re: I think this is an impossible assignment...could you help? Keith Thompson <kst-u@mib.org> - 2011-08-31 09:20 -0700
Re: I think this is an impossible assignment...could you help? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-08-31 21:15 -0400
Re: I think this is an impossible assignment...could you help? James Kuyper <jameskuyper@verizon.net> - 2011-08-31 22:18 -0400
Re: I think this is an impossible assignment...could you help? blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-09-01 15:06 +0000
Re: I think this is an impossible assignment...could you help? blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-09-01 15:04 +0000
Re: I think this is an impossible assignment...could you help? John Gordon <gordon@panix.com> - 2011-09-01 15:20 +0000
Re: I think this is an impossible assignment...could you help? David Thompson <dave.thompson2@verizon.net> - 2011-09-06 01:20 -0400
Re: I think this is an impossible assignment...could you help? Keith Thompson <kst-u@mib.org> - 2011-08-30 16:37 -0700
Re: I think this is an impossible assignment...could you help? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-08-31 00:51 +0100
Re: I think this is an impossible assignment...could you help? blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-08-31 13:28 +0000
Re: I think this is an impossible assignment...could you help? blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-09-01 15:03 +0000
Re: I think this is an impossible assignment...could you help? Keith Thompson <kst-u@mib.org> - 2011-09-01 08:49 -0700
Re: I think this is an impossible assignment...could you help? blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-09-02 07:46 +0000
Re: I think this is an impossible assignment...could you help? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-01 21:30 -0400
Re: I think this is an impossible assignment...could you help? blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-09-02 07:48 +0000
Re: I think this is an impossible assignment...could you help? Ian Collins <ian-news@hotmail.com> - 2011-08-31 11:58 +1200
Re: I think this is an impossible assignment...could you help? Ike Naar <ike@sverige.freeshell.org> - 2011-08-31 07:27 +0000
Re: I think this is an impossible assignment...could you help? "BartC" <bc@freeuk.com> - 2011-08-31 10:37 +0100
Re: I think this is an impossible assignment...could you help? blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-08-31 13:29 +0000
Re: I think this is an impossible assignment...could you help? "BartC" <bc@freeuk.com> - 2011-08-31 18:41 +0100
Re: I think this is an impossible assignment...could you help? blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-09-01 15:05 +0000
Re: I think this is an impossible assignment...could you help? Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-09-02 01:42 -0700
Re: I think this is an impossible assignment...could you help? Keith Thompson <kst-u@mib.org> - 2011-08-31 09:15 -0700
Re: I think this is an impossible assignment...could you help? "BartC" <bc@freeuk.com> - 2011-08-31 18:36 +0100
Re: I think this is an impossible assignment...could you help? Keith Thompson <kst-u@mib.org> - 2011-08-31 11:44 -0700
Re: I think this is an impossible assignment...could you help? blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-09-01 15:05 +0000
Re: I think this is an impossible assignment...could you help? Keith Thompson <kst-u@mib.org> - 2011-09-01 08:51 -0700
Re: I think this is an impossible assignment...could you help? "BartC" <bc@freeuk.com> - 2011-09-01 17:26 +0100
Re: I think this is an impossible assignment...could you help? blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-09-02 07:47 +0000
Re: I think this is an impossible assignment...could you help? "BartC" <bc@freeuk.com> - 2011-09-02 10:56 +0100
Re: I think this is an impossible assignment...could you help? blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> - 2011-09-05 20:21 +0000
Re: I think this is an impossible assignment...could you help? "Bartc" <bc@freeuk.com> - 2011-09-09 13:59 +0100
Re: I think this is an impossible assignment...could you help? "BartC" <bc@freeuk.com> - 2011-09-02 10:43 +0100
Re: I think this is an impossible assignment...could you help? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-09-01 12:02 -0600
Re: I think this is an impossible assignment...could you help? James Kuyper <jameskuyper@verizon.net> - 2011-09-01 15:12 -0400
Re: I think this is an impossible assignment...could you help? sandeep <nospam@nospam.com> - 2011-09-01 19:36 +0000
Re: I think this is an impossible assignment...could you help? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-09-01 14:53 -0600
Re: I think this is an impossible assignment...could you help? Keith Thompson <kst-u@mib.org> - 2011-09-01 16:01 -0700
Re: I think this is an impossible assignment...could you help? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-09-01 17:22 -0600
Re: I think this is an impossible assignment...could you help? pete <pfiland@mindspring.com> - 2011-09-01 19:39 -0400
Re: I think this is an impossible assignment...could you help? Keith Thompson <kst-u@mib.org> - 2011-09-01 16:39 -0700
Re: I think this is an impossible assignment...could you help? Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2011-09-01 18:51 -0600
Re: I think this is an impossible assignment...could you help? pete <pfiland@mindspring.com> - 2011-09-01 22:27 -0400
Re: I think this is an impossible assignment...could you help? James Kuyper <jameskuyper@verizon.net> - 2011-09-01 22:53 -0400
Re: I think this is an impossible assignment...could you help? Kenneth Brody <kenbrody@spamcop.net> - 2011-09-02 10:45 -0400
Re: I think this is an impossible assignment...could you help? James Kuyper <jameskuyper@verizon.net> - 2011-09-02 11:02 -0400
Re: I think this is an impossible assignment...could you help? Keith Thompson <kst-u@mib.org> - 2011-09-02 08:05 -0700
Re: I think this is an impossible assignment...could you help? Keith Thompson <kst-u@mib.org> - 2011-09-02 08:17 -0700
Re: I think this is an impossible assignment...could you help? Willem <willem@toad.stack.nl> - 2011-09-02 15:18 +0000
Re: I think this is an impossible assignment...could you help? Keith Thompson <kst-u@mib.org> - 2011-09-02 08:42 -0700
Re: I think this is an impossible assignment...could you help? James Kuyper <jameskuyper@verizon.net> - 2011-09-01 23:21 -0400
Re: I think this is an impossible assignment...could you help? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-01 21:53 -0400
Re: I think this is an impossible assignment...could you help? James Kuyper <jameskuyper@verizon.net> - 2011-09-01 23:09 -0400
Re: I think this is an impossible assignment...could you help? Michael Press <rubrum@pacbell.net> - 2011-09-01 21:43 -0700
Re: I think this is an impossible assignment...could you help? Keith Thompson <kst-u@mib.org> - 2011-09-02 08:32 -0700
Page 2 of 4 — ← Prev page 1 [2] 3 4 Next page →
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2011-08-31 00:51 +0100 |
| Message-ID | <0.158a1dd7530c17677894.20110831005123BST.87zkiq791g.fsf@bsb.me.uk> |
| In reply to | #9734 |
Jack <spamtrap@trashcan.invalid> writes:
> On Tue, 30 Aug 2011 21:29:25 +0000, John Gordon wrote:
>
>> In <j3jj2t$eq3$1@speranza.aioe.org> Jack <spamtrap@trashcan.invalid>
>> writes:
>>
>>> Ok, here's the deal...my instructor wants us to do the following
>>> program. Only one problem: what must I use to get this program to
>>> work right? Do I use combinations of if, for and while statements, or
>>> just if statements or what? I have tried nested if and while
>>> statements and CANNOT get the program to execute properly... Perhaps
>>> an experienced programmer(unlike myself) could give me a little insight
>>> on how to do this problem...
>>
>> A good start would be to show us what you wrote. Perhaps we can explain
>> where you went wrong.
>
> John --- Sure. Thought this forum was empty for a while, good to get a
> reply at last. I'd like to fix this up by 4PM today!
A reply was posted with in half an hour. That's about as good as you
can expect from Usenet (you may be reading this as a web forum but it is
really a Usenet news group).
> here is the code I have already written...maybe you could
> expand to it a little?
>
> long scan_int(void)
> {
> int c ; /* character, nb type is int not char to allow EOF */
> int sign='+'; /* + for positive number, - for negative */
> long sum=0; /* stores sum */
>
> while((c=getchar())==' '); /* whitespace delimeters */
So far so good but do you really find that easy to read? I'd write it
like this:
long scan_int(void)
{
int c; /* character, nb type is int not char to allow EOF */
int sign = '+'; /* + for positive number, - for negative */
long sum = 0; /* stores sum */
while ((c = getchar()) == ' '); /* whitespace delimeters */
Note the indentation and extra spaces. They get even more significant
later.
On a more important note, ' ' is not the only white space. Look up the
function in the header ctype.h (isspace, etc.) or, if you think you
should not use these yet, write a more complex test.
> if(c=='-') /* negative */
> {sign='-';
> c=getchar();
> while((c=getchar())==' ');
> if(c<'0'||c>'9')
> return(INVALID);}
>
> else if(c=='+') /* positive */
> {sign='+';
> c=getchar();
> while((c=getchar())==' ');
> if(c<'0'||c>'9')
> return(INVALID);}
I find this very hard to read all jammed-up like that. Do you really
want to accept input like this: " + 99"? scanf's %d format does not
so I suspect you should not do so either.
> if(c<'0'||c>'9')
> {while(c!=' ') /* space delimeter */
> c=getchar();
Are you sure this loop will end? Here's a tip: every time you write a
loop ask if will always end. If you think it will try to prove yourself
*wrong*. If you can't find any way to make the loop go on forever, you
may be safe!
> return(INVALID);}
In general, input functions should not read stuff that they don't
understand. If there is non-numeric data you should probably leave it
alone. Unfortunately you are hampered by a terrible specification. If
you do as you are told (emulate scanf and use the given driver) you can
not test non-numeric input because it will be left unread.
> if(c=='-'||c=='+')
> return INVALID;
>
> if(c>='0'&&c<='9')
> {while(c>='0'&&c<='9')
> {sum=sum*10+(c-48); /* update sum */
48? Hint: what does this do: printf("%d\n", '0'); ?
> c=getchar();}
> }
>
> else return(INVALID);
You are very nearly home and dry.
Once you've tidied it up, I think you will find that you can remove a
few of the invalid return tests. It's tempting to return an error as
soon as you know something is wrong, but the code is often easier to
follow if you have fewer tests and later ones will often catch earlier
errors.
> if(sign=='-')
> sum=sum*(-1);
> return(sum);}
You can set sign to anything you like as soon as you see the + or the -
in the input. Given that freedom, can you think of something to set it
to that would simplify this last bit if code?
I've likely missed some tings because the code is all crushed up and not
easy for me to read, but it looks pretty close to done.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> |
|---|---|
| Date | 2011-08-31 13:28 +0000 |
| Message-ID | <9c6r79Fu1rU2@mid.individual.net> |
| In reply to | #9741 |
In article <0.158a1dd7530c17677894.20110831005123BST.87zkiq791g.fsf@bsb.me.uk>,
Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:
> Jack <spamtrap@trashcan.invalid> writes:
>
> > On Tue, 30 Aug 2011 21:29:25 +0000, John Gordon wrote:
> >
> >> In <j3jj2t$eq3$1@speranza.aioe.org> Jack <spamtrap@trashcan.invalid>
> >> writes:
[ snip ]
> > long scan_int(void)
> > {
> > int c ; /* character, nb type is int not char to allow EOF */
> > int sign='+'; /* + for positive number, - for negative */
> > long sum=0; /* stores sum */
Ben, even for beginner programs this level of commenting seems
excessive. The comment about the type of c is not amiss in a
beginner program, but -- what information does "stores sum" provide
that the variable name doesn't?? (I notice that I am not the only
person to comment on this, um, comment.)
[ snip ]
> Do you really
> want to accept input like this: " + 99"? scanf's %d format does not
> so I suspect you should not do so either.
But it seems apparent from the example execution that this function
is not supposed to behave *exactly* like scanf, and in fact the
overall description says that the desired function will "approximate"
the behavior of scanf (and apparently the treatment of errors is one
reason to say "approximate" rather than "emulate").
[ snip ]
> In general, input functions should not read stuff that they don't
> understand. If there is non-numeric data you should probably leave it
> alone. Unfortunately you are hampered by a terrible specification. If
> you do as you are told (emulate scanf and use the given driver) you can
> not test non-numeric input because it will be left unread.
I guess it all depends on what you think "approximate" means. :-)
See above.
Another problem with the specification, IMO anyway, is that it
disallows a legitimate "int" value, namely -2147483648 (0x8000000).
Then again, a function that takes no arguments and returns an int --
well, the options for signalling that an error has occurred do seem
kind of limited.
[ snip ]
--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
[toc] | [prev] | [next] | [standalone]
| From | blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> |
|---|---|
| Date | 2011-09-01 15:03 +0000 |
| Message-ID | <9c9l6bF6f6U1@mid.individual.net> |
| In reply to | #9757 |
In article <9c6r79Fu1rU2@mid.individual.net>, blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> wrote: > In article <0.158a1dd7530c17677894.20110831005123BST.87zkiq791g.fsf@bsb.me.uk>, > Ben Bacarisse <ben.usenet@bsb.me.uk> wrote: > > Jack <spamtrap@trashcan.invalid> writes: > > > > > On Tue, 30 Aug 2011 21:29:25 +0000, John Gordon wrote: > > > > > >> In <j3jj2t$eq3$1@speranza.aioe.org> Jack <spamtrap@trashcan.invalid> > > >> writes: > > [ snip ] > > > > long scan_int(void) [ snip ] > Another problem with the specification, IMO anyway, is that it > disallows a legitimate "int" value, namely -2147483648 (0x8000000). > Then again, a function that takes no arguments and returns an int -- > well, the options for signalling that an error has occurred do seem > kind of limited. Correction: First, why am I talking about int values when this function returns a long? (What was I thinking.) Also, 0x8000000 might be valid long on some systems, but that's not guaranteed. (I'm not sure I'd have known that, but someone else pointed it out, and I looked up the standard, and indeed the lower end of the required range for longs is -(2**31-1), not -2**31 as I thought.) -- B. L. Massingill ObDisclaimer: I don't speak for my employers; they return the favor.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2011-09-01 08:49 -0700 |
| Message-ID | <lnk49ss1nm.fsf@nuthaus.mib.org> |
| In reply to | #9813 |
blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> writes:
[...]
> Also, 0x8000000 might be valid long on some systems, but that's not
> guaranteed. (I'm not sure I'd have known that, but someone else
> pointed it out, and I looked up the standard, and indeed the lower
> end of the required range for longs is -(2**31-1), not -2**31 as
> I thought.)
0x80000000 is a positive value, equal to 2**31. It's not a valid
long value on any system where long is 32 bits (and LONG_MAX is at
most 2**31-1).
It commonly *converts* to a valid long value, -2**31 (ULONG_MIN),
but the result of the conversion is implementation-defined.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> |
|---|---|
| Date | 2011-09-02 07:46 +0000 |
| Message-ID | <9cbfv1FftvU1@mid.individual.net> |
| In reply to | #9823 |
In article <lnk49ss1nm.fsf@nuthaus.mib.org>, Keith Thompson <kst-u@mib.org> wrote: > blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> writes: > [...] > > Also, 0x8000000 might be valid long on some systems, but that's not > > guaranteed. (I'm not sure I'd have known that, but someone else > > pointed it out, and I looked up the standard, and indeed the lower > > end of the required range for longs is -(2**31-1), not -2**31 as > > I thought.) > > 0x80000000 is a positive value, equal to 2**31. It's not a valid > long value on any system where long is 32 bits (and LONG_MAX is at > most 2**31-1). > > It commonly *converts* to a valid long value, -2**31 (ULONG_MIN), > but the result of the conversion is implementation-defined. More of the pedantry that so endears this group to me. (No sarcasm intended. :-) ? ) -- B. L. Massingill ObDisclaimer: I don't speak for my employers; they return the favor.
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-09-01 21:30 -0400 |
| Message-ID | <j3pbjj$99n$1@dont-email.me> |
| In reply to | #9813 |
On 9/1/2011 11:03 AM, blmblm@myrealbox.com wrote:
>[...]
> Also, 0x8000000 might be valid long on some systems, but that's not
> guaranteed.
0x8000000 *is* a valid long, on *all* systems. (But the
original's 0x80000000 count-the-zeroes-carefully might not be.)
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> |
|---|---|
| Date | 2011-09-02 07:48 +0000 |
| Message-ID | <9cbg2gFftvU3@mid.individual.net> |
| In reply to | #9858 |
In article <j3pbjj$99n$1@dont-email.me>, Eric Sosman <esosman@ieee-dot-org.invalid> wrote: > On 9/1/2011 11:03 AM, blmblm@myrealbox.com wrote: > >[...] > > Also, 0x8000000 might be valid long on some systems, but that's not > > guaranteed. > > 0x8000000 *is* a valid long, on *all* systems. (But the > original's 0x80000000 count-the-zeroes-carefully might not be.) > Good catch. :-) -- B. L. Massingill ObDisclaimer: I don't speak for my employers; they return the favor.
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-08-31 11:58 +1200 |
| Message-ID | <9c5bpvF5dgU4@mid.individual.net> |
| In reply to | #9734 |
On 08/31/11 09:43 AM, Jack wrote:
> On Tue, 30 Aug 2011 21:29:25 +0000, John Gordon wrote:
>
>> In<j3jj2t$eq3$1@speranza.aioe.org> Jack<spamtrap@trashcan.invalid>
>> writes:
>>
>>> Ok, here's the deal...my instructor wants us to do the following
>>> program. Only one problem: what must I use to get this program to
>>> work right? Do I use combinations of if, for and while statements, or
>>> just if statements or what? I have tried nested if and while
>>> statements and CANNOT get the program to execute properly... Perhaps
>>> an experienced programmer(unlike myself) could give me a little insight
>>> on how to do this problem...
>>
>> A good start would be to show us what you wrote. Perhaps we can explain
>> where you went wrong.
>
> John --- Sure. Thought this forum was empty for a while, good to get a
> reply at last. I'd like to fix this up by 4PM today!
>
> here is the code I have already written...maybe you could
> expand to it a little?
>
> long scan_int(void)
> {
> int c ; /* character, nb type is int not char to allow EOF */
> int sign='+'; /* + for positive number, - for negative */
> long sum=0; /* stores sum */
In addition to the other comments, most of those comments would be
considered excessive, especially the last one!
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Ike Naar <ike@sverige.freeshell.org> |
|---|---|
| Date | 2011-08-31 07:27 +0000 |
| Message-ID | <slrn3vfsj5rojr.h3h.ike@sverige.freeshell.org> |
| In reply to | #9734 |
On 2011-08-30, Jack <spamtrap@trashcan.invalid> wrote:
> if(c=='-') /* negative */
> {sign='-';
> c=getchar();
> while((c=getchar())==' ');
There's one getchar() too many. You discard the character
after the '-' sign without ever looking at it.
E.g. in the input string "-42" the '4' is discarded.
The same happens in the code fragment for '+'.
[toc] | [prev] | [next] | [standalone]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Date | 2011-08-31 10:37 +0100 |
| Message-ID | <j3kvds$ihr$1@dont-email.me> |
| In reply to | #9730 |
"Jack" <spamtrap@trashcan.invalid> wrote in message
news:j3jj2t$eq3$1@speranza.aioe.org...
> #include
> #define INVALID 0x80000000
>
> long scan_int(void); /* the prototype */
>
> int main(void)
> {long num, i;
> printf("\nenter an integer: ");
> for(i=1; (num = scan_int()) != -999; printf("\n\n\nenter an integer: "))
> { if (num == INVALID)
> printf("\n you entered an invalid sequence\n");
> else
> {
> printf("\nthe %ld-th integer read was %ld\n", i, num);
> ++i;
> }
> }
> return (0);
>
> }
>
> Use this main() as a driver. You write function
> long scan_int(void);
You may or may not get extra marks by submitting an alternate version of
main() that is easier on the eye. For example:
#include <stdio.h>
#define INVALID 0x80000000
#define ENDCODE -999
long scan_int(void); /* the prototype */
int main(void) {
long num, i=0;
while (1) {
printf("\nEnter an integer (%d to end): ",ENDCODE);
num = scan_int();
if (num == ENDCODE)
return(0);
else if (num == INVALID)
printf("\n You entered an invalid sequence\n");
else
printf("\nThe %ldth integer read was %ld\n", i++, num);
}
}
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> |
|---|---|
| Date | 2011-08-31 13:29 +0000 |
| Message-ID | <9c6r8sFu1rU3@mid.individual.net> |
| In reply to | #9752 |
In article <j3kvds$ihr$1@dont-email.me>, BartC <bc@freeuk.com> wrote:
> "Jack" <spamtrap@trashcan.invalid> wrote in message
> news:j3jj2t$eq3$1@speranza.aioe.org...
>
> > #include
> > #define INVALID 0x80000000
> >
> > long scan_int(void); /* the prototype */
> >
> > int main(void)
> > {long num, i;
> > printf("\nenter an integer: ");
> > for(i=1; (num = scan_int()) != -999; printf("\n\n\nenter an integer: "))
> > { if (num == INVALID)
> > printf("\n you entered an invalid sequence\n");
> > else
> > {
> > printf("\nthe %ld-th integer read was %ld\n", i, num);
> > ++i;
> > }
> > }
> > return (0);
> >
> > }
> >
> > Use this main() as a driver. You write function
> > long scan_int(void);
>
> You may or may not get extra marks by submitting an alternate version of
> main() that is easier on the eye. For example:
"May or may not" indeed. Sort of a :-) here. Some instructors
respond better than others to students "improving" (or actually
improving) their code.
> #include <stdio.h>
>
> #define INVALID 0x80000000
> #define ENDCODE -999
>
> long scan_int(void); /* the prototype */
>
> int main(void) {
> long num, i=0;
>
> while (1) {
Normally I'd vote for putting the assignment to num in the loop
condition, but in this case your approach avoids duplicating the
prompt, so -- a win, maybe.
> printf("\nEnter an integer (%d to end): ",ENDCODE);
IMO changing the prompt to include the stop value is quite an
improvement, and I wonder why the original program didn't!
> num = scan_int();
>
> if (num == ENDCODE)
> return(0);
> else if (num == INVALID)
> printf("\n You entered an invalid sequence\n");
> else
> printf("\nThe %ldth integer read was %ld\n", i++, num);
> }
> }
--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
[toc] | [prev] | [next] | [standalone]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Date | 2011-08-31 18:41 +0100 |
| Message-ID | <j3lrq2$isk$2@dont-email.me> |
| In reply to | #9758 |
"blmblm@myrealbox.com" <blmblm.myrealbox@gmail.com> wrote in message
news:9c6r8sFu1rU3@mid.individual.net...
> In article <j3kvds$ihr$1@dont-email.me>, BartC <bc@freeuk.com> wrote:
>> You may or may not get extra marks by submitting an alternate version of
>> main() that is easier on the eye. For example:
>
> "May or may not" indeed. Sort of a :-) here. Some instructors
> respond better than others to students "improving" (or actually
> improving) their code.
The OP will know their instructor better than we do. However the original
code looked more like it had been written by a student..
>> printf("\nEnter an integer (%d to end): ",ENDCODE);
>
> IMO changing the prompt to include the stop value is quite an
> improvement, and I wonder why the original program didn't!
This is necessary otherwise the 'user' will have no idea how to stop the
program.
>> printf("\nThe %ldth integer read was %ld\n", i++, num);
Perhaps ++i here (or start i at 1), as the original counted from 1.
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> |
|---|---|
| Date | 2011-09-01 15:05 +0000 |
| Message-ID | <9c9la8F6f6U4@mid.individual.net> |
| In reply to | #9773 |
In article <j3lrq2$isk$2@dont-email.me>, BartC <bc@freeuk.com> wrote:
> "blmblm@myrealbox.com" <blmblm.myrealbox@gmail.com> wrote in message
> news:9c6r8sFu1rU3@mid.individual.net...
> > In article <j3kvds$ihr$1@dont-email.me>, BartC <bc@freeuk.com> wrote:
>
> >> You may or may not get extra marks by submitting an alternate version of
> >> main() that is easier on the eye. For example:
> >
> > "May or may not" indeed. Sort of a :-) here. Some instructors
> > respond better than others to students "improving" (or actually
> > improving) their code.
>
> The OP will know their instructor better than we do. However the original
> code looked more like it had been written by a student..
To me the original code for main appeared to be part of the
assignment -- it's introduced by that assignment-speak phrase
"Consider the following implementation of main". That doesn't
preclude its being written by some student, but I'm guessing the
author is not the OP.
> >> printf("\nEnter an integer (%d to end): ",ENDCODE);
> >
> > IMO changing the prompt to include the stop value is quite an
> > improvement, and I wonder why the original program didn't!
>
> This is necessary otherwise the 'user' will have no idea how to stop the
> program.
Exactly.
> >> printf("\nThe %ldth integer read was %ld\n", i++, num);
>
> Perhaps ++i here (or start i at 1), as the original counted from 1.
Yes. Good catch (of a mistake in your revision?).
--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
[toc] | [prev] | [next] | [standalone]
| From | Nick Keighley <nick_keighley_nospam@hotmail.com> |
|---|---|
| Date | 2011-09-02 01:42 -0700 |
| Message-ID | <390bab0e-66d2-42f3-bdec-30fbda0e49af@a13g2000yqd.googlegroups.com> |
| In reply to | #9758 |
On Aug 31, 2:29 pm, blm...@myrealbox.com <blmblm.myreal...@gmail.com> wrote: > > You may or may not get extra marks by submitting an alternate version of > > main() that is easier on the eye. For example: > > "May or may not" indeed. Sort of a :-) here. Some instructors > respond better than others to students "improving" (or actually > improving) their code. I passed (with a good mark) a comp sci exam in which I started an answer to a question with:- "the question you meant to ask was...". The question as posed was nonsense (and it was the eam setter's second attempt at framing it, I think a student who appealled against a bad mark would have had a good case)
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2011-08-31 09:15 -0700 |
| Message-ID | <lnmxeptv4b.fsf@nuthaus.mib.org> |
| In reply to | #9752 |
"BartC" <bc@freeuk.com> writes:
> "Jack" <spamtrap@trashcan.invalid> wrote in message
> news:j3jj2t$eq3$1@speranza.aioe.org...
>
>> #include
>> #define INVALID 0x80000000
[...]
>> {long num, i;
[...]
>> { if (num == INVALID)
If long is 32 bits, then 0x80000000 is outside the range of long.
How about
#define INVALID LONG_MIN
?
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Date | 2011-08-31 18:36 +0100 |
| Message-ID | <j3lrq1$isk$1@dont-email.me> |
| In reply to | #9764 |
"Keith Thompson" <kst-u@mib.org> wrote in message
news:lnmxeptv4b.fsf@nuthaus.mib.org...
> "BartC" <bc@freeuk.com> writes:
>> "Jack" <spamtrap@trashcan.invalid> wrote in message
>> news:j3jj2t$eq3$1@speranza.aioe.org...
>>
>>> #include
>>> #define INVALID 0x80000000
> [...]
>>> {long num, i;
> [...]
>>> { if (num == INVALID)
>
> If long is 32 bits, then 0x80000000 is outside the range of long.
> How about
>
> #define INVALID LONG_MIN
> ?
I always think of 'long' as unsigned (because outside C, that's how I use
it), and I didn't see that. Similarly when I saw LONG_MIN I immediately
thought zero!
But yes that would be a better signalling value.
--
Bartc
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2011-08-31 11:44 -0700 |
| Message-ID | <lnaaapto7v.fsf@nuthaus.mib.org> |
| In reply to | #9772 |
"BartC" <bc@freeuk.com> writes:
> "Keith Thompson" <kst-u@mib.org> wrote in message
> news:lnmxeptv4b.fsf@nuthaus.mib.org...
>> "BartC" <bc@freeuk.com> writes:
>>> "Jack" <spamtrap@trashcan.invalid> wrote in message
>>> news:j3jj2t$eq3$1@speranza.aioe.org...
>>>
>>>> #include
>>>> #define INVALID 0x80000000
>> [...]
>>>> {long num, i;
>> [...]
>>>> { if (num == INVALID)
>>
>> If long is 32 bits, then 0x80000000 is outside the range of long.
>> How about
>>
>> #define INVALID LONG_MIN
>> ?
>
> I always think of 'long' as unsigned (because outside C, that's how I
> use it), and I didn't see that. Similarly when I saw LONG_MIN I
> immediately thought zero!
>
> But yes that would be a better signalling value.
(You know about "unsigned long", right?)
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> |
|---|---|
| Date | 2011-09-01 15:05 +0000 |
| Message-ID | <9c9l8uF6f6U3@mid.individual.net> |
| In reply to | #9764 |
In article <lnmxeptv4b.fsf@nuthaus.mib.org>,
Keith Thompson <kst-u@mib.org> wrote:
> "BartC" <bc@freeuk.com> writes:
> > "Jack" <spamtrap@trashcan.invalid> wrote in message
> > news:j3jj2t$eq3$1@speranza.aioe.org...
> >
> >> #include
> >> #define INVALID 0x80000000
> [...]
> >> {long num, i;
> [...]
> >> { if (num == INVALID)
>
> If long is 32 bits, then 0x80000000 is outside the range of long.
> How about
>
> #define INVALID LONG_MIN
> ?
And then you disallow one legitimate input .... Well, I guess
the options for signalling errors are limited. <shrug>?
--
B. L. Massingill
ObDisclaimer: I don't speak for my employers; they return the favor.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2011-09-01 08:51 -0700 |
| Message-ID | <lnfwkgs1k0.fsf@nuthaus.mib.org> |
| In reply to | #9815 |
blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> writes:
> In article <lnmxeptv4b.fsf@nuthaus.mib.org>,
> Keith Thompson <kst-u@mib.org> wrote:
>> "BartC" <bc@freeuk.com> writes:
>> > "Jack" <spamtrap@trashcan.invalid> wrote in message
>> > news:j3jj2t$eq3$1@speranza.aioe.org...
>> >
>> >> #include
>> >> #define INVALID 0x80000000
>> [...]
>> >> {long num, i;
>> [...]
>> >> { if (num == INVALID)
>>
>> If long is 32 bits, then 0x80000000 is outside the range of long.
>> How about
>>
>> #define INVALID LONG_MIN
>> ?
>
> And then you disallow one legitimate input .... Well, I guess
> the options for signalling errors are limited. <shrug>?
So I can't enter -2147483648. Oh, darn. 8-)}
In real life, checking for end-of-file would make a lot more sense, but
this is a beginner's assignment.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | "BartC" <bc@freeuk.com> |
|---|---|
| Date | 2011-09-01 17:26 +0100 |
| Message-ID | <j3obqu$l0f$1@dont-email.me> |
| In reply to | #9824 |
"Keith Thompson" <kst-u@mib.org> wrote in message news:lnfwkgs1k0.fsf@nuthaus.mib.org... > blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> writes: >> In article <lnmxeptv4b.fsf@nuthaus.mib.org>, >> Keith Thompson <kst-u@mib.org> wrote: >>> #define INVALID LONG_MIN >> And then you disallow one legitimate input .... Well, I guess >> the options for signalling errors are limited. <shrug>? > > So I can't enter -2147483648. Oh, darn. 8-)} > > In real life, checking for end-of-file would make a lot more sense, but > this is a beginner's assignment. I think he meant the -999 that is used to stop the program... (If it was my specification, I might have just used an empty line to end. However this is after all just an exercise.) -- Bartc
[toc] | [prev] | [next] | [standalone]
Page 2 of 4 — ← Prev page 1 [2] 3 4 Next page →
Back to top | Article view | comp.lang.c
csiph-web