??T3122/106 G??ssa p????a - PowerPoint PPT Presentation

About This Presentation
Title:

??T3122/106 G??ssa p????a

Description:

3122/106 / ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 29
Provided by: staa153
Category:
Tags: sine | ssa | t3122

less

Transcript and Presenter's Notes

Title: ??T3122/106 G??ssa p????a


1
??T3122/106G??ssa p????aµµat?sµ??
  • S??a?t?se??

2
???????µ?? / ??????µµata
  • ?????µe ??a p??ß??µa e?te???ta? µ?a a???????a ap?
    e?t???? se s???e???µ??? se???
  • ???????µ?? ?a?????e? t?? e?t???? ?a? t? se???
    e?t??es??
  • ??e???? p?????µµat?? (program control) ?a?????e?
    a?st??? t? se??? e?t??es?? t?? e?t????

3
S??a?t?se?? - Functions
  • ???????µe t? p????aµµa se µ???
  • F???s?f?a t?? d?a??e? ?a? ßas??e?e (divide and
    conquer)
  • ?a µ??? a?t?st?????? se ep?µ????? ?a? µ????te?a
    p??ß??µata
  • ??a????a µe µ?a ep??e???s?
  • ? p???st?µe??? a?a??te? µ?a d???e?? se ??p????
    ?f?st?µe??
  • ?a???e? t? ap?t??esµa ????? ?a e?d?af??eta? ??a
    t?? ?ept?µ??e?e?
  • ?a?e????e?e?

4
S??a?t?se?? - Functions
  • ?a ?p?p??ß??µata p?? e????a d?a?e???s?µa
  • ???e ?p?p??ß??µa a?t?st???e? se µ?a s????t?s?
  • ??? e????? d?a?e???s? ??d??a
  • Debugging
  • Updating
  • ???s? ?pa????t?? s??a?t?se?? se ?a??????a
    p?????µµata ??a t? ??s? ???? p??ß??µ?t?? code
    reuse
  • ?pa?a???s?µ?p???s? ??d??a
  • ?p?f??? epa??????? t?? ?d??? ??d??a
  • ?fa??es? / abstraction ???ß??ta? ?ept?µ??e?e?
    p?? de? e??a? apa?a?t?te? s??e??? st??
    p????aµµat?st?

5
S??a?t?se?? - Functions
  • Updating
  • Code reuse

main()
S??a?t?s?a??? ?????aµµat?sµ??
main()
6
??µ?µ???? p????aµµat?sµ??
  • O?????s? t?? p?????µµat?? se s??a?t?se??
    ap??p???s? t?? ??d??a
  • ep??e?t????µaste se ap???ste?e? d?ad??as?e?
  • a?t???µ? ???p???s?, d?????s? ?a? ße?t?st?p???s?
    tµ?µ?t?? t?? ??d??a, a?e???t?ta ap? t? ?p????p?
    p????aµµa
  • ?a ???s?µ?p??e?ta? ap? eµ?? ? ?????? se
    d?af??et??? p?????µµata.
  • ?ta? ?p???e? ap?µ???s? t?? ??d??a se a?t???µ?
    s????t?s?, ? ???s? t?? ap??p??e?ta? s?µa?t???
    ?a??? µa? apas???e? µ??? t? p?? t?? ?a???µe ?a?
    t? ???sµata p??pe? ?a pe??s??µe st? s????t?s?
    ?a? ??? t? p????? a???ß?? ?p?????sµ??? e?te?e?
    (µa??? ???t?).
  • ? ??????s? t?? ??d??a se ded?µ??a ?a? se
    d?ad??as?e? p?? ep?d???? se a?t? pe?????feta? ??
    d?µ?µ???? (structured) ? d?ad??ast????
    (procedural) p????aµµat?sµ??.

7
???sµ?? S??a?t?se??
  • ???µa-s????t?s?? ?p???d?p?te de?t? ???µa
  • ?p?st?ef?µe???-t?p?? t?p?? t?? ded?µ???? p??
    ep?st??fe? ? s????t?s?
  • void de? ep?st??fe? t?p?ta
  • ??sta-pa?aµ?t??? d???se?? pa?aµ?t???, ???????ta?
    µe ??µµa (t?p?? pa??µet???, t?p?? pa??µet???,
    ...)
  • ?p?st?ef?µe???-t?p?? ???µa-s????t?s??
    (??sta-pa?aµ?t???)
  • ????se?? µetaß??t??
  • ??t????

