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 1 of 4 [1] 2 3 4 Next page →
| From | Jack <spamtrap@trashcan.invalid> |
|---|---|
| Date | 2011-08-30 21:01 +0000 |
| Subject | I think this is an impossible assignment...could you help? |
| Message-ID | <j3jj2t$eq3$1@speranza.aioe.org> |
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...
ANY help would greatly be appreciated. If there's nething i can help
someone else with...i will...THANK YOU
COMSC 1613 Programming I (C) Programming Assignment
3
In this assignment, you will write a 'C' function that will approximate
the action of the command scanf("%d",&x); namely, to read ASCII
character
digits from the standard input stream and convert/interpret the sequence
into a binary integer.
Consider the following implementation of main.
#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);
The function scan_int will use getchar() to read one character at a time
from the standard input and from the digits read compute "on the fly"
the
integer value they represent. The function will return the integer
computed. If the data entered is not appropriate to be interpreted as
an
integer, the function will return INVALID as defined in the program.
White space will be used to mark the beginning and end of a number.
Do not use arrays/strings and do not use atoi() and do not use scanf()
or
fscanf() or any other library functions other than getchar(). Comment
your code thoroughly.
The following is an example execution of the program you will write:
Script command is started on Fri Aug 19 16:19:47 CDT 2011.
$ a.out
enter an integer: 457
the 1-th integer read was 457
enter an integer: -34
the 2-th integer read was -34
enter an integer: +1
the 3-th integer read was 1
enter an integer:
-967835
the 4-th integer read was -967835
enter an integer: 45h5
you entered an invalid sequence
enter an integer: ---56
you entered an invalid sequence
enter an integer: 45+9
you entered an invalid sequence
enter an integer: hello
you entered an invalid sequence
enter an integer: -
you entered an invalid sequence
enter an integer: +
you entered an invalid sequence
enter an integer: -999
$
[toc] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-08-31 09:18 +1200 |
| Message-ID | <9c52csF5dgU1@mid.individual.net> |
| In reply to | #9730 |
On 08/31/11 09:01 AM, Jack wrote:
> 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...
> ANY help would greatly be appreciated. If there's nething i can help
> someone else with...i will...THANK YOU
>
> COMSC 1613 Programming I (C) Programming Assignment
> 3
>
> In this assignment, you will write a 'C' function that will approximate
> the action of the command scanf("%d",&x); namely, to read ASCII
> character
> digits from the standard input stream and convert/interpret the sequence
> into a binary integer.
>
> Consider the following implementation of main.
>
> #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);
What awful style!
> }
>
> Use this main() as a driver. You write function
> long scan_int(void);
Start of with your own main, calling scan_int for progressively more
complex inputs, starting with one digit, then two or more then add a
sign until you get the complete funtion:
int main(void)
{
long result;
puts("enter 1");
assert( scan_int(void) == 1 );
// start with the return here until this passes.
puts("enter 11");
assert( scan_int(void) == 11 );
// then move it here and so on until all cases work.
puts("enter -1");
assert( scan_int(void) == -1 );
// add more tests here.
return 0;
}
--
Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | Jack <spamtrap@trashcan.invalid> |
|---|---|
| Date | 2011-08-30 21:20 +0000 |
| Message-ID | <j3jk6t$i79$3@speranza.aioe.org> |
| In reply to | #9731 |
Ian --- you may have misudnerstood, the problem is to WRITE this scan_int function! It is NOT given.
[toc] | [prev] | [next] | [standalone]
| From | Ian Collins <ian-news@hotmail.com> |
|---|---|
| Date | 2011-08-31 10:34 +1200 |
| Message-ID | <9c56raF5dgU2@mid.individual.net> |
| In reply to | #9732 |
On 08/31/11 09:20 AM, Jack wrote: Context? > Ian --- you may have misudnerstood, the problem is to WRITE this scan_int > function! It is NOT given. No, I didn't. I was suggesting how you should start to write the function. Start with the most basic case and grow it from there. This is a classic learning excise for test driven development! -- Ian Collins
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2011-08-30 21:29 +0000 |
| Message-ID | <j3jknk$eh0$1@reader1.panix.com> |
| In reply to | #9730 |
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 Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Jack <spamtrap@trashcan.invalid> |
|---|---|
| Date | 2011-08-30 21:43 +0000 |
| Message-ID | <j3jli2$lp0$1@speranza.aioe.org> |
| In reply to | #9733 |
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 */
while((c=getchar())==' '); /* whitespace delimeters */
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);}
if(c<'0'||c>'9')
{while(c!=' ') /* space delimeter */
c=getchar();
return(INVALID);}
if(c=='-'||c=='+')
return INVALID;
if(c>='0'&&c<='9')
{while(c>='0'&&c<='9')
{sum=sum*10+(c-48); /* update sum */
c=getchar();}
}
else return(INVALID);
if(sign=='-')
sum=sum*(-1);
return(sum);}
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2011-08-30 22:16 +0000 |
| Message-ID | <j3jnfm$sp1$1@reader1.panix.com> |
| In reply to | #9734 |
In <j3jli2$lp0$1@speranza.aioe.org> Jack <spamtrap@trashcan.invalid> writes:
> 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?
Unfortunately I don't really have the time to make detailed code comments.
But here's a general program flow that might help you:
1. Read and discard whitespace.
2a. If the next char is a minus sign, remember it so you can make the
final return value negative.
2b. Else if the next char is a plus sign, ignore it because we assume
numbers are positive anyway.
3. Read one or more digits, computing the value of sum as we go.
4. Read and discard whitespace.
5. Read and discard a carriage return. (I think you missed this in
your original effort.)
6. Return sum.
At each step in the process, you know what kind of input you're expecting,
and if you get bad input, return INVALID.
Good luck!
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | William Ahern <william@wilbur.25thandClement.com> |
|---|---|
| Date | 2011-08-30 16:26 -0700 |
| Message-ID | <s580j8-k9i.ln1@wilbur.25thandClement.com> |
| In reply to | #9736 |
John Gordon <gordon@panix.com> wrote: <snip> > 5. Read and discard a carriage return. (I think you missed this in > your original effort.) s/carriage return/new-line/ If the OP is using a text-oriented stdio stream, presumably any carriage return would be dropped or translated to new-line.
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-08-30 21:44 -0400 |
| Message-ID | <j3k3lv$a1h$2@dont-email.me> |
| In reply to | #9736 |
On 8/30/2011 6:16 PM, John Gordon wrote:
> In<j3jli2$lp0$1@speranza.aioe.org> Jack<spamtrap@trashcan.invalid> writes:
>
>> 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?
>
> Unfortunately I don't really have the time to make detailed code comments.
> But here's a general program flow that might help you:
>
> 1. Read and discard whitespace.
> 2a. If the next char is a minus sign, remember it so you can make the
> final return value negative.
> 2b. Else if the next char is a plus sign, ignore it because we assume
> numbers are positive anyway.
> 3. Read one or more digits, computing the value of sum as we go.
> 4. Read and discard whitespace.
> 5. Read and discard a carriage return. (I think you missed this in
> your original effort.)
> 6. Return sum.
Steps 4 and 5 seem ill-advised. YMMV.
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2011-08-31 02:03 +0000 |
| Message-ID | <j3k4q2$f30$1@reader1.panix.com> |
| In reply to | #9744 |
In <j3k3lv$a1h$2@dont-email.me> Eric Sosman <esosman@ieee-dot-org.invalid> writes:
> > 1. Read and discard whitespace.
> > 2a. If the next char is a minus sign, remember it so you can make the
> > final return value negative.
> > 2b. Else if the next char is a plus sign, ignore it because we assume
> > numbers are positive anyway.
> > 3. Read one or more digits, computing the value of sum as we go.
> > 4. Read and discard whitespace.
> > 5. Read and discard a carriage return. (I think you missed this in
> > your original effort.)
> > 6. Return sum.
> Steps 4 and 5 seem ill-advised. YMMV.
The problem spec does explicitly say that numbers will be bounded by
whitespace, although the examples given seem to indicate that it is
optional.
Number 5 should be "carriage return or EOF".
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2011-08-31 08:56 -0700 |
| Message-ID | <lnr541tw0k.fsf@nuthaus.mib.org> |
| In reply to | #9745 |
John Gordon <gordon@panix.com> writes:
> In <j3k3lv$a1h$2@dont-email.me> Eric Sosman <esosman@ieee-dot-org.invalid> writes:
>
>> > 1. Read and discard whitespace.
>> > 2a. If the next char is a minus sign, remember it so you can make the
>> > final return value negative.
>> > 2b. Else if the next char is a plus sign, ignore it because we assume
>> > numbers are positive anyway.
>> > 3. Read one or more digits, computing the value of sum as we go.
>> > 4. Read and discard whitespace.
>> > 5. Read and discard a carriage return. (I think you missed this in
>> > your original effort.)
>> > 6. Return sum.
>
>> Steps 4 and 5 seem ill-advised. YMMV.
>
> The problem spec does explicitly say that numbers will be bounded by
> whitespace, although the examples given seem to indicate that it is
> optional.
>
> Number 5 should be "carriage return or EOF".
Note that a carriage return (or newline; is that what was meant?) *is*
whitespace.
--
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 | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2011-08-31 16:15 +0000 |
| Message-ID | <j3lmm7$9dq$1@reader1.panix.com> |
| In reply to | #9762 |
In <lnr541tw0k.fsf@nuthaus.mib.org> Keith Thompson <kst-u@mib.org> writes:
> Note that a carriage return (or newline; is that what was meant?) *is*
> whitespace.
Sure. But if you're reading keyboard input and you don't differentiate
between newline and other kinds of whitespace, how would you ever know
that the user pressed Enter thus signaling end-of-input?
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2011-08-31 09:20 -0700 |
| Message-ID | <lnei01tuvt.fsf@nuthaus.mib.org> |
| In reply to | #9763 |
John Gordon <gordon@panix.com> writes:
> In <lnr541tw0k.fsf@nuthaus.mib.org> Keith Thompson <kst-u@mib.org> writes:
>> Note that a carriage return (or newline; is that what was meant?) *is*
>> whitespace.
>
> Sure. But if you're reading keyboard input and you don't differentiate
> between newline and other kinds of whitespace, how would you ever know
> that the user pressed Enter thus signaling end-of-input?
The same way scanf("%d", &i) does, perhaps? It returns as soon as
numeric input is finished, so it doesn't distinguish between a space and
a newline.
On the other hand, if you want to enforce one number per input line, the
specification should say something like "skip whitespace other than
newline".
--
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 | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-08-31 21:15 -0400 |
| Message-ID | <j3mmbo$59f$1@dont-email.me> |
| In reply to | #9763 |
On 8/31/2011 12:15 PM, John Gordon wrote:
> In<lnr541tw0k.fsf@nuthaus.mib.org> Keith Thompson<kst-u@mib.org> writes:
>
>> Note that a carriage return (or newline; is that what was meant?) *is*
>> whitespace.
>
> Sure. But if you're reading keyboard input and you don't differentiate
> between newline and other kinds of whitespace, how would you ever know
> that the user pressed Enter thus signaling end-of-input?
The original problem statement is somewhat vague, but I think
it's pretty clear that "white space" -- any "white space" -- is to
be taken as a terminator.
Unfortunately, "white space" is left undefined. One might appeal
to isspace() from <ctype.h>, but the problem statement forbids using
it, so the position that isspace() encapsulates the definition of
"white space" is hard to sustain. Under the circumstances, I think
the problem-solver would be justified in considering "white space" to
be any of the characters ' ', '\t', '\n', '\v', '\r', '#', 'W', and 'q'.
Justified, but probably ill-advised since the sloppy problem-setter is
unlikely to give points to a solution that points out his sloppiness.
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2011-08-31 22:18 -0400 |
| Message-ID | <j3mq2h$mrj$1@dont-email.me> |
| In reply to | #9789 |
On 08/31/2011 09:15 PM, Eric Sosman wrote:
...
> The original problem statement is somewhat vague, but I think
> it's pretty clear that "white space" -- any "white space" -- is to
> be taken as a terminator.
>
> Unfortunately, "white space" is left undefined. One might appeal
> to isspace() from <ctype.h>, but the problem statement forbids using
> it, so the position that isspace() encapsulates the definition of
> "white space" is hard to sustain.
Section 6.4p3 defines both "white space" and "white-space characters",
independently of the C standard library in general, or isspace() in
particular. I don't think there's any justification for suggesting that
any alternative definition of those terms was intended. The definition
of isspace() is based upon the definition of white-space character in
6.4p3. While the instructor said "white space", I suspect he meant
"white-space characters" - I doubt that he intended that the programs
should parse and skip C-style comments.
The problem statement specified that scan_int() would approximate
scanf("%d",&x). The standard's definition of the behavior of scanf()
explicitly says that white-space characters are skipped, and cites
isspace() as defining what that means. Lacking permission to call any
standard library function other than getchar(), scan_int() cannot
emulate the locale-dependence of isspace(), but it can at least match
it's behavior in the "C" locale, and I think it's clear that the
instructor intended it to do so.
> ... Under the circumstances, I think
> the problem-solver would be justified in considering "white space" to
> be any of the characters ' ', '\t', '\n', '\v', '\r', '#', 'W', and 'q'.
I can't see any basis for including any of those last three.
--
James Kuyper
[toc] | [prev] | [next] | [standalone]
| From | blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> |
|---|---|
| Date | 2011-09-01 15:06 +0000 |
| Message-ID | <9c9lc2F6f6U5@mid.individual.net> |
| In reply to | #9789 |
In article <j3mmbo$59f$1@dont-email.me>, Eric Sosman <esosman@ieee-dot-org.invalid> wrote: > On 8/31/2011 12:15 PM, John Gordon wrote: > > In<lnr541tw0k.fsf@nuthaus.mib.org> Keith Thompson<kst-u@mib.org> writes: > > > >> Note that a carriage return (or newline; is that what was meant?) *is* > >> whitespace. > > > > Sure. But if you're reading keyboard input and you don't differentiate > > between newline and other kinds of whitespace, how would you ever know > > that the user pressed Enter thus signaling end-of-input? > > The original problem statement is somewhat vague, but I think > it's pretty clear that "white space" -- any "white space" -- is to > be taken as a terminator. > > Unfortunately, "white space" is left undefined. One might appeal > to isspace() from <ctype.h>, but the problem statement forbids using > it, so the position that isspace() encapsulates the definition of > "white space" is hard to sustain. Under the circumstances, I think > the problem-solver would be justified in considering "white space" to > be any of the characters ' ', '\t', '\n', '\v', '\r', '#', 'W', and 'q'. > Justified, but probably ill-advised since the sloppy problem-setter is > unlikely to give points to a solution that points out his sloppiness. You never know -- I mean, there *are* instructors who are glad to have mistakes pointed out, especially if it's done reasonably diplomatically -- but that might not be the way to bet. :-)? -- 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:04 +0000 |
| Message-ID | <9c9l7iF6f6U2@mid.individual.net> |
| In reply to | #9763 |
In article <j3lmm7$9dq$1@reader1.panix.com>, John Gordon <gordon@panix.com> wrote: > In <lnr541tw0k.fsf@nuthaus.mib.org> Keith Thompson <kst-u@mib.org> writes: > > > Note that a carriage return (or newline; is that what was meant?) *is* > > whitespace. > > Sure. But if you're reading keyboard input and you don't differentiate > between newline and other kinds of whitespace, how would you ever know > that the user pressed Enter thus signaling end-of-input? Say what? If you're reading from the standard input stream, you won't get *any* input until the user presses "Enter", will you? -- B. L. Massingill ObDisclaimer: I don't speak for my employers; they return the favor.
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2011-09-01 15:20 +0000 |
| Message-ID | <j3o7sd$imq$1@reader1.panix.com> |
| In reply to | #9814 |
In <9c9l7iF6f6U2@mid.individual.net> blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> writes:
> > Sure. But if you're reading keyboard input and you don't differentiate
> > between newline and other kinds of whitespace, how would you ever know
> > that the user pressed Enter thus signaling end-of-input?
> Say what? If you're reading from the standard input stream, you won't
> get *any* input until the user presses "Enter", will you?
Good point! Not sure what I was thinking.
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | David Thompson <dave.thompson2@verizon.net> |
|---|---|
| Date | 2011-09-06 01:20 -0400 |
| Message-ID | <m5bb679vh71olgu9ohpgjrlb406odh5tl5@4ax.com> |
| In reply to | #9814 |
On 1 Sep 2011 15:04:18 GMT, blmblm@myrealbox.com <blmblm.myrealbox@gmail.com> wrote: > In article <j3lmm7$9dq$1@reader1.panix.com>, > John Gordon <gordon@panix.com> wrote: > > In <lnr541tw0k.fsf@nuthaus.mib.org> Keith Thompson <kst-u@mib.org> writes: > > > > > Note that a carriage return (or newline; is that what was meant?) *is* > > > whitespace. > > > > Sure. But if you're reading keyboard input and you don't differentiate > > between newline and other kinds of whitespace, how would you ever know > > that the user pressed Enter thus signaling end-of-input? > > Say what? If you're reading from the standard input stream, you won't > get *any* input until the user presses "Enter", will you? No, both pedantically and seriously. First, C's standard input need not be a keyboard and often isn't. Second, keyboards (and keyboard processing) are not all the same. If there is a key that generates input-end-of-line it isn't always labelled Enter, and there may be (other) key combinations or sequences that generate it also (or even instead). Skipping over those to the serious question, can a C program receive input from a keyboard before the user enters an end-of-line character? Still yes. The standard requires that stdin (and others) be not fully buffered if the stream '[cannot] be determined not to refer to an interactive device' and 'What constitutes an interactive device is implementation-defined.' Even assuming the implementation can correctly identify keyboard input as interactive -- which is not always possible -- this does not specify whether it is then line buffered or unbuffered. Unbuffered is 'intended' to receive (or transmit) 'as soon as possible', which again can depend on the implementation, but certainly can be without any end-of-line. And there are real cases where this happens. In addition to special modes on many systems, on Unix systems using a standard tty driver in normal 'cooked' mode, the 'end of file' character, usually defaulted to control-D, only does that function at the beginning of a line. Partway into a line it causes the _partial_ line to be read. What you can say is that for *most* users of *most* systems *most* of the time, the program gets keyboard input only when the user presses an end-of-line key. This happens often enough that it is frequently useful to code programs to assume and process line-at-a-time input. In particular in C you can force line-oriented input, portably and even for 'non-interactive devices' like files, with fgets() (or <horrible>gets()</>) and then process it accordingly.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2011-08-30 16:37 -0700 |
| Message-ID | <ln1uw2v5cx.fsf@nuthaus.mib.org> |
| 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!
>
> 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 */
>
> 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);}
>
> if(c<'0'||c>'9')
> {while(c!=' ') /* space delimeter */
> c=getchar();
> return(INVALID);}
>
> if(c=='-'||c=='+')
> return INVALID;
>
> if(c>='0'&&c<='9')
> {while(c>='0'&&c<='9')
> {sum=sum*10+(c-48); /* update sum */
> c=getchar();}
> }
>
> else return(INVALID);
>
> if(sign=='-')
> sum=sum*(-1);
> return(sum);}
I find your code layout difficult to read (though not much worse than
the code in your original post, which presumably was from the
assignment).
I've taken the liberty of reformatting it. I have made no semantically
significant changes. I filtered it with "indent -kr", then made a few
more cosmetic changes.
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 delimiters */
/* nothing */
}
if (c == '-') { /* negative */
sign = '-';
c = getchar();
while ((c = getchar()) == ' ') {
/* nothing */
}
if (c < '0' || c > '9') {
return INVALID;
}
}
else if (c == '+') { /* positive */
sign = '+';
c = getchar();
while ((c = getchar()) == ' ') {
/* nothing */
}
if (c < '0' || c > '9') {
return INVALID;
}
}
if (c < '0' || c > '9') {
while (c != ' ') { /* space delimiter */
c = getchar();
}
return INVALID;
}
if (c == '-' || c == '+') {
return INVALID;
}
if (c >= '0' && c <= '9') {
while (c >= '0' && c <= '9') {
sum = sum * 10 + (c - 48); /* update sum */
c = getchar();
}
}
else {
return INVALID;
}
if (sign == '-') {
sum = sum * (-1);
}
return sum;
}
Some comments on the code itself:
Return statements do not require parentheses.
48 is the ASCII encoding for '0', but how is the reader expected to know
that? Just write '0'.
You have a lot of redundancy. For example, the blocks for c == '-' and
c == '+' could be combined.
In the "update sum" block, the "if" is unnecessary. A while loop will
not be executed (or, if you prefer, will iterate 0 times) if the
condition is initially false.
To negate a number, you can use the unary "-" operator; multiplication
by -1 is overkill.
--
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]
Page 1 of 4 [1] 2 3 4 Next page →
Back to top | Article view | comp.lang.c
csiph-web