Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics.

Slides:



Advertisements
Similar presentations
COMP 2130 Intro Computer Systems Thompson Rivers University
Advertisements

Lectures 10 & 11.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
C++ Basics March 10th. A C++ program //if necessary include headers //#include void main() { //variable declaration //read values input from user //computation.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Engineering Computing I Chapter 1 – Part B A Tutorial Introduction continued.
1 Homework Assignments Turn in HW1 (If not done yet, catch up!) Questions about HW1? Anyone still stuck on apply / UNIX account? Everyone have the books?
The C programming language: Introduction Fall 2003, Jen-Chang Liu.
Character Input and Output C and Data Structures Baojian Hua
C workshop Yuli Kaplunovsky - Today - Introduction to C Recommended book: The C programming Language / Kernighan & Ritchie.
Introduction to C Systems Programming. Systems Programming: Introduction to C 2 Systems Programming: 2 Introduction to C  A ‘C’ Program –Variable Declarations.
流程控制: while loop 迴圈 Test condition Enter loop Yes (non-0) Execute Loop body no exit F=0 F=F+20 … F=F
0 Arrays (1/2) #include /* count digits, white space, others */ main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i
Functions / Procedures
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Prof. Béat Hirsbrunner Fulvio Frapolli, PhD Student (exercises) Bachelor students : - Major in computer science (first year, 2nd term) - Major in information.
Character Input and Output
Review: midterm #9 #include void main(void) { int c; c = getchar(); if(c>=48){ if(c
C programming---basic 1 Introduction to C 2 C Fundamentals 3 Formatted Input/Output 4 Expression 5 Selection Statement 6 Loops 7 Basic Types 8 Arrays 9.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
A simple C program: Printing a line of text #include main() { printf(“hello, world\n”); } Program output: hello, world.
Introduction to C Language
C Programming A Modern Approach
Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 –HW3 is posted Questions?
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Introduction to C Programming Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University.
By Sidhant Garg.  C was developed between by Dennis Ritchie at Bell Laboratories for use with the Unix Operating System.  Unlike previously.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
Programming I Introduction Introduction The only way to learn a new programming language is by writing programs in it. The first program to.
CSC 270 – Survey of Programming Languages C Lecture 4 – Strings Heavily borrowed from Dr. Robert Siegfried and Dietel How to Program in C Powerpoint Presentations.
Outline Symbolic Constants Character Input and Output if … else switch…case.
C Program Design Functions and Program Structure 主講人:虞台文.
Programming Language  C Tutorial Introduction 主講人:虞台文.
#include int main(void) { printf("Hello, world!\n"); return 0; } entry point called on program start only one main( ) in any program # for preprocessor.
1/16 Programski jezik C Vladimir Filipović
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Functions #include int sum(int i, int j); main() { int a, b, res; a = 1; b = 2; res = sum(a, b); printf("%d + %d = %d\n", a, b, res); } int sum(int i,
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
USER DEFINED FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
1 Homework –Continue Reading K&R Chapter 2 –We’ll go over HW2 at end of class today –Continue working on HW3 Questions?
Prof. Béat Hirsbrunner Ammar Halabi, PhD student (exercises) Dani Rotzetter, Master student (exercises) Bachelor students : Major in computer science (3rd.
Introduction to Computer Organization & Systems Topics: Types in C: floating point COMP C Part III.
1 Homework Done the reading? –K&R –Glass Chapters 1 and 2 Applied for cs240? (If not, keep at it!) Gotten a UNIX account? (If not, keep at it!)
CS 261 C Basics Page 2 1/14/2016 CS 261, WSU Vancouver Primitive Types Notes: 4 A is a constant; B is a variable.
Minimal standard C program int main(void) { return 0 ; }
C Homework Write mypaste.c and mycomm.c – mypaste is like paste & mycomm is like comm – Both take two files as arguments – Paste reads a line from each.
Operating System Discussion Section. The Basics of C Reference: Lecture note 2 and 3 notes.html.
Chapter 1 Basic C Programming
2/22/2016 1R. Smith - University of St Thomas - Minnesota CISC 130: Today’s Class History Paper scheduleHistory Paper schedule RecapRecap Plus PlusPlus.
The Cast Operator The cast operator converts explicitly from one data type of an expression to another. For example, if x is of type int, the value of.
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
C is a high level language (HLL)
CPT: Chars/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to look at how C processes characters 4. Character.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
D. Gotseva PL - Lectures 1 Programming Languages Lectures Assoc. Prof. PhD Daniela Gotseva
Formatted Input and Output
Chap. 2. Types, Operators, and Expressions
C Programming Tutorial – Part I
C programming---basic
CS 1430: Programming in C++.
Structured Programming (Top Down Step Refinement)
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Introduction to C Topics Compilation Using the gcc Compiler
Your questions from last session
Homework Applied for cs240? (If not, keep at it!) 8/10 Done with HW1?
C Programming Getting started Variables Basic C operators Conditionals
Introduction to C Topics Compilation Using the gcc Compiler
Presentation transcript:

Imperative Programming Prof. Béat Hirsbrunner Amine Tafat, PhD Student Matthias Buchs and Raphaël Lesceux, Graduate Students Department of Informatics University of Fribourg Summer Term 2003www.unifr.ch/diuf/pai/ip03

1 Hello World #include main() { printf("hello, world\n"); } % cc hello.c // Compile hello.c %./a.out // Run a.out % cc hello.c -o hello // Compile hello.c %./hello // Run hello (KR p6) Session March 2003

2 Variables and Arithmetic Expressions (1/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)

3 Variables and Arithmetic Expressions (2/2) (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 ); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; }