int SquareIt(int a) int result result
aa return result
8
???s? S??a?t?se?? Function Call
  • ???µa_s????t?s?? ( ???sµa1, ..., ???sµa?)
  • ??f?as?1 ? ???µa_s????t?s?? ( ??f?as?1 ...,
    ??f?as??)
  • t?µ?1 ? ? t?µ? p?? ep?st??fe? ? ??d??a? t??
    s????t?s?? ?ta? e?te?este? µe ta d?sµ??a
    ???sµata
  • t?p??1 ? ? t?p?? p?? ep?st??fe? ? s????t?s?
  • x (SquareIt(5) gt 7)
  • ? ??f?as? SquareIt(5)a?t??a??stata? ap? t??
    ??f?as? 25.

9
S??a?t?se?? void
  • ? ep?st?ef?µe??? t?p?? µp??e? ?a e??a? void.
    ?????eta? ?ts? ?t? de? ep?st??feta? t?µ?.
  • ? ??sta ???sµ?t?? µp??e? ?a e??a? ?e?? ?,
    ?s?d??aµa, ?a pe????e? t? ???? void.
  • void f(int x)
  • printf(Test d\n,x)
  • return
  • ?????
  • x f() 10

10
???s?
  • ?? ???µ? t??, µ?sa se ?e???? pa?e??se??, ap?
    p?s?t?te? ?at??????? t?p?? ?ste ?a a?t?st??????
    sta ???sµat? t??, ? ?a µp????? ?a µetat?ap??? se
    a?t?.
  • (G?a t??a) ?? p?s?t?te? a?t?? p?epe? ?a e??a?
    a???ß?? t?se? ?sa ?a? ta ???sµata.

11
Call by value
  • ??t???af? t?? t?µ?? t?? ???sµat?? pe???e? st?
    s????t?s?
  • ???a??? st? ???sµa (a?t???af?) de? ep??e????? t?
    p??t?t?p?
  • ???s? ?ta? ? s????t?s? ??? ??e???eta? ?a a????e?
    t? ???sµa
  • ?p?fe????µe a??a??? ?at? ?????

/?p?st??fe? t? µ???st? µeta?? t?? x, y/ int
max(int x,int y) int z x /d???se??
µetaß??t??/ if (y gt x) /??sta e?t????/
z y return z
12
Call by value
  • ???s?µ? st? d?µ?µ??? p????aµµat?sµ? ?a???µe µ?a
    s????t?s? ????? t?? ???d??? ?a µa? a????e? t??
    t?µ?? t?? µetaß??t?? µa?
  • ??d????? ?a p??pe? ?a t? ?aµß????µe ???e f??? ?p
    ????

13
???S??OS
14
Call by reference
  • T????µe ?a a??????µe t?µ? sta ???sµata t??
    s????t?s??
  • ?e???µe t? d?e????s? t?? µetaß??t??
  • ????e???µe p??? sta ?d?a ta ded?µ??a ?a? ???
    sta a?t???afa t???
  • ???pe? ?a ?aµß????µe ?p ???? t?? ep?d?as? t??
    s?et???? ???se??

15
??? t?? ??????µµat??
  • ? ??? t?? p?????µµat?? a????e? ?ta? s??a?t?se?
    t?? ???s? µ?a? s????t?s??
  • f(a, b, c)
  • ???ta ?p????????ta? ?? e?f??se?? p?? a?t?st??????
    sta ???sµata
  • ?? t?µ?? t?? e?f??se?? a, b, c a?t????f??ta? sta
    ???sµata t?? f µe t?? ?d?a se???
  • ? ??e???? µetap?d? st?? p??t? e?t??? t?? f
  • ?? e?t???? t?? f e?te????ta? µ???? ?a s??a?t??e?
  • return ??f?as?
  • ????? s????t?s?? (d??ad? )
  • ? ???s? f(a, b, c) a?t??a??stata? µe t??
    ep?st?ef?µe?? t?µ?

16
S??a?t?se?? ß?ß????????
  • S??a?t?se?? ???sµ??e? ap? t? ???st? (user defined
    functions)
  • S??a?t?se?? ß?ß???????? (library functions)
  • ? t?p??? ß?ß??????? t?? C
  • ß?ß??????e? s??a?t?se?? st? d?ad??t??

