Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is this C program doing what it is supposed to do ?

Reply
Thread Tools

Is this C program doing what it is supposed to do ?

 
 
John
Guest
Posts: n/a
 
      01-30-2011
The program is supposed to read lines from the standard input, then each line is printed on the standard output preceded by its line number. The program should have no built-on limit on how long a line it can handle.

So I wrote the following program in C, but I'm not sure that it is doing what it is supposed to do. I verified the source code against the solution in the back of the book and I'm pasting here what the solution in the back of the book is.

#include<stdio.h>
#include<stdlib.h>

int main(){

int ch;
int at_beginning = 1;
int line = 0;

while( (ch==getchar())!= EOF){

if(at_beginning == 1){

at_beginning = 0;
line+=1;
printf("%d ", line);

}

putchar(ch);

if(ch == '\n')
at_beginning = 1;
}
return EXIT_SUCCESS;
}
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      01-30-2011
On 1/30/2011 4:08 PM, John wrote:
> The program is supposed to read lines from the standard input, then each line is printed on the standard output preceded by its line number. The program should have no built-on limit on how long a line it can handle.
>
> So I wrote the following program in C, but I'm not sure that it is doing what it is supposed to do. I verified the source code against the solution in the back of the book and I'm pasting here what the solution in the back of the book is.
>
> #include<stdio.h>
> #include<stdlib.h>
>
> int main(){
>
> int ch;
> int at_beginning = 1;
> int line = 0;
>
> while( (ch==getchar())!= EOF){


You want `=' instead of `==' here. (This may be the first time
I've seen the "equals/assign" mistake in this direction; usually, it's
using `=' where `==' is meant, not the other way around.)

--
Eric Sosman
lid
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-30-2011
John <> writes:

> The program is supposed to read lines from the standard input, then each line is printed on the standard output preceded by its line number. The program should have no built-on limit on how long a line it can handle.
>
> So I wrote the following program in C, but I'm not sure that it is doing what it is supposed to do. I verified the source code against the solution in the back of the book and I'm pasting here what the solution in the back of the book is.
>
> #include<stdio.h>
> #include<stdlib.h>
>
> int main(){


int main(void) is better. It hardly matters for main, but for other
functions using the form with void will enable proper function prototype
checking so it's worth getting into the habit.

> int ch;
> int at_beginning = 1;
> int line = 0;
>
> while( (ch==getchar())!= EOF){


== confused with = already commented on.

> if(at_beginning == 1){


It's usually better to write:

if (at_beginning) { ...

> at_beginning = 0;
> line+=1;
> printf("%d ", line);
>
> }
>
> putchar(ch);
>
> if(ch == '\n')
> at_beginning = 1;


and here I'd write

at_beginning = ch == '\n';

This way the reader can see at once when at_beginning is 0 and when it
is 1. The earlier assignment becomes redundant.

> }
> return EXIT_SUCCESS;
> }


--
Ben.
 
Reply With Quote
 
Chris H
Guest
Posts: n/a
 
      01-31-2011
In message <0.8a2a169873977757779c.20110130214737GMT.87k4hm5a iu.fsf@bsb.
me.uk>, Ben Bacarisse <> writes
>> if(at_beginning == 1){

>
>It's usually better to write:
>
> if (at_beginning) { ...


No it isn't

>> if(ch == '\n')
>> at_beginning = 1;

>
>and here I'd write
>
> at_beginning = ch == '\n';


Appalling! And dangerous.




--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/



 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-31-2011
On 1/31/2011 4:05 AM, Chris H wrote:
> In message<0.8a2a169873977757779c.20110130214737GMT.8 7k4hm5aiu.fsf@bsb.
> me.uk>, Ben Bacarisse<> writes
>>> if(at_beginning == 1){

>>
>> It's usually better to write:
>>
>> if (at_beginning) { ...

>
> No it isn't


De gustibus non disputandum est.

>>> if(ch == '\n')
>>> at_beginning = 1;

>>
>> and here I'd write
>>
>> at_beginning = ch == '\n';

>
> Appalling! And dangerous.


Balderdash! And ignorant.

--
Eric Sosman
lid
 
Reply With Quote
 
Chris H
Guest
Posts: n/a
 
      01-31-2011
In message <ii69dn$6vh$>, Eric Sosman
<> writes
>On 1/31/2011 4:05 AM, Chris H wrote:
>> In message<0.8a2a169873977757779c.20110130214737GMT.8 7k4hm5aiu.fsf@bsb.
>> me.uk>, Ben Bacarisse<> writes
>>>> if(at_beginning == 1){
>>>
>>> It's usually better to write:
>>>
>>> if (at_beginning) { ...

>>
>> No it isn't

>
> De gustibus non disputandum est.
>
>>>> if(ch == '\n')
>>>> at_beginning = 1;
>>>
>>> and here I'd write
>>>
>>> at_beginning = ch == '\n';

>>
>> Appalling! And dangerous.

>
> Balderdash! And ignorant.


The OP has already made one mistake with = and ==
The line is dangerous.

--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/



 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-31-2011
Chris H <> writes:

> In message <ii69dn$6vh$>, Eric Sosman
> <> writes
>>On 1/31/2011 4:05 AM, Chris H wrote:
>>> In message<0.8a2a169873977757779c.20110130214737GMT.8 7k4hm5aiu.fsf@bsb.
>>> me.uk>, Ben Bacarisse<> writes
>>>>> if(at_beginning == 1){
>>>>
>>>> It's usually better to write:
>>>>
>>>> if (at_beginning) { ...
>>>
>>> No it isn't

>>
>> De gustibus non disputandum est.
>>
>>>>> if(ch == '\n')
>>>>> at_beginning = 1;
>>>>
>>>> and here I'd write
>>>>
>>>> at_beginning = ch == '\n';
>>>
>>> Appalling! And dangerous.

>>
>> Balderdash! And ignorant.

>
> The OP has already made one mistake with = and ==
> The line is dangerous.


The original code (to which I was offering this alternative) has the
same set of operators so it would seem to offer the same opportunities
for =/== confusion. You also snipped my supporting argument. What is
your argument in support of the original (or against my alternative)?

--
Ben.
 
Reply With Quote
 
Chris H
Guest
Posts: n/a
 
      01-31-2011
In message <0.c67692d26d53aa1f6676.20110131132623GMT.87ei7t5h mo.fsf@bsb.
me.uk>, Ben Bacarisse <> writes
>Chris H <> writes:
>
>> In message <ii69dn$6vh$>, Eric Sosman
>> <> writes
>>>On 1/31/2011 4:05 AM, Chris H wrote:
>>>> In message<0.8a2a169873977757779c.20110130214737GMT.8 7k4hm5aiu.fsf@bsb.
>>>> me.uk>, Ben Bacarisse<> writes
>>>>>> if(at_beginning == 1){
>>>>>
>>>>> It's usually better to write:
>>>>>
>>>>> if (at_beginning) { ...
>>>>
>>>> No it isn't
>>>
>>> De gustibus non disputandum est.
>>>
>>>>>> if(ch == '\n')
>>>>>> at_beginning = 1;
>>>>>
>>>>> and here I'd write
>>>>>
>>>>> at_beginning = ch == '\n';
>>>>
>>>> Appalling! And dangerous.
>>>
>>> Balderdash! And ignorant.

>>
>> The OP has already made one mistake with = and ==
>> The line is dangerous.

>
>The original code (to which I was offering this alternative) has the
>same set of operators so it would seem to offer the same opportunities
>for =/== confusion. You also snipped my supporting argument. What is
>your argument in support of the original (or against my alternative)?


Er what argument for it? There wasn't one.


--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/



 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      01-31-2011
Chris H wrote:
> In message
> <0.c67692d26d53aa1f6676.20110131132623GMT.87ei7t5h mo.fsf@bsb. me.uk>,
> Ben Bacarisse <> writes
>> Chris H <> writes:
>>
>>> In message <ii69dn$6vh$>, Eric Sosman
>>> <> writes
>>>> On 1/31/2011 4:05 AM, Chris H wrote:
>>>>> In
>>>>> message<0.8a2a169873977757779c.20110130214737GMT.8 7k4hm5aiu.fsf@bsb.
>>>>> me.uk>, Ben Bacarisse<> writes
>>>>>>> if(at_beginning == 1){
>>>>>>
>>>>>> It's usually better to write:
>>>>>>
>>>>>> if (at_beginning) { ...
>>>>>
>>>>> No it isn't
>>>>
>>>> De gustibus non disputandum est.
>>>>
>>>>>>> if(ch == '\n')
>>>>>>> at_beginning = 1;
>>>>>>
>>>>>> and here I'd write
>>>>>>
>>>>>> at_beginning = ch == '\n';
>>>>>
>>>>> Appalling! And dangerous.
>>>>
>>>> Balderdash! And ignorant.
>>>
>>> The OP has already made one mistake with = and ==
>>> The line is dangerous.

>>
>> The original code (to which I was offering this alternative) has the
>> same set of operators so it would seem to offer the same
>> opportunities for =/== confusion. You also snipped my supporting
>> argument. What is your argument in support of the original (or
>> against my alternative)?

>
> Er what argument for it? There wasn't one.


This one :
> This way the reader can see at once when at_beginning is 0 and when it
> is 1. The earlier assignment becomes redundant.


Bye, Jojo

 
Reply With Quote
 
Chris H
Guest
Posts: n/a
 
      01-31-2011
In message <ii6ift$9k$>, Joachim Schmitz
<> writes
>Chris H wrote:
>> In message
>> <0.c67692d26d53aa1f6676.20110131132623GMT.87ei7t5h mo.fsf@bsb. me.uk>,
>> Ben Bacarisse <> writes
>>> Chris H <> writes:
>>>
>>>> In message <ii69dn$6vh$>, Eric Sosman
>>>> <> writes
>>>>> On 1/31/2011 4:05 AM, Chris H wrote:
>>>>>> In
>>>>>> message<0.8a2a169873977757779c.20110130214737GMT.8 7k4hm5aiu.fsf@bsb.
>>>>>> me.uk>, Ben Bacarisse<> writes
>>>>>>>> if(at_beginning == 1){
>>>>>>> It's usually better to write:
>>>>>>> if (at_beginning) { ...
>>>>>> No it isn't
>>>>> De gustibus non disputandum est.
>>>>>
>>>>>>>> if(ch == '\n')
>>>>>>>> at_beginning = 1;
>>>>>>> and here I'd write
>>>>>>> at_beginning = ch == '\n';
>>>>>> Appalling! And dangerous.
>>>>> Balderdash! And ignorant.
>>>> The OP has already made one mistake with = and ==
>>>> The line is dangerous.
>>> The original code (to which I was offering this alternative) has
>>>the
>>> same set of operators so it would seem to offer the same
>>> opportunities for =/== confusion. You also snipped my supporting
>>> argument. What is your argument in support of the original (or
>>> against my alternative)?

>> Er what argument for it? There wasn't one.

>
>This one :
>> This way the reader can see at once when at_beginning is 0 and when it
>> is 1. The earlier assignment becomes redundant.

>
>Bye, Jojo
>


So there is no argument to support it then and it is dangerous.





--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is this program doing what is supposed to do ? devkays C Programming 1 01-30-2011 09:07 PM
What XML is supposed to be sent back after WPS signup? white_cs Wireless Networking 6 07-11-2005 01:31 AM
Is FF Options/Advanced/SoftwareUpdate supposed to work ? Al Dykes Firefox 4 01-07-2005 10:18 PM
Thunderbird is supposed to have wow ? R Fruth in Houston Firefox 5 12-17-2004 01:03 PM
MS wireless/wired router - supposed to work this way? Dana Cline - MVP Wireless Networking 2 07-13-2004 01:05 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57