CSC161 2010F, Class 19: Control Flow: Loops Overview: * Exam 1 questions. * Loop Basics. * Questions from the Reading. Admin: * Preparation for today's class got a bit derailed, so it may go differently than normal. * I was surprised to get so few questions on the reading. (Don't worry, I'm pretty sure that the questions I got will take a bit of time.) * Reading for tomorrow: K&R 3.7-3.8. * There are certainly questions on exam 1. Exam questions: * "negate" is too hard. Can we just use "n"? * Yes * Do I have to support numbers like +23 and -18? * No, it makes your life too difficult * How am I supposed to read the input, anyway? Should I use getchar() or scanf()? * Yes, you should use getchar() or scanf(). Probably not both, though. * If I use getchar(), what strategy should I use? * switch (ch = getchar()) { case '+': Code for plus break; case '-': Code for minus break; ... case '0': case '1': ... case '9': Modified code for atoi - will need to read more characters Code for pushing the number on the stack break; case ' ': case '\t': case '\n': Skip it break; default: Report an error break; } * If I use scanf, how should I read a number? scanf ("%d", &i); * How do I know that I'm supposed to read a number? * You can try reading one and if scanf returns 0, you know you failed. * How do I use scanf to decide whether I see '+' or '-' or ...? * Option 1: Read a character and use a case statement. * Option 2: Use something ugly like the following (which looks for a plus sign) i = 0; if ((scanf (" +%n", &i) != EOF) && (i >= 1)) { Code for add } * Note: Ordering matters! * Did I imagine you saying "Don't bother supporting 'p', the print operation?" * Yes, it appears that you dream optimistically about this class. * What I did say was "You cannot assume that every input line ends with the 'p' operations, even though all the example lines do." * In IEEE floating point numbers, what are the last 23 bits? If the exponent is not 00000000 and not 11111111, the last 23 bits are read from left to right as 1/2's bit, 1/4's bit, 1/8's bit .... And then add 1. * Do we have to consider expondnets of all 0's and all 1's? * Sure, why not. * Do we have to consider the all 1's case in problem 4? * Certainly. * How about the case in which there's only 1 1? * Certainly? * How about the case in which there are ten 1's? * Since you only have eight bits, that's a bit hard. * What exactly is the input? * Taken from the command line as string s * If the user types ./coolprogram 1 1 1 0 0 1 1 1 C automatically converts each value to a string, so argv[1] is "1", argv[2] is "1", argv[3] is "1", argv[4] is "0", and so on and so forth. * You can assume that the command line has exactly eight digits after the command name * If the user types ./coolprogram 1 1 1 You can crash and burn (segmentation fault or stupid answer). * If the user types ./coolprogram 2 3 1 0 0 0 1 0 You can crash and burn (segmentation fault or stupid answer). * Okay? * Not particuloarly. * Can I write a function that checks if three numbers represent a triangle and then use it within an if statement? int is_triangle (int x, int y, int z) { } ... if (!is_triangle (x, y, z)) { printf ("You confusedly nice person, you! %d, %d, and %d do not form a triangle.\n"); return 1; } Loops: Primary kind: while loop while (TEST) BODY Meaning: Evaluate the test If it holds (not 0), do the BODY and start all over again If the test does not hold, you're done with the while statement, so you can go on to the next statement For loops and Do while loops are only syntactic sugar (more or less) for (INIT; TEST; INCREMENT) BODY Is shorthand for INIT; while (TEST) { BODY; INCREMENT; } And we use it because it's clearer and shorter. Questions from the Reading * What I found most confusing was actually the syntax of the atoi function on page 61. I can't understand where the for loops start and end, or how they interact with each other - especially the first one which seems to do nothing. This could be because it's written with a different coding standard than what we're using though.... * I found the shellsort example on page 62 the most confusing part of the reading. So, nested for loops are somewhat confusing. * The do-while loop I understand conceptually but a few more examples on how it's implemented would be helpful. * I'm not sure what's going on when it talks about comma operators in 3.5, as I find the example code too confusing to be helpful.