CS1201: Programming Language 2

Slides:



Advertisements
Similar presentations
Introduction to Functions Programming. COMP102 Prog Fundamentals I: Introduction to Functions /Slide 2 Introduction to Functions l A complex problem is.
Advertisements

While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Chapter 4 Summation.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
C++ programming I Lab#3 Control Statements Instructor: Eng. Ruba A. Salamah Tuseday: 17/10/2010.
Programming is instructing a computer to perform a task for you with the help of a programming language.
CS Class 07 Topics –  When software goes wrong  Count controlled loops  Sentential controlled loops  putting it all together Announcements.
CS1201: Programming Language 2 Recursion By: Nouf Almunyif.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan Snd Term Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
CSC1201: Programming Language 2 Lecture 1 Level 2 Course Nouf Aljaffan (C) CSC 1201 Course at KSU1.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Introduction to Functions.  A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself.
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
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 Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Andy Wang Object Oriented Programming in C++ COP 3330
Introduction to Computer Programming
#define #include<iostream> using namespace std; #define GO
CSC1201: Programming Language 2
C++ Programming: CS150 For.
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Functions and an Introduction to Recursion
Andy Wang Object Oriented Programming in C++ COP 3330
CSC113: Computer Programming (Theory = 03, Lab = 01)
CSC113: Computer Programming (Theory = 03, Lab = 01)
לולאות קרן כליף.
Pointer Data Type and Pointer Variables
Dynamic Memory Allocation Reference Variables
Random Number Generation
פונקציות לעיתים קרובות לא נוח להגדיר את כל התוכנה בתוך גוף אחד.
CS1201: Programming Language 2
CS1201: Programming Language 2
While Loop Design ENGI 1020 Fall 2018.
CS150 Introduction to Computer Science 1
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Pass by Reference.
CS150 Introduction to Computer Science 1
If Statements.
Arrays of Two-Dimensions
CS1201: Programming Language 2
Functions and an Introduction to Recursion
CS1201: Programming Language 2
Statements and flow control
Recursion.
Introduction to Algorithms and Programming
Using string type variables
Pointers & Functions.
Pointer Data Type and Pointer Variables
Main() { int fact; fact = Factorial(4); } main fact.
Pointer Data Type and Pointer Variables
CS1201: Programming Language 2
Introduction to Functions
Functions Divide and Conquer
Programming Strings.
Introduction to Algorithms and Programming COMP151
Presentation transcript:

CS1201: Programming Language 2 By: Nouf Almunyif Recursion

Recursion and Recursive Functions Main calls another function…..normal A function calls another function2….normal A function calls itself ?! Possible?? YES A recursive function is one that call itself.

General form void recurse() { recurse(); //Function calls itself }

Finding Factorial 5! = 5*4*3*2*1 Final value=120 5!=5*24=120 returned 5*4! 5*4! 4!=4*6=24 returned 4*3! 4*3! 3!=3*2=6 returned 3*2! 3*2! 2!=2*1=2 returned 2*1! 2*1! 1 1 1

Finding Factorial #incloud <iostream>; Using nampespace std; Void main() { int n,factorial,i; cout << "Enter a positive integer: "; cin >> n; for (i = 1; i <= n; i++) { if (i == 1) factorial = 1; Else factorial = factorial * i; } cout << "The factorial of " << n << " is " << factorial << endl;

Finding Factorial Recursively Enter a positive integer : 4 //Recursive factorial Function #include<iostream> Int factorial(int N); Void main() { int num; cout<<“Enter a positive integer:”; cin>>num; cout<<“factorial=“<<factorial(num); } Int factorial( int N) { if ( N <= 1) //the base case return 1; else return N * factorial (N - 1); Factorial = 24 N=4 Retrun 4 *factorial(3) 6 4*6=24 N=3 Retrun 3 *factorial(2) 2 3*2 =6 N=2 Retrun 2 *factorial(1) 2*1 = 2 1 N=1 Retrun 1

Example printing number counting doun  void main() { int input; cout<<"enter positive number "; cin>>input; myMethod(input);   } #include <iostream> using namespace std; void myMethod( int counter) { if(counter == 0) return; // no value returning just for EXIT the function else { cout<<counter<<endl; myMethod(--counter); }

Exercise write a recursion function to print int number counting up: 1,2,3,4,5 …. N. Test your function and show the out put