17
? ß?ß??????? ltmath.hgt
  • double sqrt(double x) ?p??????e? t??
    tet?a?????? ???a t?? x, p??pe? x gt 0
  • double exp(double x) ?p??????e? t?? e??et???
    t?? x, ex
  • double log(double x) ?p??????e? t?? f?s???
    ???????µ? t?? x, ln(x)
  • int abs(int val) ?p??????e? t?? ap???t? t?µ?
    t?? val
  • double pow(double x, double y)  ?p??????e? t??
    t?µ? t?? x ???µ??? st? d??aµ? y, ?y
  • double sin(double x) ?p??????e? t? sine t?? x,
    se rad
  • double cos(double x) ?p??????e? t? cosine t??
    x, se rad
  • double tan(double x) ?p??????e? t?? efapt?µ???
    t?? x, se rad
  • double fmod(double x, double y) ?p??????e? t?
    de?ad??? ?p????p? t?? d?a??es?? ?/y, y gt 0.
  • double ceil(double x) ?p??????e? t?? µ????te??
    a???a?a t?µ? ??? µ????te?? ap? x.
  • double floor(double x) ?p??????e? t??
    µe?a??te?? a???a?a t?µ? ??? µe?a??te?? ap? ?.

G?a ?a ?????µe compile p????aµµa p?? ???s?µ?p??e?
t?? math.h gtgcc lm program.c
include ltstdlib.hgt int rand(void) void
srand(unsigned int seed) ? s????t?s? rand
ep?st??fe? ?e?d?t??a???? a???µ??? ap? 0 ???
RAND_MAX ? s????t?s? srand ???s?µ?p??e?ta? ??a
t?? a?????p???s? t?? rand() ?a? t? ?????s?
?e?d?t??a??? a???µ??. ?a???µe µ?a f??? t??
srand ?a? µet? t?? rand d?ad????? ??a t??
pa?a???? t?? t??a??? a???µ??. ?? ?a?a?a??s??µe
t?? srand ?a? µet? t?? rand ?a p???a??s??µe t??
?d?a se??? t??a??? a???µ??.
18
???sµ?? S??a?t?se??
  • ????se?? µetaß??t?? µ?sa se blocks ??????S
    µetaß??t??
  • S??a?t?se?? ??? µp????? ?a ???st??? µ?sa se ???e?
    s??a?t?se??
  • ??????????? s????t?s? µp??e? ?a ?a??se? ???e?
    s??a?t?se?? ?a? t?? ea?t? t?? (a?ad??µ?)

19
???t?t?pa S??a?t?se?? (Function Prototypes)
  • ?p?st?ef?µe???-t?p?? ???µa-s????t?s?? (??sta
    t?p??-pa?aµ?t???)
  • (???-) ????se?? t?? s??a?t?se?? st?? a??? t??
    a??e???
  • ??e???eta? a? ???s?µ?p????µe t? s????t?s? p???
    t?? ???sµ? t??
  • ??e??e?t?µata
  • ???????µe t?? ???? ???s? t?? s??a?t?se?? (p???
    a??µ? t?? ???p???s??µe)
  • ?p????µe ?a d??µe p??e? e??a? ?? s??a?t?se?? p??
    ????? ???p????e? st? p????aµµa
  • ?p????µe ?a ?a??s??µe ??e? t?? s??a?t?se?? p??
    ????? ???p????e? st? p????aµµa ????? ?a µa?
    e?d?af??e? ? se??? p?? ????? ??afte?

20
  • 1. Function prototype (3 parameters)
  • 2. Input values
  • 2.1 Call function
  • Function definition
  • Program Output

Enter three integers 22 85 17 Maximum is 85
21
S??a?t?se??
  • ?p????????a µe t? ?p????p? p????aµµa
  • ?????ta? ???sµata
  • ?p?st??f??? µ?a t?µ?
  • ?p?µ??????? t?? ep???s? t?? ?p?p??ß??µat??
  • ?da????, de? ?????µe ?aµ?a ???? ep?d?as? st?
    ?p????p? p????aµµa p??a? t?? pa?ap???
  • S??a?t?s?a??? p????aµµat?sµ?? (functional
    programming)
  • ??d??eta? ?a ep??e?s??? ?? a????? t? p????aµµ?
    µa? a? ???s?µ?p????µe ?a??????? µetaß??t??,
    de??te?, ?t?
  • ?a?e????e?e?

