DS Lab 9 - Recursion in C++
DS Lab 9 - Recursion in C++
Table of Contents
1. Introduction............................................................................................................................................................3
Basic Concepts about Data Structures..............................................................................................................................3
Recursion in C++..............................................................................................................................................................3
Direct recursion:...............................................................................................................................................................3
Indirect recursion:.............................................................................................................................................................3
Relevant Lecture Readings...............................................................................................................................................4
2. Activity Time boxing.............................................................................................................................................4
3. Objectives of the experiment.................................................................................................................................4
4. Concept Map..........................................................................................................................................................4
Recursion in C++..............................................................................................................................................................4
Implementation of Recursion function in C++.................................................................................................................5
Example.............................................................................................................................................................................6
Homework before Lab.............................................................................................................................................................7
Problem Solution Modeling..............................................................................................................................................7
Problem description:.........................................................................................................................................................7
Procedures & Tools.................................................................................................................................................................7
Tools 8
Editing, Compiling and Running programs in Visual Studio 2017[Expected time = 5mins]..........................................8
Walk through Task [Expected time = 20mins]..........................................................................................................8
Practice Tasks..........................................................................................................................................................................9
Practice Task 1 [Estimated time: 20 min]................................................................................................................9
Practice Task 2 [Estimated time: 20 min]................................................................................................................9
Practice Task 3 [Estimated time: 20 min]................................................................................................................9
Practice Task 4 [Estimated time: 20 min]................................................................................................................9
Out comes........................................................................................................................................................................10
Evaluation Task (Unseen) [Expected time = 60mins for two tasks]...............................................................................10
9. Evaluation criteria.............................................................................................................................................................10
10. Further Readings.............................................................................................................................................................10
10.1 C++ Tutorial Web Sites..........................................................................................................................................10
10.2 Web sites containing supporting material...............................................................................................................10
1. Introduction
Objective of this lab is to provide you knowledge about recursion in C++. You have come to know
about the basic concepts about C++ language in courses: computer programming and object oriented
programming. You have learned about the statements used in this language like input/output statements,
selection statements (if..else, switch) and iterative statements/loops (while, do..while, for). Further you
will also have knowledge about classes, encapsulation, inheritance and polymorphism.
In C++, every application/program contains a function called main(), which is a global function.
Execution of every program starts from this function, so you have to write this function in each C++
program. You may write other functions also in your program beside main() function, which depends on
modular solution you have developed to solve your problem.
A program is a set of instructions that processes data which is given as input to it. If we need to
develop a good (efficient) program, we need to organize data in computer’s memory in effective way.
This organized data is called a Data Structure.
Study of computer science includes the study of how data (information) is organized in computer
and how it can be manipulated and utilized by programs. It is extremely important for you to understand
the concepts of data (information) organization and manipulation. Data structures for storing information
are lists, stacks, queues, trees, graphs and tables.
Recursion in C++
The process in which a function calls itself is known as recursion and the corresponding function
is called the recursive function. In recursion function there is a base condition which is very important;
without this base condition the function cannot execute properly. The purpose of recursion is to divide the
problem into smaller problems till the base condition is reached.
Direct recursion:
When function calls itself, it is called direct recursion, the example we have seen above is a direct
recursion example.
Indirect recursion:
When function calls another function and that function calls the calling function, then this is called
indirect recursion. For example: function A calls function B and Function B calls function A.
a) From books: C++ Data Structures by Nell Dale (Page79-85) and Data structures using C++
by D. S. Malik (Page130-135).
b) From books: C++ Data Structures by Nell Dale (Page 99-102) and Data structures using
C++ by D. S. Malik (Page 138-145).
4. Concept Map
This concept map will help students to understand the main concepts of topic covered in lab.
Recursion in C++
In C++ recursion is used when a function calls itself again and again. The popular example to
understand the recursion is factorial function.
Example:
A function that calls itself is known as recursive function. And, this technique is known as recursion.
To prevent infinite recursion, if...else statement (or similar approach) can be used where one branch
makes the recursive call and other doesn't.
Example
Problem description:
Design a program which should contain a recursive function for calculating sum of all natural
numbers up to a number given by user.
Tools
Microsoft Visual Studio 2017 with Visual C++ compiler configured.
Department of Computer Science Page 7
IIU, Islamabad
Lab 9: Recursion in C++
You are required to use Microsoft Visual Studio 2017 environment for programming on data
structures. It is expected that you are already familiar with this environment in your courses of Computer
Programming and Object Oriented Programming. You should be able to write code in source code files
(cpp files) and perform compilation of code. Beside that you are also required to be proficient in
performing line by line debugging and break point based debugging to remove the errors from your code.
Following program represents the concept related to static and dynamic arrays; you are required to
type this program in editor of Visual Studio 2017, compile and run to see the output of this program.
Figure 3 shows the source code window of this program in Visual Studio 2017.
Output of this program is shown in figure 4, when program is compiled and executed.
Practice Tasks
This section will provide information about the practice tasks which are required to be performed in lab
session. Design solutions of problems discussed in each task and place solution code in a folder specified
by your lab instructor.
Lab instructors are guided to help students learn how ACM problems work and provide students
with certain practice/home tasks on the pattern of ACM Problems.
Sample output:
Entered sequence of characters is palindrome.
Out comes
After completing this lab, student will be able to understand and develop programs related to static and
dynamic arrays in C++ using Microsoft Visual Studio 2017 environment.
9. Evaluation criteria
The evaluation criteria for this lab will be based on the completion of the following tasks. Each task is
assigned the marks percentage which will be evaluated by the instructor in the lab whether the student has
finished the complete/partial task(s).