Introduction to algorithms and data structures in C - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Introduction to algorithms and data structures in C

Description:

Introduction to algorithms and data structures in C lectures 7 and 8. MSc Bioinformatics. Introduction to algorithms and data structures in C. jesus.ibanez_at_upf.edu ... – PowerPoint PPT presentation

Number of Views:96
Avg rating:3.0/5.0
Slides: 20
Provided by: sga63
Category:

less

Transcript and Presenter's Notes

Title: Introduction to algorithms and data structures in C


1
Introduction to algorithms and data structures in
C
  • jesus.ibanez_at_upf.edu

2
Functions
  • Functions are modules with parameters (or
    arguments), statements, and a result
  • Why functions?
  • Decomposition
  • Code reuse

3
Functions
  • long int factorial(int n)
  • int i
  • long int f1
  • for (inigt2i--)
  • ffi
  • return f

4
Calling functions
  • Variable(s) must match the parameter(s)
  • Same number, same order, compatible type,
  • Names could be different
  • How about printf, scanf?
  • printf(hello)printf(ad,a)
  • Variable-length parameter list
  • int printf(char, ) /stdio.h/

5
Calling functions
  • include ltstdio.hgt
  •  
  • long int factorial(int n)
  • int i
  • long int f1
  • for (inigt2i--)
  • ffi
  • return f
  •  
  • main()
  • int x5
  • long int res
  • resfactorial(x)
  • printf("d\n",res)

6
Calling functions
  • include ltstdio.hgt
  • int major(int a, int b)
  • if (agtb) return a
  • else return b
  • main()
  • int a3,b5,c
  • cmajor(b,a)
  • printf("d", c)

7
Call by value
  • The execution of a function does not affect the
    value of variables passed as parameters
  • void increment(int a)
  • a
  • int main()
  • int i1
  • increment(i)
  • printf("id\n",i) / i1, not 2 /

8
Variable scopes
  • Declarations must appear at top of their block
  • Function block, if/while block, arbitrary block
  • Before any non-declaration statements

Will not compile int myftn() int i
10 i int j 12 j
Will compile int myftn() int i 10 i
11 int j 12 j
Will compile int myftn() int i 10 int
j i 11 j 12 j
9
Variable scopes
  • A scope of a variable is the portion of the
    program where the variable can be recognized
  • File scope
  • global variables
  • Function scope
  • local variables, parameters
  • Block scope
  • local variables

10
Variable scopes
  • include ltstdio.hgt
  • int x / global variable /
  • int f(float x) / parameter /
  • return 0
  • int g()
  • int x / local variable /
  • return 0
  • int main()
  • float x / local variable /
  • int x / local variable /

11
Prototypes and definitions
  • A prototype declares a function with a return
    type and a parameter list.
  • float power (float a, float b)
  • A definition implements the function
  • float power (float a, float b)
  • float s
  • / calculate the power of a and b /
  • / put the result into the variable s/
  • return s
  • The definition must match the prototype

12
Prototypes and definitions
  • include ltstdio.hgt
  •  
  • long int factorial(int n)
  •  
  • main()
  • int x5
  • long int res
  • resfactorial(x)
  • printf("d\n",res)
  •  
  • long int factorial(int n)
  • int i
  • long int f1
  • for (inigt2i--)
  • ffi
  • return f

Either the prototype or the definition must
appear before the function call
13
Functions
  • Functions can call functions
  • Example

14
Functions
  • include ltstdio.hgt
  •  
  • long int factorial(int n)
  • int i
  • long int f1
  • for (inigt2i--)
  • ffi
  • return f
  •  
  • long int combinatori(int m, int n)
  • return factorial(m)/(factorial(n)factorial(m-n))
  •  ...

15
Functions
  • ...
  • main()
  • int m,n
  • printf(Enter m ")
  • scanf("d",m)
  • printf(Enter n ")
  • scanf("d",n)
  • printf("d\n",combinatori(m,n))

16
Functions (void)
  • For functions taking no parameters
  • int f(void)
  • For functions returning no values
  • void g(void)

17
Functions (void)
  • ...
  • void print_result(int m, int n, int c)
  • printf("d over d is d\n",m,n,c)
  •  
  • main()
  • int m,n,c
  • printf(Enter m ")
  • scanf("d",m)
  • printf(Enter n ")
  • scanf("d",n)
  •  
  • ccombinatori(m,n)
  • print_result(m,n,c)

18
Operator precedence
 
 
19
Short-cut assignments
Write a Comment
User Comments (0)
About PowerShow.com