22
S??a?t?se?? ?a? ?etaß??t??
  • ?d??t?te? µetaß??t??
  • ?µß??e?a (scope)
  • ???a µ??? t?? p?????µµat?? a?a????????? t?? ?d?a
    µetaß??t? (?d?a ??s? µ??µ??) µe t? ?d?? ???µa
  • ?????e?a ?pa???? st? µ??µ? (storage duration)
  • ??te µ?a s???e???µ??? d?e????s? µ??µ?? ?aµß??e?
    ??a s???e???µ??? ???µa ?a? p?te ap?desµe?eta?
    a?t? ? a?t?st????s?
  • ???? t? pa??? ?a a????s??µe t?? pe??pt?s? ?a
    ????µe ????se? t? p????aµµa se d?af??et??? a??e?a

23
?µß??e?a
  • ??a µetaß??t? µp??e? ?a ??e? eµß??e?a
  • ?a?????? se ??? t? p????aµµa e??? a??e???
  • ??p??? µ?sa st?? ???sµ? µ?a? s????t?s??
  • ??p??? µ?sa se ??a block
  • int foobar
  • int
  • fun()
  • int i, foo
  • int bar
  • for (i0iltNi)
  • int bar

24
?a??????? (global) µetaß??t??
  • ???????ta? (s?????? p??? ?a?) ??? ap? ???e
    s????t?s?
  • int global1, global2
  • int main(void)
  • global1 5
  • ?µß??e?a p??spe??s?µe? ap? ???e ???? s????t?s?
    µet? t?? d???s? t???
  • ?????e?a ?pa???? ap? t?? a??? t?? e?t??es??
    µ???? t? t???? ???? t?? p?????µµat??

25
?a??????? (global) µetaß??t??
  • ???s?µe? ?ta? p????? s??a?t?se?? µ???????ta?
    ????? ded?µ??a
  • de? e??a? p?a?t??? ?a ta pe???µe ?? ???sµata ???e
    f???
  • ? ???s? µ?a? s????t?s?? µp??e? ?a a????e? t??
    ?at?stas? ?a? ??a t?? ?p????pe?.
  • ??s???? ?aµ?? f??? ?a ßeßa??s??µe ?t? ?? a??a???
    ?a ?????? µe s?st? se???
  • ???pe? ?a ?aµß????µe ?p ???? p????? pe??pt?se??
    ta?t?????a
  • ??s???? ? epa?a???s?µ?p???s? t??p?p???s? t??
    p?????µµat??
  • ???F?G??? t?? ?a??????? µetaß??t?? a? de? e??a?
    apa?a?t?te?!

26
??p???? (local) ?etaß??t??
  • ????s? µ?sa se µ?a s????t?s?/block
  • int main(void)
  • int local1, local2
  • local1 5
  • int local3
  • local3 7
  • ?µß??e?a p??spe??s?µe? µ??? µ?sa st? ?d?a
    s????t?s?/block
  • ?????e?a ap? t?? a??? µ???? t? t???? t??
    e?t??es?? t?? s????t?s??/block

27
??p???? (local) µetaß??t??
  • ???s?µe? ??a t?? pe??ss?te?e? pe??pt?se??
  • S?????? ?????µe µetaß??t?? ??a ap????e?s?
    e?d??µes?? ap?te?esµ?t??. ?? te?????
    ?p?????sµ?? ?ata?????? s?????? se µ?a t?µ?, a?t??
    p?? ep?st??fe? ? s????t?s?
  • ???spa??ste ?a ???s?µ?p??e?te µ??? t?p????
    µetaß??t?? ?a? ?a s??fteste ??se?? p?? de?
    ???s?µ?p????? ?a??????? µetaß??t??
  • ???s?µ? st? d?µ?s? t?? p?????µµat?? µe ap???
    s??a?t?se??

28
?etaß??t??
  • ?a??????? (global) ??a?t? t?p???? µetaß??t??
    (local)
  • double sum //?a???????
  • int nextOfX(int x)
  • int y x //t?p???
  • y y 1
  • sum sum1
  • return y
  • main()
  • int x 0 //t?p???
  • sum 0
  • x nextOfX(0)
  • x nextOfX(x)

?p?????? ?s? t???e? ? s????t?s?
?p?????? ?s? t???e? t? p????aµµa
Write a Comment
User Comments (0)
About PowerShow.com