480 likes | 572 Views
3.5 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. File I/O. I/O from Files. General-purpose I/O functions allow us to specify the stream on which they act
E N D
3.5 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
File I/O I/O from Files • General-purpose I/O functions allow us to specify the stream on which they act • Must declare a pointer to a FILE struct for each physical file we want to manipulate FILE* infile; FILE* outfile; • The I/O stream is "opened" and the FILE struct instantiated by the fopen function • Arguments include the name of file to open and a description of the operation modes we want to perform on that file infile = fopen("myinfile", "r"); outfile = fopen("myoutfile", "w"); • Returns pointer to file descriptor or NULL if unsuccessful 3.4 - Input and Output
File I/O I/O from Files • File operation modes are: • "r" for reading • "w" for writing (an existing file will lose its contents) • "a" for appending • "r+" for reading and writing • "b" for binary data • Once a file is opened, it can be read from or written to with functions such as • fgetc Read character from stream • fputc Write character to stream • fread Read block of data from stream • fwrite Write block of data to stream • fseek Reposition stream position indicator • fscanf Read formatted data from stream • fprintf Write formatted output to stream 3.4 - Input and Output
File I/O I/O from Files #define LIMIT 1000 int main() { FILE* infile; FILE* outfile; double prices[LIMIT]; inti=0; infile = fopen("myinputfile", "r"); // open for reading outfile = fopen("myoutputfile", "w"); // open for writting if ((infile != NULL) && (outfile != NULL)) { while (i < LIMIT) { if ((fscanf(infile, "%lf", &prices[i]) == EOF)) break; printf("\nprice[%d] = %10.2lf", i, prices[i]); // ... process prices[i] i += 1; } } else printf("\nfopen unsuccessful!"); fclose(infile); fclose(outfile); } 3.4 - Input and Output
sprintf / sscanf sprintf and sscanf • sprintf converts binary numbers to a string • int sprintf(char* buffer, const char* fmt,…); • buffer – char array big enough to hold converted number(s) • fmt – format specification • Variable list to be converted • Returns number of characters in converted string (not including null character • Useful in converting data to strings • sscanf converts a string to binary numbers • int sscanf(char* buffer, const char* fmt,…); • buffer – char array contains data to be converted • fmt – format specification • List of pointers to variables to hold converted values • Returns number of items converted • Useful to convert character data files 3.4 - Input and Output
Final Exam • Questions? • All work due Tuesday! • On-line evaluations • BYU Grade scores in LearningSuite • Final Exam in Testing Center • Available during University Final Exam Days • No time limit • No make-up • 66 Questions: 3-5 questions/chapter • 2 pages hand-written notes • Study slides • Study homework • Study previous exams • Study review sheet Sundry Items
CS 124 3.4 - Input and Output
Learning Objectives… • Recursion • Factorial • Running Sum • Binary Search • Fibonacci Numbers • Activation Records • Integer to ASCII • Line Draw Advanced C
Recursion 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. Advanced C
Recursion 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)); } Advanced C
Binary Search 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), thensearch the upper half of the stack.3b. else search the lower half of the stack. 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
Fibonacci Numbers Fibonacci Numbers f(n) = f(n-1) + f(n-2) f(0) = 1 f(1) = 1 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987… int Fibonacci(int n) { if (n <= 1) return 1; /* base case */ return (Fibonacci(n-1) + Fibonacci(n-2)); } Advanced C
Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) 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); } Advanced C
Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) Fibonacci(3) 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); } Advanced C
Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) Fibonacci(3) Fibonacci(2) 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); } Advanced C
Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) Fibonacci(3) Fibonacci(2) Fibonacci(1) 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); } Advanced C
1 Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) Fibonacci(3) Fibonacci(2) Fibonacci(1) 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); } Advanced C
Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) Fibonacci(3) Fibonacci(2) 1 Fibonacci(1) Fibonacci(0) 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); } Advanced C
1 Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) Fibonacci(3) Fibonacci(2) 1 Fibonacci(1) Fibonacci(0) 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); } Advanced C
1 Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) Fibonacci(3) Fibonacci(2) + 1 Fibonacci(1) Fibonacci(0) 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); } Advanced C
Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) Fibonacci(3) 2 Fibonacci(2) + 1 1 Fibonacci(1) Fibonacci(0) 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); } Advanced C
Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) Fibonacci(3) 2 Fibonacci(2) Fibonacci(1) + 1 1 Fibonacci(1) Fibonacci(0) 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); } Advanced C
1 Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) Fibonacci(3) 2 Fibonacci(2) Fibonacci(1) + 1 1 Fibonacci(1) Fibonacci(0) 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); } Advanced C
Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) Fibonacci(3) + 2 1 Fibonacci(2) Fibonacci(1) + 1 1 Fibonacci(1) Fibonacci(0) 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); } Advanced C
Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) 3 Fibonacci(3) + 2 1 Fibonacci(2) Fibonacci(1) + 1 1 Fibonacci(1) Fibonacci(0) 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); } Advanced C
Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) 3 Fibonacci(3) Fibonacci(2) + 2 1 Fibonacci(2) Fibonacci(1) + 1 1 Fibonacci(1) Fibonacci(0) 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); } Advanced C
Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) 3 Fibonacci(3) Fibonacci(2) + 2 1 Fibonacci(2) Fibonacci(1) Fibonacci(1) + 1 1 Fibonacci(1) Fibonacci(0) 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); } Advanced C
1 Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) 3 Fibonacci(3) Fibonacci(2) + 2 1 Fibonacci(2) Fibonacci(1) Fibonacci(1) + 1 1 Fibonacci(1) Fibonacci(0) 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); } Advanced C
Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) 3 Fibonacci(3) Fibonacci(2) + 1 2 1 Fibonacci(2) Fibonacci(1) Fibonacci(1) Fibonacci(0) + 1 1 Fibonacci(1) Fibonacci(0) 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); } Advanced C
Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) 3 Fibonacci(3) Fibonacci(2) + 1 1 2 1 Fibonacci(2) Fibonacci(1) Fibonacci(1) Fibonacci(0) + 1 1 Fibonacci(1) Fibonacci(0) 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); } Advanced C
Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) 3 Fibonacci(3) Fibonacci(2) + + 1 1 2 1 Fibonacci(2) Fibonacci(1) Fibonacci(1) Fibonacci(0) + 1 1 Fibonacci(1) Fibonacci(0) 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); } Advanced C
Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) 2 3 Fibonacci(3) Fibonacci(2) + + 1 1 2 1 Fibonacci(2) Fibonacci(1) Fibonacci(1) Fibonacci(0) + 1 1 Fibonacci(1) Fibonacci(0) 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); } Advanced C
Fibonacci Numbers Call Tree for Fibonacci main() Fibonacci(4) + 2 3 Fibonacci(3) Fibonacci(2) + + 1 1 2 1 Fibonacci(2) Fibonacci(1) Fibonacci(1) Fibonacci(0) + 1 1 Fibonacci(1) Fibonacci(0) 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); } Advanced C
Fibonacci Numbers Call Tree for Fibonacci main() 5 Fibonacci(4) + 2 3 Fibonacci(3) Fibonacci(2) + + 1 1 2 1 Fibonacci(2) Fibonacci(1) Fibonacci(1) Fibonacci(0) + 1 1 Fibonacci(1) Fibonacci(0) 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); } Advanced C
Activation Records Activation Records • A new activation record is pushed on the stack for each subroutine call. main calls Fibonacci(3) Fibonacci(3) calls Fibonacci(2) Fibonacci(2) calls Fibonacci(1) SP Fib(1) SP Fib(2) Fib(2) SP Fib(3) Fib(3) Fib(3) main main main Advanced C
Activation Records Activation Records Fibonacci(1) returns,Fibonacci(2) calls Fibonacci(0) Fibonacci(2) returns,Fibonacci(3) calls Fibonacci(1) Fibonacci(3)returns SP Fib(0) SP Fib(2) Fib(1) Fib(3) Fib(3) SP main main main Advanced C
Activation Records MSP430 Fibonacci Fibonacci: 0xa064: 120A PUSH R10 0xa066: 8321 DECD.W SP 0xa068: 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,R12 0xa074: 3C09 JMP (C$L2) C$L1: 0xa076: 831C DEC.W R12 0xa078: 12B0 A064 CALL #Fibonacci 0xa07c: 4C0A MOV.W R12,R10 0xa07e: 412C MOV.W @SP,R12 0xa080: 832C DECD.W R12 0xa082: 12B0 A064 CALL #Fibonacci 0xa086: 5A0C ADD.W R10,R12 C$L2: 0xa088: 5321 INCD.W SP 0xa08a: 413A POP.W R10 0xa08c: 4130 RET Advanced C
Integer to ASCII 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 */ }} Advanced C
Integer to ASCII 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') Advanced C
Line Draw 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 Advanced C
Moving from C to C++ 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. Advanced C
Moving from C to C++ 10 Major Differences C to C++ • C follows the procedural programming paradigm while C++ is a multi-paradigm language(procedural as well as object oriented). • C data is not secured while the data is secured(hidden) in C++. • C is a low-level language while C++ is a middle-level language. • C uses the top-down approach while C++ uses the bottom-up approach. • C is function-driven while C++ is object-driven. • C++ supports function overloading (polymorphism) while C does not. • We can use functions inside structures in C++ but not in C. • C++ uses NAMESPACE to avoid name collisions while C uses static. • The standard input & output functions differ in the two languages (C uses scanf & printf while C++ uses cin>> & cout<< ) • C++ allows the use of reference variables while C does not. Advanced C
Moving from C to C++ structs vs classes 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; class 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); Advanced C
Moving from C to C++ 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 #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
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
What? The semester’s over already? Advanced C
See you soon! Advanced C