Showing posts with label Data structure and algorithm. Show all posts
Showing posts with label Data structure and algorithm. Show all posts

Tuesday, December 8, 2020

Learn recursion for beginners.

 Learn recursion for beginners.


Recursion is a function that call itself. It is an important concept. Recursion should must have a base condition that stop it from further calling.


General syntax of recursion.


void fun(int x)

{

        //  logic

}

int main()

{

      fun(x);

     return 0;

}


When we call the recursion then printing is done at two time;

  1. Printing at calling time.
  2. Printing at returning time.


Printing at calling time:



#include<stdio.h>

void fun(int x)

{

if(x>0)

{

printf("%d",x);

fun(x-1);

}

}

int main()

{

int x = 3;

fun(x);

return 0;

}


Printing at returning time:


#include<stdio.h>
void fun(int x)
{
if(x>0)
{
fun(x-1);
printf("%d",x);
}
}
int main()
{
int x = 3;
fun(x);
return 0;
}


Difference between loop and recursion:


1. Loop is repetitive statement.
2. Recursion is also repetitive statement.But loop will have ascending phase and recursion will have both ascending and descending phase.




Types of recursion:

  1. Tail recursion
  2. Head recursion
  3. Tree recursion
  4. Indirect recursion
  5. Nested recursion


 1.Tail recursion:

This is type of recursion where everything performed at calling time.Function will not do anything at returning time.

Program:

#include<stdio.h>

void fun(int x)

{

if(x>0)

{

printf("%d",x);

fun(x-1);

}

}

int main()

{

int x = 3;

fun(x);

return 0;

}


2.Head recursion:


This type of recursion will perform everything at returning time.This will not do anything at calling time.

If the function is performing something at returning time it can't be easily converted into loop but it can be converted.

Program that explain head recursion:


#include<stdio.h>
void fun(int x)
{
if(x>0)
{
fun(x-1);
printf("%d",x);
}
}
int main()
{
int x = 3;
fun(x);
return 0;
}


3. Tree recursion.


In this type of recursion a function is called multiple time.

Program that explain tree recursion:


#include<stdio.h>
void fun(int x)
{
if(x>0)
{
printf("%d",x);
fun(x-1);
fun(x-1);
}
}
int main()
{
int x = 3;
fun(x);
return 0;
}


4. Indirect recursion.


In indirect recursion there may be more than one function calling each other in circular fashion.So that the first function calls second one and then second function calls third one and then third function again callback first function.


Then it become a cycle so it become recursion.


5. Nested recursion.

This is basically recursion inside recursion.


#include<stdio.h>

int fun(int n)

{

if(n>100)

{

return n-10;

}

else

{

return fun(fun(n+10));

}

}

int main()

{

int x = 3;

int d = fun(x);

printf("%d",d);

return 0;

}

Find whether an element is present in an array or not.

 Find whether an element is present in an array or not.


Question: Write a program to find whether an element is present in an array or not?

Explanation: 

        A[10] = {10,20,30,40,50,60},ele = 60

    

   We have to find whether element 60 is present in our array or not.


Programs:


#include<iostream>

using namespace std;

int main()

{

int A[10] = {10,20,30,40,50,60},pos,ele,flag=0;

cout<<"Enter an element to search:"<<endl;

cin>>ele;

for(int i=0;i<6;i++)

{

if(A[i] == ele)

{

flag = 1;

break;

}

}

if(flag==1)

{

cout<<"Element found";

}else{

cout<<"Element not found";

}

return 0;

}

FInd index of an element in array.

 Find index of an element in array.


Question: write a program to find index of an element in array ?

Explanation:

A[10] = {10,20,30,40,50,60},ele = 60.

Answer: 5

Because element 60 is present at index 5.


Program:


#include<iostream>

using namespace std;

int main()

{

int A[10] = {10,20,30,40,50,60},pos,ele,flag=0;

cout<<"Enter an element to search:"<<endl;

cin>>ele;

for(int i=0;i<6;i++)

{

if(A[i] == ele)

{

pos = i;

flag = 1;

break;

}

}

if(flag==1)

{

cout<<"Element found"<<pos;

}else{

cout<<"Element not found";

}

return 0;

}

Deletion in an array for beginner for free.

 Deletion in an array.


For example:


After deletion: A[] = {10,20,30,40,50,60};

Delete an element at index = 3.

After deletion:  A[] = {10,20,30,50,60};


Programs:


#include<iostream>

using namespace std;

int main()

{

int A[10] = {10,20,30,40,50,60},delPos = 3;

for(int i=delPos+1;i<6;i++)

{

A[i-1] = A[i];

}

for(int i=0;i<5;i++)                                                

{

cout<<A[i]<<endl;

}

return 0;

}

Insertion in an array for beginner in c++.

 Insertion in an array.

For example:

Before insertion:  A[] = {10,20,30,40,50,60};

Insert an element = 100 at position index = 3.

After insertion: A[] = {10,20,30,100,40,50,60};


Program:


#include<iostream>
using namespace std;
int main()
{
int A[10] = {10,20,30,40,50,60},position = 3,element = 100;
for(int i=5;i>= position;i--)
{
A[i+1] = A[i];
}
A[position] = element;
for(int i=0;i<7;i++)
{
cout<<A[i]<<endl;
}
return 0;
}

Arrays for beginners for free.

Array.


Array is basically a contiguous memory allocation of same data type.It can't be of different data type.

It is a homogeneous continuous memory allocation.


We will learn about:

  1. What is an Array
  2. Declaring and initializing
  3. Accessing array element

1.What is an array:


Array is basically a contiguous memory allocation of same data type.It can't be of different data type.

It is a homogeneous continuous memory allocation.


2.Declaring and initializing:


int A[5];

The above statement is used for declaring an array.

int A[5] = {1,2,3,4,5};

The above statement is used for initialing an array.


Program to illustrate difference between declaring and initializing:


#include<iostream>
using namespace std;
int main()
{
int A[5];
int B[5] = {1,2,3,4,5};
for(int i=0;i<5;i++)
{
cout<<"Enter element for array A";
cin>>A[i];
}
for(int i=0;i<5;i++)
{
cout<<A[i]<<" ";
}
cout<<endl;
for(int i=0;i<5;i++)
{
cout<<B[i]<<" ";
}
return 0;
}

3.Accessing array element:

We can access array element with their position index.

Program that will explain how to access array element:


#include<iostream>
using namespace std;
int main()
{
int A[5];
for(int i=0;i<5;i++)
{
cout<<"Enter element for array A";
cin>>A[i];
}
for(int i=0;i<5;i++)
{
cout<<A[i]<<" ";
}
return 0;
}


Programs on array: