| « Now is the time | The whole Myth » |
Tue, Jan 02, 2007
![[Icon]](rsc/img/chain_link.gif)
Back to K&R, and I encounter the next excercise: Write a loop to replace the trusty "Getline" function that doesn't use && or || - the "AND" and "NOT" operators
I almost didn't do this one: It basically requires you to take an elegant and simple bit of code, and replace it with something horrible. It would have been slightly better if I knew about "break" but I don't yet (by the book).
Still, I did it: Too stubborn to leave it alone. Even so, I still don't like it. Horrible bit of code.
Follow up:
{
int i, c;
for (i = c = 0; c != '\n' && (c = getchar()) != EOF && i < max-1; ++i)
string[i] = c;
string[i] = '\0';
return i;
}
That was the original bit of code. The "for" had to be replaced with a loop that didn't require && or ||, and so:
{
int i, c, cont;
i = c = cont = 0;
while (!cont)
{
if (c != '\n')
{
if ((c=getchar()) != EOF)
{
if (i < max-1)
{
string[i] =c;
++i;
}
else
cont = 1;
}
else
cont = 1;
}
else
cont = 1;
}
string[i] = '\0';
return i;
} One of the first things every CS student notices is that the more "elegant" the algorithm, the bigger and nastier it is to implement
#include <stdio.h>
int main()
{
char world[] = "Hello, world\n\0"
int i = 0;
while (world[i] != '\0')
{
putchar(world[i]);
++i;
}
return 0;
}
Creative Hedgehog
La parte A se refiere solamente a las dos novelas estudiadas. La parte A debe ser preparada después de leer la primera mitad de la novela y contestar las siguientes preguntas: ¿te está gustando la novela/película o no, y por qué? No me gusta la novela. Las personajes que puedes gustar son superficiales, o hacen [...]
06/08/10 - SPN3730 diario: Pascual Duarte parte A
Hari's corner
Why please do stay on a bit longer! Hopefully I'll come back with something to write about
04/09/10 - Hello, are you still there?
Place of Stuff
Isn't this exciting? We're out of the tedium of Genesis (world created, man falls, many people live and die. Oh, and attempted forced buggery and a spot of incest). We're into Exodus now; the Bible has got going, that tricky first chapter is out of the way and the real action can start! When the [...]
03/08/10 - The Bible ? On The Waterfront
Advice From a Single Girl
Yay! Labour Day!
Long weekend!
Yay!
04/09/10 - Long Weekend!
Nation
  This was possibly the most ridiculous show I have seen in a long time and I can get Sky 1 I know ridiculous. It could be summed up in three sentences Do you know what's in your cereal? Want to? Read the label. Instead it went on for a hour about how evil the [...]
27/10/09 - Dispatches ? do you know what?s in your breakfast? (warning...
Blogroll generated by MagpieRSS
![[Links]](http://geekblog.oneandoneis2.org/skins/112/rsc/img/chain_link.gif)
You know.. It's been 24 hours and I *still* can't think how the way COBOL uses multi-level variables could ever have seemed like a good idea
05/09/10
Dominic tried to explain how circular references can cause a memory leak to a colleague this morning, and got told off for not working. Apparently, the analogy of a madman shooting anybody who isn't being pointed at by somebody else was NOT the boss-safe way to go..
01/09/10
![]()
I last listened to:
The Offspring - She's Got Issues
Most recent photo:
Submersible houseboat