4 The For Statement #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)

5 Symbolic Constants #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)

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

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

8 Character Counting #include /* count characters in input; 1st version */ main() { long nc; nc = 0; while (getchar() != EOF) ++nc; printf("%ld\n", nc); } (KR p18)

9 Character Counting #include /* count characters in input; 2nd version */ main() { double nc; for (nc = 0; getchar() != EOF; ++nc) ; printf("%.0f\n", nc); } (KR p18)

10 Line Counting #include /* count lines in input */ main() { int c, nl; nl = 0; while ((c = getchar()) != EOF) if (c == '\n') ++nl; printf("%d\n", nl); } (KR p19)

11 #include #define IN 1 /* inside a word */ #define OUT 0 /* outside a word */ /* count lines, words, and characters in input */ main() { int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; while ((c = getchar()) != EOF) { ++nc; if (c == '\n') ++nl; if (c == ' ' || c == '\n' || c == '\t') state = OUT; else if (state == OUT) { state = IN; ++nw; } printf("%d %d %d\n", nl, nw, nc); } Word Counting (KR p20) inout exit init S = {‘ ‘, ‘\n’, ‘\t’} c S c == EOF ++nw : for every state transition out to in

12 Arrays (1/2) #include /* count digits, white space, others */ main() { int c, i, nwhite, nother; int ndigit[10]; nwhite = nother = 0; for (i = 0; i < 10; ++i) ndigit[i] = 0; (KR p22) index ndigit ndigit[2]

13 Arrays (2/2) while ((c = getchar()) != EOF) if (c >= '0' && c <= '9') ++ndigit[c - '0']; else if (c == ' ' || c == '\n' || c == '\t') ++nwhite; else ++nother; printf("digit ="); for (i = 0; i < 10; ++i) printf(" %d", ndigit[i]); printf(", white space = %d, other = %d\n", nwhite, nother); } (KR p22)

14 Functions (1/2) #include int power(int m, int n); /* test power function */ main() { int i; for (i = 0; i < 10; ++i) printf("%d %d %d\n", i, power(2, i), power(-3, i)); return 0; } (KR p24)

15 Functions (2/2) /* power: raise base to n-th power; n >= 0 */ int power(int base, int n) { int i, p; p = 1; for (i = 1; i <= n; ++i) p = p * base; return p; } (KR p24) power (base, n) = base n

16 Arguments – Call by Value /* power: raise base to n-th power; n >= 0; */ /* version 2 */ int power(int base, int n) { int p; for (p = 1; n > 0; --n) p = p * base; return p; } (KR p27)

17 Character Arrays (1/2) #include #define MAXLINE 1000/* maximum input line size */ int getline(char line[], int maxline); void copy(char to[], char from[]); /* print longest input line */ main() { int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ max = 0; while ((len = getline(line, MAXLINE)) > 0) if (len > max) { max = len; copy(longest, line); } if (max > 0) /* there was a line */ printf("%s", longest); return 0; } (KR p29)

18 Character Arrays (2/2) /* getline: read a line into s, return length */ int getline(char s[], int lim) { int c, i; for (i = 0; i < lim-1 && (c = getchar())!= EOF && c!='\n'; ++i) s[i] = c; if (c == '\n') { s[i] = c; ++i; } s[i] = '\0'; return i; } /* copy: copy 'from' into 'to'; assume 'to' is big enough */ void copy(char to[], char from[]) { int i; i = 0; while ((to[i] = from[i]) != '\0') ++i; } (KR p29)

19 Summary #include name #define name replacement text Main() { } Return-type function-name (parameter declarations, if any) { declarations statements } while (expression) statement for (expr1; expr2; expr3) statement if (expr1) statement1 else if (expr2) statement2 … else (exprn) statementn