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

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 * 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 Cs 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....
View Full Document

Page1 / 6

LEC20080908 - Introduction to Computer Programming...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online