Presentation is loading. Please wait.

Presentation is loading. Please wait.

Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)

Similar presentations


Presentation on theme: "Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)"— Presentation transcript:

1 Instructor: Alexander Stoytchev http://www.cs.iastate.edu/~alex/classes/2008_Fall_185/ CprE 185: Intro to Problem Solving (using C)

2 Review for the Final Exam CprE 185: Intro to Problem Solving Iowa State University, Ames, IA Copyright © Alexander Stoytchev

3 Administrative Stuff HW 10 due today @ 8pm! Ritika will not have office hours this week There will be labs this week (also TA evaluations)

4 Final Exam Final Exam: Tuesday Dec 16: 9:45am- 11:45am Location: This room.

5 Final Exam Final Exam: Tuesday Dec 16: 9:45am- 11:45am Location: This room. The final is worth 25% of your grade. The best way to fail this class is to not show up.

6 Final Format (Tentative) True/False(10 x 1p each = 10p) Short answer( 5 x 3p each = 15p) Code Snippets( 4 x 5p each = 20p) What is the output( 2 x 5p each = 10p) Program 1(20p) Program 2(25p) Program 3(30p) TOTAL (130p)

7 Final Format You don’t need to get all 130 points to get an A 100 is a 100 You must get at least 65 points in order to pass this exam

8 Ways to get an ‘A’ on the Final Option 1: –3 programs (75p) –True/False (10p) –Short Answers (15p) –TOTAL: (100p) Option 2: –2 programs(45p) –True/False(10p) –Short Answers(15p) –Code Snippets(20p) –What is the output(10p) –TOTAL: (100p)

9 General Topics for the Final Everything since midterm2 –Recursion –Stuctures –Strings –Files –Pointers

10 General Topics for the Final Everything since midterm2 –Recursion –Stuctures –Strihngs –Files –Pointers And everything before midterm2. No topic is off limits!

11 How to Study for the Final Form a study group. (this will help you overcome your fear) Go back and look at my slides for this class. Go back and look at the sample programs that were demonstrated during the lectures. Programs you wrote during the labs. Homework problems (solve them again). Exercise. And get some sleep.

12 © 2004 Pearson Addison-Wesley. All rights reserved Recursive Programming // This function returns the sum of 1 to num int sum (int num) { int result; if (num == 1) result = 1; else result = num + sum (n-1); return result; }

13 sum(1); sum(1)sum(2) Recursive Control Flow In Recursive calls methods can call themselves, but typically with different arguments each time return 1;

14 Recursive Control Flow In Recursive calls methods can call themselves, but typically with different arguments each time sum(2) sum(1) sum(1); sum(2); sum(3) main() return 1; sum(3);

15 © 2004 Pearson Addison-Wesley. All rights reserved Recursive Programming main sum sum(3) sum(1) sum(2) result = 1 result = 3 result = 6

16 Recursion: Fibonacci Numbers The sequence: {0,1,1,2,3,5,8,13,...}

17 Fibonacci Sequence #include int fib(int n) { int ret; if (n < 2) { ret = 1; } else{ ret= fib(n-1) + fib(n-2); } return ret; } int main() { int i; for(i=0;i<10;i++) printf("%d ", fib(i)); system("pause"); }

18 Mathematical notation v.s. C code int fib(int n) { if(n <= 1) return n; //base case else return fib(n-1) + fib(n-2); }

19 Execution Trace

20 Mystery Recursion void mystery1(int a, int b) { if (a <= b) { int m = (a + b) / 2; printf(“%d “, m); mystery1(a, m-1); mystery1(m+1, b); } } int main() { mystery1(0, 5); system(“pause”); }

21 Think of recursion as a tree …

22 … an upside down tree

23

24 (a=0, b=5) (a=0, b=1)(a=3, b=5) (a=0, b=-1)(a=1, b=1)(a=3, b=3)(a=5, b=5) (a=1, b=0)(a=2, b=1)(a=3, b=2)(a=4, b=3)(a=6, b=5)(a=5, b=4)

25 (a=0, b=5) (a=0, b=1)(a=3, b=5) (a=0, b=-1)(a=1, b=1)(a=3, b=3)(a=5, b=5) (a=1, b=0)(a=2, b=1)(a=3, b=2)(a=4, b=3)(a=6, b=5)(a=5, b=4) 2 m=(a+b)/2 => print 2

26 (a=0, b=5) (a=0, b=1)(a=3, b=5) (a=0, b=-1)(a=1, b=1)(a=3, b=3)(a=5, b=5) (a=1, b=0)(a=2, b=1)(a=3, b=2)(a=4, b=3)(a=6, b=5)(a=5, b=4) 2 0 135 4

27 (a=0, b=5) (a=0, b=1)(a=3, b=5) (a=0, b=-1)(a=1, b=1)(a=3, b=3)(a=5, b=5) (a=1, b=0)(a=2, b=1)(a=3, b=2)(a=4, b=3)(a=6, b=5)(a=5, b=4) 2 0 135 4 Traversal Order

28 Declaring Strings char c[10] = “EE/CprE”; //only 7 chars What about the uninitialized characters in the string?

29 Array contents Contents of the c array c E E / C p r E \0 ? ? NULL character automatically assigned

30 Array Contents Program: // character array char c[10] = “EE/CprE”; printf(“%s”, c); //output 1 c[8] = ‘a’; printf(“%s”, c); //output 2 _________________________ Output 1: EE/CprE Output 2: EE/CprE c E E / C p r E \0 a ?

31 #include … char c[7]; strcpy(c, “Wednesday”); Assign value to a String Watch out for overflow (bad) c W e d n e s d a \0 y overflow a y

32 #include … char c[7]; strncpy(c, “Wednesday”, 7); Assign value to a String Better to use strncpy  copies up to n characters from the source c W e d n e s d need NULL

33 #include … char c[7]; strncpy(c, “Wednesday”, 6); c[6] = ‘\0’; //OR c[6] = NULL; //NULL and ‘\0’ are the same Assign value to a String Better to use strncpy  assign NULL to the end afterword c W e d n e s \0

34 Basic pointer operations int i = 5; Int* ip = &i; printf("%d\n", *ip); [http://www.eskimo.com/~scs/cclass/notes/sx10a.html]

35 Basic pointer operations *ip = 7; [http://www.eskimo.com/~scs/cclass/notes/sx10a.html]

36 Basic pointer operations int j = 3; ip = &j; [http://www.eskimo.com/~scs/cclass/notes/sx10a.html]

37 Basic pointer operations int *ip2; ip2 = ip; [http://www.eskimo.com/~scs/cclass/notes/sx10a.html]

38 Basic pointer operations ip = &i; [http://www.eskimo.com/~scs/cclass/notes/sx10a.html]

39 Pointers and arrays Array indexing: a[0], a[1], a[2], … int *ip = &a[0]; Pointer indexing: *ip, *(ip+1), *(ip+2), … In general a[i] is “equivalent” to *(ip+i)

40 Pointer Arithmetic ip2 = ip1 + 3; ip2 - ip1 == 3 (true or false?)

41 int *ip; int a[10]; ip = &a[3]; ip2 = ip + 1; [http://www.eskimo.com/~scs/cclass/notes/sx10a.html]

42 What is the meaning of this? typedef struct node { int value; struct node *next; } node_t; valuenext ?

43 Inserting a node into a Linked List BeforeAfter [http://en.wikipedia.org/wiki/Linked_list]

44 Deleting a node from a linked list Before: After: [http://en.wikipedia.org/wiki/Linked_list]

45 THE END


Download ppt "Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)"

Similar presentations


Ads by Google