CS235102 Data Structures Appendix 1 How to transfer a simple loop- expression to a recursive function (factorial calculation)

Slides:



Advertisements
Similar presentations
Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.
Advertisements

Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Program Looping EE2372 Software Design I Dr. Gerardo Rosiles.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Chapter 13 Control Structures in C. BYU CS/ECEn 124Variables and Operators2 Topics to Cover… Control Structures if Statement if-else Statement switch.
void count_down (int count) { for(i=count; i>1; i--) printf(" %d\t", count); } printf("A%d\n", count); if(count>1) count_down(count-1); printf("B%d\n",
Loop invariant code removal CS 480. Our sample calculation for i := 1 to n for j := 1 to m c [i, j] := 0 for k := 1 to p c[i, j] := c[i, j] + a[i, k]
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
Discussion of Assignment #2 CS-2301, B-Term Discussion of Assignment #2 CS-2301, System Programming for Non-Majors (Slides include materials from.
CS150 Introduction to Computer Science 1
Digression on Loop Invariants CS-2303, C-Term Digression on Loop Invariants CS-2303, System Programming Concepts (Slides include materials from The.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
For Loop Lesson 3 CS1313 Spring for Loop Lesson 3 Outline 1. for Loop Lesson 3 Outline 2. for Loop with a float Counter: BAD! 3. float Counter Example.
For Loop Lesson 2 CS1313 Spring for Loop Lesson 2 Outline 1. for Loop Lesson 2 Outline 2. for Loop Application 3. Factorial 4. Factorial Program.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Return function Random function Recursion function Function in C 1.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
For Loop Lesson 1 CS1313 Spring for Loop Lesson 1 Outline 1. for Loop Lesson 1 Outline 2.A while Loop That Counts #1 3.A while Loop That Counts.
1. Agenda for loop Short-handed notation How to use break and continue 2.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Looping Construct or Statements. Introduction of looping constructs In looping,a sequence of statements are executed until some condition for termination.
LOOP & Type Conversion. do – while Loop In the while loop, the test expression is evaluated at the beginning of the loop. If the test condition is false.
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
The Assignment operator tMyn1 The Assignment Operator The result of a calculation can be stored in a variable using the assignment operator =. Because.
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
CS 108 Computing Fundamentals Notes for Thursday, February 18, 2016.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Flow Control. Comments u Comments: /* This is a comment */ –Use them! –Comments should explain: v special cases v the use of functions (parameters, return.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
Repetition statements
C Functions -Continue…-.
Lecture 7: Repeating a Known Number of Times
for Loop 1 Outline for Loop 1 Outline for Loop
Pointers.
Flow of Control.
11/10/2018.
Flow of Control.
Flow of Control.
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
Flow of Control.
Assignment Operators Topics Increment and Decrement Operators
Lesson #6 Modular Programming and Functions.
ECE 103 Engineering Programming Chapter 19 Nested Loops
Digression on Loop Invariants
When a function is called...
Yan Shi CS/SE 2630 Lecture Notes
ECE 103 Engineering Programming Chapter 18 Iteration
Assignment Operators Topics Increment and Decrement Operators
Lec 6 Loop Statements Introduction to Computer Programming
Assignment Operators Topics Increment and Decrement Operators
EECE.2160 ECE Application Programming
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
REPETITION Why Repetition?
Presentation transcript:

CS Data Structures Appendix 1 How to transfer a simple loop- expression to a recursive function (factorial calculation)

How To Transfer A Loop To A Recursive Program Step 1: Use if, label and goto statements to replace a loop statement a loop statement Step 2: Replace the initial assignment of loop statement as a function call and add statement as a function call and add necessary variables as arguments necessary variables as arguments Step 3: Replace the label statement as a recursive function function Step 4: Replace the incremental and goto statements as a recursive call as a recursive call

main() { int i, n, value; value = 1; for ( i<=n; ) { *value = *value * i; printf (“%d! = %d\n”, i, value); }} i=1; i++ The initial assignment is moved to the outside area of for loop conditional expression is placed in an if() statement incremental is placed at the end of for loop if (i<=n) { Add a label in front of the if statement and a goto statement at the end of if body goto L1; The block forms the recursive function L1: Replace the initial assignment of loop statement as a function call Replace the label statement as a recursive function name void factor(int i, const int n, int *value) { factor(1, n, &value); } Necessary variables as arguments factor(i+1, n, value); Replace the incremental and goto statements as a recursive call A simple loop program The final recursive function (call)