C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-08-2008, 02:11 AM   #1
Registered User
 
Join Date: Feb 2008
Location: Yokohama
Posts: 48
K&R solution?

I was doing one of the K&R exercises which is to write a loop equivalent for the following short program without using || or &&. Usually my solutions are pretty similar to the solution book answer or some of the solutions I can find on the web but this time it is very different. Am I way off with this solution?
Code:
for (i = 0; i < lim -1 && (c = getchar()) != '\n' && c != EOF; ++i)
		s[i] = c;
to
Code:
        i = 0;
	while (i < lim -1)
		if (c = getchar() != '\n')
			if (c != EOF)
				++i;
        s[i] = c;

Last edited by deadhippo; 05-08-2008 at 02:31 AM.
deadhippo is offline   Reply With Quote
Old 05-08-2008, 02:17 AM   #2
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
I'm assuming yours is the second one, in which case you are missing the condition to break the loop when you get a end-of-line or end-of-file.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 05-08-2008, 02:22 AM   #3
Registered User
 
Join Date: Apr 2008
Posts: 278
I guess the 1st solution is the correct one and the second is your attempt?
Clearly they don't do the same thing. The first one fill the array with characters
read from the input stream until the array is full or there's nothing more to read
or the last character was a new line. The second solution fill the array and
everything should be fine until the EOF or '\n'... in which case, if the array is not
full yet, we should fall into an infinite loop as the index is no longer incremented
and the current item in the array is overwritten by EOF or '\n'.
The particular case in which the last character read is also the last index of the
array should work fine however...
root4 is offline   Reply With Quote
Old 05-08-2008, 02:37 AM   #4
Registered User
 
Join Date: Feb 2008
Location: Yokohama
Posts: 48
Thanks. Back to the drawing board.
deadhippo is offline   Reply With Quote
Old 05-08-2008, 02:51 AM   #5
Mysterious C++ User
 
Join Date: Oct 2007
Posts: 14,099
Code:
if ( (c = getchar()) != '\n' )
Just a note, but it's better to do it like this. Otherwise you would get a warning in Visual Studio saying assignment within if. And we'd like to write code as clean as possible and as portable as possible that works without warnings in as many compilers as possible.

Just thought I'd mention. Carry on!
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 05-08-2008, 02:53 AM   #6
Registered User
 
Join Date: Feb 2008
Location: Yokohama
Posts: 48
Ok, I have another idea.

Code:
	i = 0;
	loop = 1;
	while (loop == 1)
		if (i >= lim-1)
			loop = 0;
		else if ((c = getchar()) == '\n')
			loop = 0;
		else if (c == EOF)
			loop = 0;
		else
		{
			s[i] = c;
			++i;
		}
I think this might work but it is very similar to the answer in Tondo and Gimpel.

Thanks Elysia, I had been reading that that was good practice too.
deadhippo is offline   Reply With Quote
Old 05-08-2008, 03:02 AM   #7
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by Elysia View Post
Code:
if ( (c = getchar()) != '\n' )
Just a note, but it's better to do it like this. Otherwise you would get a warning in Visual Studio saying assignment within if. And we'd like to write code as clean as possible and as portable as possible that works without warnings in as many compilers as possible.

Just thought I'd mention. Carry on!
Not to mention that the assignment does the wrong thing, as c will be equal to (getchar() != '\n'), because the comparison operator has higher priority than the assignment.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 05-08-2008, 03:07 AM   #8
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by deadhippo View Post
Ok, I have another idea.

Code:
	i = 0;
	loop = 1;
	while (loop == 1)
		if (i >= lim-1)
			loop = 0;
		else if ((c = getchar()) == '\n')
			loop = 0;
		else if (c == EOF)
			loop = 0;
		else
		{
			s[i] = c;
			++i;
		}
I think this might work but it is very similar to the answer in Tondo and Gimpel.

Thanks Elysia, I had been reading that that was good practice too.
I can't see anything wrong, but I would simplify the if/else tree a bit, and use a different condition:

Code:
        i = 0;
	while (i < lim-1)
        {
		if ((c = getchar()) == '\n' || c == EOF)
			break;
		s[i] = c;
		++i;
        }
--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 05-08-2008, 03:52 AM   #9
Registered User
 
Join Date: Feb 2008
Location: Yokohama
Posts: 48
Thanks Mats, but according to the exercise we can't use || or &&.
deadhippo is offline   Reply With Quote
Old 05-08-2008, 04:01 AM   #10
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by deadhippo View Post
Thanks Mats, but according to the exercise we can't use || or &&.
Ok, but you can reduce the number of if/else branches to two by just splitting that "|| c == EOF" into it's own if-statement.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Old 05-09-2008, 12:11 AM   #11
Registered User
 
Join Date: Feb 2008
Location: Yokohama
Posts: 48
Thanks. I'll try that.
deadhippo is offline   Reply With Quote
Old 05-09-2008, 06:28 AM   #12
-AppearingOnThis..........
 
Join Date: May 2005
Location: Netherlands
Posts: 44
why even bother splitting it up into if/elseif cases. just use the fact that conditionals such as == and != represent a boolean value.

ie. something like
Code:
while((cond1) + (cond2) == 2) ...
SirNot is offline   Reply With Quote
Old 05-09-2008, 06:36 AM   #13
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by SirNot View Post
why even bother splitting it up into if/elseif cases. just use the fact that conditionals such as == and != represent a boolean value.

ie. something like
Code:
while((cond1) + (cond2) == 2) ...
That is just bypassing the rule of not using || and &&, isn't it? I think the rule was there to show how much simpler [and sometimes more complex] the code becomes when you use or don't use those - not "find another way to make the same thing", even if your suggestion is quite creative.

--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
'Solution' and 'Project' usage in VC++ C+/- C++ Programming 2 01-13-2007 09:50 AM
anyone know the solution? heeroyung C Programming 15 09-30-2005 06:46 AM
My Unix/Linux SECURITY SOLUTION - pls read bjdea1 Linux Programming 3 04-11-2004 09:28 PM
Solution - Workspace --> are they the same actionbasti C++ Programming 2 09-14-2003 04:05 PM
Problem and Solution DavidP Tech Board 3 08-18-2003 02:23 AM


All times are GMT -6. The time now is 04:14 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22