Use of Recursion
//cheak sorted array or not
#include<iostream>
using namespace std;
bool check(int arr[],int n)
{ if (arr[n-2]==arr[0])
{return 1;}
if(arr[n-1]>arr[n-2])
{ return check(arr,n-1) ;}
else{ return 0;} }
// write the sequence of decnbr
void decnmbr (int n)
{
cout<<n<<" ";
if(n==0){return;}
decnmbr(n-1);
}
// find first occurance
int firstoccurance (int a[],int n,int key,int i)
{ if(i>n)
{return -1;}
if(a[i]==key)
{return i;}
firstoccurance( a,n,key,i+1);
}
//find last occurrence
int lstocrnc(int a[],int n,int key,int j)
{ if(j<0){return -1;}
if(a[j]==key)
{return j;}
lstocrnc( a,n,key,j-1);
}
int main()
{ int arr[5]={2,3,4,5,6};
if(check(arr,3))
{cout << "sorted / increasing order";}
else {cout<<"Not increasing order ";}
cout<<endl;
decnmbr(5);
int a[7]={1,2,3,5,2,4,2};
int key;
cout<<"\nkey =";cin>>key;
cout<<endl;
int i=0;
int j=7-1;
cout << endl;
cout<< firstoccurance( a,7,key,i)<<endl;
cout<<lstocrnc(a,7,key,j)<<endl;
return 0;
}
Comments
Post a Comment
If you have any doubt ,let me know