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.ece.iastate.edu/~alexs/classes/2009_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 Final Exam Final Exam: Thursday Dec 17: 2:15- 4:15 pm Location: This room. The final is worth 25% of your grade. The best way to fail this class is to not show up.

4 Final Format True/False(10 x 1p each = 10p) Fun Stuff( 3 x 5p each = 15p) Short answers( 5 x 3p each = 15p) Code Snippets( 4 x 5p each = 20p) What is the output( 3 x 5p each = 15p) Program 1(15p) Program 2(20p) Program 3(20p) TOTAL (130p)

5 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

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

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

8 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.

9 © 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; }

10 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;

11 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);

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

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

14 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"); }

15 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); }

16 Execution Trace

17 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”); }

18 Think of recursion as a tree …

19 … an upside down tree

20

21 (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)

22 (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

23 (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

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) 2 0 135 4 Traversal Order

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

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

27 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 ?

28 #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

29 #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

30 #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

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

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

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

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

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

36 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)

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

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

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

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

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

42 The road ahead This class is only the beginning. It taught you only the very basics of programming. It taught you very little about many other things. Continue to write programs (even small ones to help you with your other classes or just for fun). Otherwise you’ll forget it all in 1-2 years!

43 Recommended Books (C language)

44 Recommended Books (The Unix Operating System)

45 Recommended Books (algorithms)

46 Recommended Books (Software Engineering)

47 Recommended Books (C and Unix)

48 Recommended Books (Scientific Computing)

49 Recommended Books (Just for Fun!)

50 THE END


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

Similar presentations


Ads by Google