recursion, c and beyond "… but it clearly is the only right way. the fact that everybody else...
TRANSCRIPT
Recursion, C and Beyond
"… but it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong!"
--Linus Torvalds
BYU CS 124 Advanced C 2
CS 224
Chapter Project HomeworkS00: Introduction
Unit 1: Digital Logic
S01: Data TypesS02: Digital Logic
L01: Warm-upL02: FSM
HW01HW02
Unit 2: ISA
S03: ISAS04: MicroarchitectureS05: Stacks / InterruptsS06: Assembly
L03: BlinkyL04: MicroarchL05b: Traffic LightL06a: Morse Code
HW03HW04HW05HW06
Unit 3: C
S07: C LanguageS08: PointersS09: StructsS10: I/O, Recursion
L07b: Morse IIL08a: LifeL09b: Snake
HW07
HW08
Advanced C 3BYU CS 124
Learning Objectives…
Recursion Factorial
Running Sum Binary Search Fibonacci Numbers Integer to ASCII Line Draw
Activation Records
Find and use standard C functions.
Interpret a data stream according to a specified format.
Direct character output to a data stream.
Assign a data stream to an I/O device, file, or memory.
Students will be able to: Topics:
Advanced C 4BYU CS 124
Recursion
The idea behind recursion is simple. (The visualization is difficult!)
A recursive function is one that performs its task by calling itself on smaller pieces of the same task.
When applied correctly, recursion can simplify programming tasks.
“Build a wall that is ten feet high.” First build a 9 foot high wall, and then add an extra foot of bricks. Conceptually, this is like saying the "build wall" function takes a
height and if that height is greater than one, first calls itself to build a lower wall, and then adds one a foot of bricks.
Recursive tasks must have a “base case”. Recursion is an iterative programming construct.
Recursion
Advanced C 5BYU CS 124
Recursion
Factorial is the classic example: 6! = 6 5! 6! = 6 5 x 4!… 6! = 6 5 4 3 2 1
The factorial function can be easily written as a recursive function:
int Factorial(int n) { if (n < 2) return 1; /* base case */ return (n * Factorial(n – 1));}
Recursion
Advanced C 6BYU CS 124
Binary Search
Given a sorted set of exams, in alphabetical order,find the exam for a particular student.
1. Look at the exam halfway through the pile. 2. If it matches the name, we're done. 3a. else if the name is greater (alphabetically), then
search the upper half of the stack.3b. else search the lower half of the stack.
Binary Search
FindExam(studentName, start, end){
halfwayPoint = (end + start)/2;if (end < start)
ExamNotFound(); /* exam not in stack */else if (studentName == NameOfExam(halfwayPoint))
ExamFound(halfwayPoint); /* found exam! */else if (studentName < NameOfExam(halfwayPoint))
FindExam(studentName, start, halfwayPoint - 1); /* search lower half */else FindExam(studentName, halfwayPoint + 1, end); /* search upper half */
}
Advanced C 7BYU CS 124
Fibonacci Numbers
f(n) = f(n-1) + f(n-2)f(0) = 1f(1) = 1
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987…
Fibonacci Numbers
int Fibonacci(int n){ if (n <= 1) return 1; /* base case */ return (Fibonacci(n-1) + Fibonacci(n-2));}
Advanced C 8BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 9BYU CS 124
Fibonacci(4)
Fibonacci(3)
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Call Tree for Fibonacci
Advanced C 10BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2)
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 11BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2)
Fibonacci(1)
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 12BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2)
Fibonacci(1)1
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 13BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2)
Fibonacci(1) Fibonacci(0)1
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 14BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2)
Fibonacci(1) Fibonacci(0)1 1
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 15BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2)
Fibonacci(1) Fibonacci(0)
+1 1
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 16BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2)
Fibonacci(1) Fibonacci(0)
+1 1
2
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 17BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2) Fibonacci(1)
Fibonacci(1) Fibonacci(0)
+1 1
2
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 18BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2) Fibonacci(1)
Fibonacci(1) Fibonacci(0)
+1 1
12
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 19BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2) Fibonacci(1)
Fibonacci(1) Fibonacci(0)
+
+
1 1
12
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 20BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2) Fibonacci(1)
Fibonacci(1) Fibonacci(0)
+
+
1 1
12
3
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 21BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2) Fibonacci(1)
Fibonacci(2)
Fibonacci(1) Fibonacci(0)
+
+
1 1
12
3
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 22BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2) Fibonacci(1)
Fibonacci(2)
Fibonacci(1)
Fibonacci(1) Fibonacci(0)
+
+
1 1
12
3
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 23BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2) Fibonacci(1)
Fibonacci(2)
Fibonacci(1)
Fibonacci(1) Fibonacci(0)
+
+
1 1
12
3
1
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 24BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2) Fibonacci(1)
Fibonacci(2)
Fibonacci(1) Fibonacci(0)
Fibonacci(1) Fibonacci(0)
+
+
1 1
12
3
1
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 25BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2) Fibonacci(1)
Fibonacci(2)
Fibonacci(1) Fibonacci(0)
Fibonacci(1) Fibonacci(0)
+
+
1 1
12
3
1 1
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 26BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2) Fibonacci(1)
Fibonacci(2)
Fibonacci(1) Fibonacci(0)
Fibonacci(1) Fibonacci(0)
+
+ +
1 1
12
3
1 1
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 27BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2) Fibonacci(1)
Fibonacci(2)
Fibonacci(1) Fibonacci(0)
Fibonacci(1) Fibonacci(0)
+
+ +
1 1
12
3
1 1
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
2
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 28BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2) Fibonacci(1)
Fibonacci(2)
Fibonacci(1) Fibonacci(0)
Fibonacci(1) Fibonacci(0)
+
+
+
+
1 1
12
3
1 1
2
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 29BYU CS 124
Call Tree for Fibonacci
Fibonacci(4)
Fibonacci(3)
Fibonacci(2) Fibonacci(1)
Fibonacci(2)
Fibonacci(1) Fibonacci(0)
Fibonacci(1) Fibonacci(0)
+
+
+
+
1 1
12
3
1 1
2
main()
int Fibonacci(int n){ if (n==0 || n ==1) return 1; else return (Fibonacci(n-1) + Fibonacci(n-2));}
5
int main(){ int x;
x = Fibonacci(4);}
Fibonacci Numbers
Advanced C 30BYU CS 124
Activation Records
A new activation record is pushed on the stack for each subroutine call.
Fib(1)
SP
Fib(2)
Fib(3)
main
main calls Fibonacci(3)
Fibonacci(3) calls Fibonacci(2)
Fibonacci(2) calls Fibonacci(1)
SP
Fib(3)
main
SP
Fib(2)
Fib(3)
main
Activation Records
Advanced C 31BYU CS 124
Activation Records
Fibonacci(1) returns,Fibonacci(2) calls
Fibonacci(0)
Fibonacci(2) returns,Fibonacci(3) calls
Fibonacci(1)
Fibonacci(3)returns
SP
main
SP
Fib(1)
Fib(3)
main
Fib(0)
SP
Fib(2)
Fib(3)
main
Activation Records
Advanced C 32BYU CS 124
MSP430 Fibonacci Fibonacci:0xa064: 120A PUSH R100xa066: 8321 DECD.W SP0xa068: 4C81 0000 MOV.W R12,0x0000(SP)0xa06c: 93A1 0000 CMP.W #2,0x0000(SP)0xa070: 3402 JGE (C$L1)0xa072: 431C MOV.W #1,R120xa074: 3C09 JMP (C$L2) C$L1:0xa076: 831C DEC.W R120xa078: 12B0 A064 CALL #Fibonacci0xa07c: 4C0A MOV.W R12,R100xa07e: 412C MOV.W @SP,R120xa080: 832C DECD.W R120xa082: 12B0 A064 CALL #Fibonacci0xa086: 5A0C ADD.W R10,R12 C$L2:0xa088: 5321 INCD.W SP0xa08a: 413A POP.W R100xa08c: 4130 RET
Activation Records
Recursive call
R12 = Fibonacci(n-1)
Base case
R12 = Fibonacci(n-2)
Pop activationrecord, restoreCallee-safe
Advanced C 33BYU CS 124
Printing an Integer
Recursively converts an unsigned integer as a string of ASCII characters. If integer <10, convert to char and print. Else, call self on first (n-1) digits and then print last
digit.
void IntToAscii(int num) { int prefix, currDigit;
if (num < 10) putchar(num + '0'); /* print single char */else{ prefix = num / 10; /* shift right one digit */
IntToAscii(prefix); /* print shifted num */currDigit = num % 10;putchar(currDigit + '0'); /* print shifted digit */
}}
Integer to ASCII
Advanced C 34BYU CS 124
Trace of IntToAscii
Calling IntToAscii with parameter 12345:
IntToAscii(12345) IntToAscii(1234) IntToAscii(123) IntToAscii(12) IntToAscii(1) putchar('1') putchar('2') putchar('3') putchar('4')putchar('5')
Integer to ASCII
Advanced C 35BYU CS 124
Recursive Line Draw
void lcd_draw_line(int16 x0, int16 y0, int16 x1, int16 y1){ if ((abs(x1 - x0) <= 1) && (abs(y1 - y0) <= 1)) { lcd_point(x0, y0, PS); lcd_point(x1, y1, PS); } else { int16 newX = ((x0 + x1) / 2); int16 newY = ((y0 + y1) / 2); lcd_draw_line(newX, newY, x1, y1); lcd_draw_line(x0, y0, newX, newY); }} // end lcd_draw_line
Line Draw
Advanced C 36BYU CS 124
Moving from C to C++
In the 1980’s Bjourn Stroustrup, working for AT&T, took the C language to its next progression.
He added features to correct some of the problems in the C language, while changing the way programmers view programs by introducing object orientation (OOP) to the language.
The original C++ language was not a compiler, but a pre-compiler of C++ code into regular C code.
C++ has much stronger data typing, whereas C is known as a weakly type language.
The biggest C++ advantage is data hiding – protecting data. Data is only accessed through methods. Only those functions that should change the data can change the data. The user has no knowledge of your variables and is concerned only with
the results of your program, not the source code or the variables that produce the result.
Moving from C to C++
Advanced C 37BYU CS 124
10 Major Differences C to C++
1. C follows the procedural programming paradigm while C++ is a multi-paradigm language(procedural as well as object oriented).
2. C data is not secured while the data is secured(hidden) in C++.
3. C is a low-level language while C++ is a middle-level language.
4. C uses the top-down approach while C++ uses the bottom-up approach.
5. C is function-driven while C++ is object-driven.
6. C++ supports function overloading (polymorphism) while C does not.
7. We can use functions inside structures in C++ but not in C.
8. C++ uses NAMESPACE to avoid name collisions while C uses static.
9. The standard input & output functions differ in the two languages (C uses scanf & printf while C++ uses cin>> & cout<< )
10. C++ allows the use of reference variables while C does not.
Moving from C to C++
Advanced C 38BYU CS 124
typedef struct{
double sales;double profit;char name[25];
} Store;
Store big_store;strcpy(big_store.name, “Mom’s Food”);big_store.sales = 100.00;big_store.profit = 10.00;
structs vs classesclass Store{
double sales;double profit;char name[25];
};
Store big_store;strcpy(big_store.name, “Mom’s Food”);
class Store{ private:
double sales;double profit;
public:char name[25];
double Store::getSales();double Store::getProfit();void Store::setSales(double);void Store::setProfit(double);
};
Store big_store;strcpy(big_store.name, “Mom’s Food”);big_store.setSales(100.00);big_store.setProfit(10.00);
Moving from C to C++
Advanced C 39BYU CS 124
Polymorphism
Polymorphism means that something has many (poly), forms (morph). The same function name can work on different types of objects Keeps the clutter out of your code.
Example: employee.print(); // Print the employee objects customer.print(); // Print the customer objects
Moving from C to C++
#include <iostream>using namespace std;
int size(int x);int size(int a, int b);int main() { cout << "Size of a square with side 5: " << size(5) << '\n'; cout << "Size of rectangle of with sides 5,4: " << size(5,4) << endl; return 0;}int size(int x) { return (x*x);}int size(int a, int b) { return (a*b);}
Advanced C 40BYU CS 124
Sundry Additions
Call by reference (ie: myfunc(int &a, int &b);) Strongly typed enums Generic, type-save data structures (using templates) New keywords
typeid, bool, dynamic_cast, mutable, catch, explicit, namespace, static_cast, using, export, new, virtual, class, operator, private, template, const_cast, protected, this, wchar_t, public, throw, friend, delete, reinterpret_cast, try
Constructors/destructors Namespaces
Advanced C 41BYU CS 124
What?
The semester’s over already?
Advanced C 42BYU CS 124
See you soon!
Advanced C 43BYU CS 124