Thread: K&R solution?

  1. #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.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    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...

  4. #4
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Thanks. Back to the drawing board.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #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.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Thanks Mats, but according to the exercise we can't use || or &&.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  11. #11
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Thanks. I'll try that.

  12. #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) ...

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

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