{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

LEC20080908 - Introduction to Computer Programming...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computer Programming September 8, 2008 1 Required readings How many lines of code have you written? Let n be your answer. If n is zero, you must read Chapter 2 of the textbook twice carefully. If n is between 1 and 1 , 000, you should read Chapter 2 once. If n is greater than 1 , 000, you can safely ignore Chapter 2, and proceed to Chapter 3. 2 Convert the above requirements to C code The requirements in Section 1 can be easily converted to a C program, which, based on our input, n , tells us what we should do. read.c is the source file for the program. See Section 3 for detailed explanations. /* read.c */ #include <stdio.h> void read_ch2_twice() { printf(‘‘:( I must read Chapter 2 twice carefully!\n’’); } void read_ch2_once() { printf(‘‘I should read Chapter 2 once.\n’’); } void ignore_ch2() { printf(‘‘:) I can safely ignore Chapter 2.\n’’); } /* 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
* Input the number of lines of code that you have written. Hit Enter * key. Follow the instructions given by the computer. */ int main() { int n; printf(‘‘How many lines of code have you written? ‘‘); if (scanf(‘‘%d’’, &n) < 1 || n < 0) return 1; if (n == 0) /* no lines? */ read_ch2_twice(); if (n > 0 && n <= 1000) /* some lines? */ read_ch2_once(); if (n > 1000) /* enough lines? */ ignore_ch2(); return 0; } 3 Explanation When we see a /* */ pattern, this is a C’s comment. The comment helps us (not computers) understand the code. As we will see below, a comment may occupy only one line, or may occupy multiple lines. /* read.c */ /* * Input the number of lines of code that you have written. Hit Enter * key. Follow the instructions given by the computer. */ When we see #include , this is a C’s preprocessor directive. #include <stdio.h> inserts the contents from the file stdio.h into the current file, read.c . If we check stdio.h , we will find this line: extern int printf (__const char *__restrict __format, ...);
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}