CSCI1190: Beginning C Programming for Engineers - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

CSCI1190: Beginning C Programming for Engineers

Description:

CSCI-1190: Beginning C Programming for Engineers. Lecture 3: ... Trigonometric sine of x. sin(x) x raised to power y. pow(x,y) Absolute value of x. fabs(x) ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 23
Provided by: gang3
Category:

less

Transcript and Presenter's Notes

Title: CSCI1190: Beginning C Programming for Engineers


1
CSCI-1190 Beginning C Programming for Engineers
  • Lecture 3 Functions
  • Gang Chen

2
Control Structures
  • if, if-else, switch
  • while, do-while
  • for

3
From 1 to 100 step 1
  • int i1
  • do
  • i
  • while (ilt100)

int i1 while(ilt100) i
for(i1ilt100i)
4
From 1 to 100 step 2
  • int i1
  • do
  • ii2
  • while (ilt100)

int i1 while(ilt100) i2
for(i1ilt100i2)
Question What is the value of i in the last
iteration?
5
From 100 to 1 step -1
  • int i100
  • do
  • i--
  • while (igt0)

int i100 while(igt0) i--
for(i100igt0i--)
6
1,2,4,8,16,32,64,,1024
  • int i1
  • do
  • i2
  • while (ilt1024)

int i1 while(ilt1024) i2
for(i1ilt1024i2)
  • Other sequences
  • 1,-2,3,-4,5,-6,7,-8,
  • 1,1/2,1/4,1/8,1/16,1/32,1/64,
  • 1,1/2,1/3,1/4,1/5,1/6,1/7,
  • 1,2,3,5,8,13,21,34,

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

8
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
  • Either the prototype or the definition must
    appear before the function call

9
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/

10
Type Conversion
  • float power(float a, float b)
  • float x,y,z
  • int i,j
  • power(x) / error /
  • power(x,y,z) / error /
  • power(i,j) / ok /

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

12
Math Functions
  • Header file include ltmath.hgt
  • Compiling gcc Wall o prog prog.c -lm

13
Random Number Generator
  • include ltstdio.hgt
  • include ltstdlib.hgt / srand() and rand() /
  • include lttime.hgt / time() /
  • int main()
  • float r
  • srand(time(NULL))
  • r1.0rand()/RAND_MAX
  • printf("Random number f\n",r)
  • return 0

14
In-Class Exercise 3-1
  • Write a program to generate 1000 floating point
    random numbers between 0 and 1.
  • Find out the largest number, the smallest number
    and the average.

15
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 /
16
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

17
Variable Scopes An Example
  • 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 /
  • return 0

18
Recursion
  • A function can call itself, either directly or
    indirectly through other functions
  • Some conditional code is needed to stop the
    recursion
  • Recursion is equivalent to iteration

19
Factorial
  • The factorial of an integer
  • n!n(n-1)(n-2)21
  • 1!10!1
  • Recursive equation
  • n!n(n-1)!, if n0
  • n!1, if n0

20
Factorial Recursion
  • include ltstdio.hgt
  • int factorial(int n)
  • if(n0) return 1
  • else return nfactorial(n-1)
  • int main()
  • int n
  • printf("Enter n ")
  • scanf("d",n)
  • printf("d! d\n",n,factorial(n))
  • return 0

21
Factorial Iteration
  • int factorial(int n)
  • int i,f
  • for(i1,f1iltni)
  • fi
  • return f

22
In-Class Exercise 3-1
  • Use recursion to implement the power function
    that takes two integers.
  • Hint power(a,b)apower(a,b-1)
Write a Comment
User Comments (0)
About PowerShow.com