0% found this document useful (0 votes)
27 views

DS Lab 9 - Recursion in C++

This document provides information about recursion in C++ including basic concepts, direct and indirect recursion, relevant readings, objectives of the lab experiment, a concept map explaining recursion in C++ and implementation of recursive functions in C++ with examples, homework, procedures and tools to be used, and practice and evaluation tasks.

Uploaded by

Asim Shareef
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

DS Lab 9 - Recursion in C++

This document provides information about recursion in C++ including basic concepts, direct and indirect recursion, relevant readings, objectives of the lab experiment, a concept map explaining recursion in C++ and implementation of recursive functions in C++ with examples, homework, procedures and tools to be used, and practice and evaluation tasks.

Uploaded by

Asim Shareef
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Department of Computer Science, Faculty of Computing

International Islamic University, Islamabad

Lab Manual for Data


Structures
Lab-9
Recursion in C++
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

Department of Computer Science Page 2


IIU, Islamabad
Lab 9: Recursion in C++

Lab 9: Recursion in C++

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.

Basic Concepts about Data Structures

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.

Department of Computer Science Page 3


IIU, Islamabad
Lab 9: Recursion in C++

Relevant Lecture Readings

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).

2. Activity Time boxing


Table 1: Activity Time Boxing
Task No. Activity Name Activity time Total Time
5.1 Design Evaluation 15 mins 15 mins
6.2 Editing and compiling a C++ 5mins 5mins
program in Visual Studio 2017
6.3 Walk Through Task 20mins 20mins
7 Practice tasks 80 mins for task 1,2 ,3 and 4 80mins
9 Evaluation Task 60mins for all assigned task 50mins

3. Objectives of the experiment


 To get basic understanding of recursion function.
 To write programs in C++ using Microsoft Visual Studio 2017 environment.
 To get an understanding of identifying basic errors in a C++ program by using debugging
techniques.

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:

Department of Computer Science Page 4


IIU, Islamabad
Lab 9: Recursion in C++

Implementation of Recursion function in C++

A function that calls itself is known as recursive function. And, this technique is known as recursion.

The recursion continues until some condition is met.

Department of Computer Science Page 5


IIU, Islamabad
Lab 9: Recursion in C++

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

Department of Computer Science Page 6


IIU, Islamabad
Lab 9: Recursion in C++

Homework before Lab


This homework will help students to study the concepts of topic before start of lab.

Problem Solution Modeling


After studying the introduction and concept map sections you should be ready to provide the
solution of following problems. Design the solutions of the problems in C++ and bring your code
to lab so that lab instructor should assess and grade it.

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.

Procedures & Tools


This section provides information about tools and programming procedures used for the lab.

Tools
Microsoft Visual Studio 2017 with Visual C++ compiler configured.
Department of Computer Science Page 7
IIU, Islamabad
Lab 9: Recursion in C++

Editing, Compiling and Running programs in Visual Studio 2017[Expected time =


5mins]

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.

Walk through Task [Expected time = 20mins]

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.

Figure 3: A program about recursion function in Visual Studio 2017.

Output of this program is shown in figure 4, when program is compiled and executed.

Department of Computer Science Page 8


IIU, Islamabad
Lab 9: Recursion in C++

Figure 4: Output window displaying results of program after compilation.

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.

Practice Task 1 [Estimated time: 20 min]


Write a C++ program to which will create a static array of size 10 now pass this array to a function which
will print the array elements using recursion.

Practice Task 2 [Estimated time: 20 min]


Create a C++ program to check a number is a prime number or not using recursion

Practice Task 3 [Estimated time: 20 min]


Create a C++ program which take two integers from user. Your program should also have a recursive
function which will display odd numbers between the range given by user.

Practice Task 4 [Estimated time: 20 min]


Write a program to create a static character array. Write a recursive function FindPalindrome which
should tell whether or not the entered set of characters is palindromic sequence.
Sample input:
Enter the sequence of characters:
After insertion:
m o m

Sample output:
Entered sequence of characters is palindrome.

Department of Computer Science Page 9


IIU, Islamabad
Lab 9: Recursion in C++

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.

Evaluation Task (Unseen) [Expected time = 60mins for


two tasks]
The lab instructor will give you unseen task depending upon the progress of the class.

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).

Table 2: Evaluation of the Lab

Sr. No. Task No Description Marks


1 4 Problem Modeling 20
2 6 Procedures and Tools 10
3 7 Practice tasks and Testing 35
4 8 Evaluation Tasks (Unseen) 20
5 Comments 5
6 Good Programming Practices 10

10. Further Readings


10.1 C++ Tutorial Web Sites
1. http://www.cplusplus.com/doc/tutorial/
2. http://www.learncpp.com/
3. http://www.tutorialspoint.com/cplusplus/
10.2 Web sites containing supporting material
1.http://www.compgeom.com/~piyush/teach/3330/slides/

Department of Computer Science Page 10


IIU, Islamabad

You might also like