0% found this document useful (0 votes)
19 views29 pages

RU - CS - DS - 02 (Arrays & Recursion)

Uploaded by

bachagak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views29 pages

RU - CS - DS - 02 (Arrays & Recursion)

Uploaded by

bachagak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Data Structure Chapter 02_Arrays & Recursion

Faculty of Computer Science

Data Structure

Lecturer: Shekofa GHOURY


Year: 2020
Data Structure Chapter 02_Arrays & Recursion

Outline
• Introduction to One Dimensional Array
• Insertion program using C++
• Deletion program using C++
• Introduction to Two Dimensional Array
• Program for creation of Two Dimensional Array
• Program to calculate the elements of Two Dimensional Array
• Recursion
• Factorial using Recursive function in C++
• Fibonacci sequence using Recursive function in C++
Data Structure Chapter 02_Arrays & Recursion

Arrays
• Array is a collection of similar data elements grouped under one name.
• In C++ indexes of an array starts from 0.
Data Structure Chapter 02_Arrays & Recursion

Advantages of Arrays
• Efficient usages of memory
• Stores multiple data items of same type as single variable
• Helps in reusability of code
• Avoids memory overflow
• Predictable access time of an array (Access Speed)
Data Structure Chapter 02_Arrays & Recursion

Disadvantages of Arrays
• Fixed size of arrays
• Difficulty with insertion or deletion of elements
• In unsorted array search takes long time
• In sorted array insertion takes long time
• In both sorted and unsorted array deletion is slow
• Sometimes Wastage of memory
Data Structure Chapter 02_Arrays & Recursion

Static & Dynamic Array


• Static Array means that the • Dynamic Array means the array is created
size of an array is static and inside the heap and its size can be determined
is created inside the stack. either at compilation or at run-time.
Data Structure Chapter 02_Arrays & Recursion

One-Dimensional Array
• Arrays can be single or multidimensional. The number of subscript or
index determines the dimensions of the array.
• An array of one dimension is known as a one-dimensional array or 1D
array. Conceptually you can think of a one-dimensional array as a row,
where elements are stored one after another.
Data Structure Chapter 02_Arrays & Recursion

1D Array Declaration Methods in C++


1. First method: 4. Fourth method:

2. Second method
5. Fifth method:

3. Third method:
Data Structure Chapter 02_Arrays & Recursion

Insertion in 1D Array
Data Structure Chapter 02_Arrays & Recursion

Insertion in
1D Array
Data Structure Chapter 02_Arrays & Recursion

Deletion from 1D Array


Data Structure Chapter 02_Arrays & Recursion

Deletion from
1D Array
Data Structure Chapter 02_Arrays & Recursion

Two Dimensional Array


• An array of array is known as two dimensional array. 2D arrays are
indexed by two subscripts. One for row and one for column.
Data Structure Chapter 02_Arrays & Recursion

Declaration of 2D Array in C++


• The first method is to create the array completely inside the stack. In
this method a normal declaration of a 2D array along with name of an
array, data type of an array and dimensions of the array.
Data Structure Chapter 02_Arrays & Recursion

Declaration of 2D Array in C++


• The second method is to create the 2D array partially inside the stack and
partially inside the heap. In this method, we create an array of pointers with some
size and type. The array will be created inside the stack and all of each one of its
elements will be pointing to another array inside the heap.
Data Structure Chapter 02_Arrays & Recursion

Declaration of 2D Array in C++


• The third method is to create almost everything inside the heap. In
this method well will define a double pointer array. We will use double
pointer.
Data Structure Chapter 02_Arrays & Recursion

Calculate sum of the elements of 2D array


Data Structure Chapter 02_Arrays & Recursion

Recursion
• Recursion is a programming technique in which a function (or member
function) calls itself.
• It is a Programming tactic that divides a problem into sub-problems.
• A method where the solution to a problem depends on solutions to
smaller instances of the same problem
• Easy way for complex problems
Data Structure Chapter 02_Arrays & Recursion

Recursion – cont..
Int fun (parameter)
{
1 ----------
2 ---------
3 ---------
}
Int main()
{
1 --------
2 --------
3 fun (parameter)
4----------
}
Data Structure Chapter 02_Arrays & Recursion

Recursion – cont..
Type fun (parameter)
{
if ( Base condition )
{
1 ----------
2 ---------
3 fun (parameter)
}
}
Data Structure Chapter 02_Arrays & Recursion

Recursion – cont..
• To solve a problem recursively do the following:
• Specify the problem
• Specify the size of the problem for each call
• Specify the base case (s)
• Specify the Recursive case (s)
Data Structure Chapter 02_Arrays & Recursion

Find Factorial using Recursion in C++


• 0! = 1
• 1! = 1 * 0! = 1
• 2! = 2 * 1! = 2
• 3! = 3 * 2! = 3 * (2 *1) = 6
• 4! = 4 * 3! = 4 * (3 * 2 * 1) = 24
• 5! = 5 * 4! = 5 * 24 = 120
• n! = n * (n-1)!
Data Structure Chapter 02_Arrays & Recursion

Find Factorial using Recursion in C++


Data Structure Chapter 02_Arrays & Recursion

Find Fibonacci sequence using Recursion in C++


• Fibonacci sequence is the series of numbers in which each value of a
Fibonacci series is the sum of the two most recent values. For example:
• 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... , here he next number is found by adding up the
two numbers before it.
Data Structure Chapter 02_Arrays & Recursion

Find Fibonacci sequence using Recursion in C++


0 1 1 2 3 5 8 13 21 34
0 1 2 3 4 5 6 7 8 9
Data Structure Chapter 02_Arrays & Recursion

Structures in C++
• Structure can be defined as collection of related data members under
one name and those data members maybe of similar type or maybe of
dissimilar type.
• So usually it is defined as collection of dissimilar data items under
one name. Is used to create data types.
Data Structure Chapter 02_Arrays & Recursion

Accessing Structure members in C++


Data Structure Chapter 02_Arrays & Recursion

Example: Arrays as Structures (ADT arrays)

1 2

3
Data Structure Chapter 02_Arrays & Recursion

End of Chapter

You might also like