Imperative Programming - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Imperative Programming

Description:

cc hello.c -o hello // Compile hello.c % ./hello // Run hello (KR p6) ... Return-type function-name (parameter declarations, if any) declarations. statements ... – PowerPoint PPT presentation

Number of Views:148
Avg rating:3.0/5.0
Slides: 21
Provided by: bathirb
Category:

less

Transcript and Presenter's Notes

Title: Imperative Programming


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

Summer Term 2003
www.unifr.ch/diuf/pai/ip03
2
Hello World
Session 1 - 18 March 2003
  • include lt stdio.hgt
  • 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)
3
Variables and Arithmetic Expressions (1/2)
  • include ltstdio.hgt
  • / 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 lt upper)
  • celsius 5 (fahr - 32) / 9
  • printf("d\td\n", fahr, celsius)
  • fahr fahr step

(KR p9)
4
Variables and Arithmetic Expressions (2/2)
  • include ltstdio.hgt
  • / 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 lt upper)
  • celsius (5.0/9.0) (fahr - 32.0)
  • printf("3.0f 6.1f\n", fahr, celsius)
  • fahr fahr step

(KR p12)
5
The For Statement
  • include ltstdio.hgt
  • / print Fahrenheit-Celsius table /
  • main()
  • int fahr
  • for (fahr 0 fahr lt 300 fahr fahr
    20)
  • printf("3d 6.1f\n", fahr,
    (5.0/9.0)(fahr-32))

(KR p13)
6
Symbolic Constants
  • include ltstdio.hgt
  • 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 lt UPPER fahr
    fahr STEP)
  • printf("3d 6.1f\n", fahr,
    (5.0/9.0)(fahr-32))

(KR p15)
7
File Copying
  • include ltstdio.hgt
  • / copy input to output 1st version /
  • main()
  • int c
  • c getchar()
  • while (c ! EOF)
  • putchar(c)
  • c getchar()

(KR p16)
8
File Copying
  • include ltstdio.hgt
  • / copy input to output 2nd version /
  • main()
  • int c
  • while ((c getchar()) ! EOF)
  • putchar(c)

(KR p17)
9
Character Counting
  • include ltstdio.hgt
  • / count characters in input 1st version /
  • main()
  • long nc
  • nc 0
  • while (getchar() ! EOF)
  • nc
  • printf("ld\n", nc)

(KR p18)
10
Character Counting
  • include ltstdio.hgt
  • / count characters in input 2nd version /
  • main()
  • double nc
  • for (nc 0 getchar() ! EOF nc)
  • printf(".0f\n", nc)

(KR p18)
11
Line Counting
  • include ltstdio.hgt
  • / count lines in input /
  • main()
  • int c, nl
  • nl 0
  • while ((c getchar()) ! EOF)
  • if (c '\n')
  • nl
  • printf("d\n", nl)

(KR p19)
12
Word Counting
  • include ltstdio.hgt
  • 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)

(KR p20)
13
Arrays (1/2)
  • include ltstdio.hgt
  • / count digits, white space, others /
  • main()
  • int c, i, nwhite, nother
  • int ndigit10
  • nwhite nother 0
  • for (i 0 i lt 10 i)
  • ndigiti 0

(KR p22)
14
Arrays (2/2)
  • while ((c getchar()) ! EOF)
  • if (c gt '0' c lt '9')
  • ndigitc - '0'
  • else if (c ' ' c '\n' c '\t')
  • nwhite
  • else
  • nother
  • printf("digit ")
  • for (i 0 i lt 10 i)
  • printf(" d", ndigiti)
  • printf(", white space d, other d\n",
    nwhite, nother)

(KR p22)
15
Functions (1/2)
  • include ltstdio.hgt
  • int power(int m, int n)
  • / test power function /
  • main()
  • int i
  • for (i 0 i lt 10 i)
  • printf("d d d\n", i, power(2, i), power(-3,
    i))
  • return 0

(KR p24)
16
Functions (2/2)
  • / power raise base to n-th power n gt 0 /
  • int power(int base, int n)
  • int i, p
  • p 1
  • for (i 1 i lt n i)
  • p p base
  • return p

(KR p24)
17
Arguments Call by Value
  • / power raise base to n-th power n gt 0 /
  • / version 2 /
  • int power(int base, int n)
  • int p
  • for (p 1 n gt 0 --n)
  • p p base
  • return p

(KR p27)
18
Character Arrays (1/2)
  • include ltstdio.hgt
  • 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 lineMAXLINE / current input line
    /
  • char longestMAXLINE / longest line saved
    here /
  • max 0
  • while ((len getline(line, MAXLINE)) gt 0)
  • if (len gt max)
  • max len
  • copy(longest, line)

(KR p29)
19
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 lt lim-1 (c getchar())! EOF
    c!'\n' i)
  • si c
  • if (c '\n')
  • si c
  • i
  • si '\0'
  • return i
  • / copy copy 'from' into 'to' assume 'to' is
    big enough /
  • void copy(char to, char from)
  • int i

(KR p29)
20
Summary
  • include name
  • define name replacement text
  • Main()
  • Return-type function-name (parameter
    declarations, if any)
  • declarations
  • statements
Write a Comment
User Comments (0)
About PowerShow.com