Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prof. Béat Hirsbrunner Fulvio Frapolli, PhD Student (exercises) Bachelor students : - Major in computer science (first year, 2nd term) - Major in information.

Similar presentations


Presentation on theme: "Prof. Béat Hirsbrunner Fulvio Frapolli, PhD Student (exercises) Bachelor students : - Major in computer science (first year, 2nd term) - Major in information."— Presentation transcript:

1 Prof. Béat Hirsbrunner Fulvio Frapolli, PhD Student (exercises) Bachelor students : - Major in computer science (first year, 2nd term) - Major in information systems (first year, 2nd term) - Minor in computer science - Propedeutic in computer science (first year, 2nd term) Imperative Programming University of Fribourg, Department of Informatics Summer term 2007, www.unifr.ch/diuf/pai/ip

2 1 #include main() { printf("hello, world\n"); } (KR p6) Session 1 (Lecture: 3 hours, Exercises: 1 hour) - 13 March 2007 Hello World % gcc hello.c // Compile hello.c %./a.out // Run a.out % gcc -o hello hello.c // Compile hello.c %./hello // Run hello

3 2 #include /* Print Fahrenheit-Celsius table for fahr = 0, 20,..., 300 */ main() { int fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature table */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = 5 * (fahr - 32) / 9; printf("%d\t%d\n", fahr, celsius); fahr = fahr + step; } (KR p9) Variables and Arithmetic Expressions (1)

4 3 (KR p12) #include /* Print Fahrenheit-Celsius table for fahr = 0, 20,..., 300 */ /* Floating-point version */ main() { float fahr, celsius; int lower, upper, step; lower = 0; /* lower limit of temperature table */ upper = 300; /* upper limit */ step = 20; /* step size */ fahr = lower; while (fahr <= upper) { celsius = (5.0/9.0) * (fahr - 32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; } Variables and Arithmetic Expressions (2)

5 4 #include /* print Fahrenheit-Celsius table */ main() { int fahr; for (fahr = 0; fahr <= 300; fahr = fahr + 20) printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); } (KR p13) The For Statement

6 5 #include #define LOWER 0 /* lower limit of table */ #define UPPER 300 /* upper limit */ #define STEP 20 /* step size */ /* print Fahrenheit-Celsius table */ main() { int fahr; for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP) printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32)); } (KR p15) Symbolic Constants

7 6 #include /* copy input to output; 1st version */ main() { int c; c = getchar(); while (c != EOF) { putchar(c); c = getchar(); } (KR p16) File Copying (1)

8 7 #include /* copy input to output; 2nd version */ main() { int c; while ((c = getchar()) != EOF) putchar(c); } (KR p17) File Copying (2)


Download ppt "Prof. Béat Hirsbrunner Fulvio Frapolli, PhD Student (exercises) Bachelor students : - Major in computer science (first year, 2nd term) - Major in information."

Similar presentations


Ads by Google