Download presentation
Presentation is loading. Please wait.
Published byCatherine Wade Modified over 6 years ago
1
CS 108 Computing Fundamentals November 2, 2017
2
Review Quizzes #16 and #17
3
Review Exam #2
4
Posted later today… due on Tuesday.
GHP #19 Posted later today… due on Tuesday.
5
Compare with ITERATION
Recursion Pronunciation: ree-kur-zhun Function: noun Etymology: Late Latin recursion, recursio, from recurrere 1 : RETURN 2 : the determination of a succession of elements (as numbers or functions) by operation on one or more preceding elements according to a rule or formula involving a finite number of steps 3 : a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls itself in a step having a termination condition so that successive repetitions are processed up to the critical step until the condition is met at which time the rest of each repetition is processed from the last one called to the first. Compare with ITERATION
6
Recursion A recursive function is a function that calls itself.
Anything that can be solved iteratively can be also be solved recursively and vice versa. Recursion is sometimes useful because the recursive solution to a problem can sometimes be expressed more simply and succinctly than an iterative solution. Let’s look at a classic example
7
Example: Factorial Main Entry: factorial Function: noun 1 : the product of all the positive integers from 0 to n ; symbol n! 2 : the quantity 0! arbitrarily defined as equal to 1
8
Example: Factorial factorial(0) = 1 (by definition… see previous slide) factorial(1) = 1 * 1 factorial(2) = 2 * 1 *1 factorial(3) = 3 * 2 * 1 *1 factorial(4) = 4 * 3 * 2 *1 *1 factorial(5) = 5 * 4 * 3 * 2 *1 *1 factorial(6) = 6 * 5 * 4 * 3 * 2 *1 *1 factorial(7) = 7 * 6 * 5 * 4 * 3 * 2 *1 *1 factorial(8) = 8 *7 * 6 * 5 * 4 * 3 * 2 *1 *1
9
First: Factorial Via Iteration
#include <stdio.h> // 1.c int factorial ( int ) ; int main (void) { int input = 0 , answer = 0; printf("\n\n The program computes the factorial of an entered integer. ") ; printf("\n Enter an postive integer: ") ; scanf("%d", &input ) ; answer = factorial ( input ) ; printf("\n\n %d factorial equals: %d \n\n" , input , answer ) ; return (0) ; } // continued on next slide
10
First: Factorial Via Iteration
// continued from previous slide int factorial ( int passed_input ) { int index = 1 , result = 1; for ( index = 1 ; index <= passed_input ; index++ ) result = result * index ; } return (result) ;
11
Factorial Via Recursion
factorial(0) = 1 (by definition) factorial(1) = 1 * 1 is the same as 1 * factorial(0) factorial(2) = 2 * 1 * 1 is the same as 2 * factorial(1) factorial(3) = 3 * 2 * 1 * 1 is the same as 3 * factorial(2) factorial(4) = 4 * 3 * 2 * 1 * 1 is the same as 4 * factorial(3) factorial(5) = 5 * 4 * 3 * 2 * 1 * 1 is the same as 5 * factorial(4) factorial(6) = 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 6 * factorial(5) factorial(7) = 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 7 * factorial(6) factorial(8) = 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 8 * factorial(7) Is there a general way to state our observation of factorials (above) into rules for an algorithm using factorial(n) as a starting point?
12
Factorial Via Recursion
factorial(0) = 1 (by definition) factorial(1) = 1 * 1 is the same as 1 * factorial(0) factorial(2) = 2 * 1 * 1 is the same as 2 * factorial(1) factorial(3) = 3 * 2 * 1 * 1 is the same as 3 * factorial(2) factorial(4) = 4 * 3 * 2 * 1 * 1 is the same as 4 * factorial(3) factorial(5) = 5 * 4 * 3 * 2 * 1 * 1 is the same as 5 * factorial(4) factorial(6) = 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 6 * factorial(5) factorial(7) = 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 7 * factorial(6) factorial(8) = 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 * 1 is the same as 8 * factorial(7) If n = = 0 then answer = 1 … factorial(0) = 1 For all other cases, what is the relationship between factorial(n) on the left side of the assignment operator and the formula on the right side? The next slide might be more helpful.
13
Factorial Via Recursion
factorial(0) = 1 (by definition) factorial(1) = 1 * factorial(0) factorial(2) = 2 * factorial(1) factorial(3) = 3 * factorial(2) factorial(4) = 4 * factorial(3) factorial(5) = 5 * factorial(4) factorial(6) = 6 * factorial(5) factorial(7) = 7 * factorial(6) factorial(8) = 8 * factorial(7) For all cases other than n = = 0, what is the relationship between factorial(n) on the left side of the assignment operator and the formula on the right side? factorial ( n ) = ??
14
Factorial Via Recursion
First: answer = 1 if n = = 0 … factorial(0) = 1 Second: otherwise: factorial(n) = n * factorial( n - 1 ) Notice that the identifier "factorial" appears on both sides of the assignment operator How would we write a factorial function to implement these two rules into a C algorithm ?
15
Factorial Via Recursion
First: the solution is 1 if n == 0 Second: the solution is n * factorial( n - 1 ) if n > 0 int factorial ( int n ) { if n = = 0 return 1 ; else return ( n * factorial ( n – 1 ) ); }
16
Understanding Recursion
You can think of a recursive function call as if it were calling a completely separate function Understand this about recursively called functions: the operations that can be performed by recursively called functions are the same, but the data that is input to each recursively called function is different as are the address of recursive function memory objects The next slide might help to visualize the sameness of operations and the differences in data through the use of different (but very, very similar) functions... we’ll pass factorial_a the value 3.
17
Understanding Recursion
int factorial_a ( int m ) { if ( m = = 0 ) return 1; else return m * factorial_b ( m - 1 ) ; } int factorial_b ( int n ) { if ( n = = 0 ) return 1; else return n * factorial_c ( n - 1) ; } int factorial_c ( int q ) { if( q = = 0 ) return 1; else return q * factorial_d ( q - 1) ; } int factorial_d ( int r ) { if( r = = 0 ) return 1; else return r * factorial_e ( r - 1) ; }
18
Let's develop a symbol table for the previous slide which is incorporated in this program (this program can handle 0 – 3 as inputs, which is enough to demonstrate the concepts) : Let's look at the addresses and values of the variables/parameters Notice that each version of the factorial functions does the same thing Therefore, we can condense this into something more general
19
Recursion Example: Factorial
#include <stdio.h> // 2.c int factorial ( int ) ; int main (void) { int input = 0 , answer = 0; printf("\n\n The program computes the factorial of an entered integer. ") ; printf("\n Enter an postive integer: ") ; scanf("%d", &input ) ; answer = factorial ( input ) ; printf("\n\n %d factorial equals: %d \n\n" , input , answer ) ; return 0 ; } int factorial ( int passed_input ) if ( passed_input = = 0) // if cutting, remove the extra space between = and = return 1 ; else return ( passed_input * factorial ( passed_input - 1 ) ) ;
20
Let's develop a symbol table for the previous slide which is incorporated in this program:
21
Questions to Guide The Use of Recursion
Can you define the problem in terms of smaller problem(s) of the same type? Think about the factorial problem: factorial(n) = n * factorial ( n-1 ) if n > 0 Does each recursive call reduce the size of the problem? As the problem "shrinks", will you eventually reach a point where an obvious solution presents itself? Again, think about the factorial problem: factorial (0) = 1
22
Fundamentals of Characters and Strings
Characters in C consist of any printable or nonprintable character in the computer's character set including lowercase letters, uppercase letters, decimal digits, special characters and escape sequences. A character is usually stored in the computer as an 8-bits (1 byte) unsigned integer. The integer value stored for a character depends on the character set used by the computer on which the program is running.
23
Fundamentals of Characters and Strings
There are two commonly used character sets: ASCII (American Standard Code for Information Interchange) EBCDIC (Extended Binary Coded Decimal Interchange Code)
24
Fundamentals of Characters and Strings
There are two commonly used character sets: ASCII (American Standard Code for Information Interchange) EBCDIC (Extended Binary Coded Decimal Interchange Code)
25
Example: ASCII character
#include <stdio.h> // 3a.c int main(void) { char demo_Z = 'Z'; char demo_a = 'a'; printf("\n ASCII integer value for A is %d", 'A' ) ; printf("\n ASCII integer value for Z is %d", demo_Z ) ; printf("\n ASCII integer value for a is %d", demo_a ) ; printf("\n ASCII integer value for z is %d", 'z' ) ; printf("\n\n\n"); printf("\n 65 in ASCII represents %c", ); printf("\n 90 in ASCII represents %c", ); printf("\n 97 in ASCII represents %c", ); printf("\n 122 in ASCII represents %c \n\n", ); return 0 ; }
26
Example (continued) equivalent to #include <stdio.h> //4a.c
int main(void) { char ch; printf("\n\nEnter a character: "); scanf("%c", &ch); if ( ch >= 'A' && ch <= 'Z' ) printf("\nCapital letter.\n"); } return 0 ; #include <stdio.h> //5a.c int main(void) { char ch; printf("\n\nEnter a character: "); scanf("%c", &ch); if ( ch >= 65 && ch <= (65+25) ) printf("\nCapital letter.\n"); } return 0 ; equivalent to
27
Character Type: char In C, characters are indicated by single quotes: char input_char = 'P'; printf("input_char is %c\n", input_char );
28
Strings In C a string is really an array of characters that ends with a special character: the null character ( '\0' ) … we use double quotes: "this is a string" // " " means the \0 is added But you can't use an assignment operator to assign a string to a character array after it has been declared (need to use the string copy function) char demo[80]; demo = "this is a string"; // Unacceptable assignment
29
Strings A null ( '\0' ) character is placed to mark the end of each string String functions use '\0' to locate end of string (so you don't need to pass a string's length as argument to a string function) t s i h a \0 g n r
30
Fundamentals of Characters and Strings
A string in C is an array of characters ending with the null character ( '\0' ). It is written inside a double quotation mark ( " " ) A string may be assigned (in a declaration) to either a char array or to a char pointer (this is a declaration, so the = is allowed): char color[ ] = "green";
31
In C Strings are Character Arrays (logically)
Strings in C, in a logical sense, are arrays of characters which terminate with a delimiter (\0 or ASCII NULL character). char str_1 [ ] = {'H', 'e', 'l', 'l', 'o', '\0'}; char str_2 [10] = {'\0'}; str_2 is a ten-element array that can hold a up to nine characters + \0 Strings in C need the delimeter to determine where the string ends (remember: the length of a string is dynamic… it isn't determined before runtime) It's the delimiter position (not the size of the array) that determines the length of the string
32
Accessing String Characters
Like any array, the first element of a string in C is at index 0. The second is at index 1, and so on … char str_2 [10] ; str_2[0] = 'P'; str_2[1] = 'u'; str_2[2] = 'l'; str_2[3] = 'p'; str_2[4] = '\0'; str_2 now contains the string "Pulp" + \ unused (at this time) "blanks"
33
String Literals String literals are given as a string inside double quotes. You've used string literals many times already printf("Enter an inter value:"); String literals are often used to initialize a string array/pointer variable char str_1[10] = "Roswell"; char str_1 [ ] = "Roswell"; Remember: the NULL is added automatically to the end (if there is space available)
34
Entering Strings with scanf ( )
Use %s format specifier: %s scans up to but does not include next white space %ns scans the next n characters or up to the next white space, whichever is first You only need to provide the starting address of the string with %s Example: scanf ("%s", str_1); scanf ("%2s", str_1); No ampersand (&) necessary when inputting strings (%s) into character arrays! But you an use the & if you like.
35
String Functions Pretty straightforward The text is pretty clear
Practice questions at the end of the chapter are useful Play with your food The textbook and MPL have plenty of easy-to-understand info
36
Arrays Bootcamp
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.