0% found this document useful (0 votes)
31 views5 pages

Recursive Functions in C++ Code Examples

The document contains examples of recursive C++ functions. It shows functions that check if two strings are anagrams, generate all possible combinations of dots and underscores, and sort an array using recursion. It also contains test cases and driver code for recursively summing and multiplying negative elements of an array.

Uploaded by

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

Recursive Functions in C++ Code Examples

The document contains examples of recursive C++ functions. It shows functions that check if two strings are anagrams, generate all possible combinations of dots and underscores, and sort an array using recursion. It also contains test cases and driver code for recursively summing and multiplying negative elements of an array.

Uploaded by

Andrei Haiduc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

9)

(recursiv)
#include <iostream>
#include <cstring>
using namespace std;
int anagrame(char x[],char y[])
{
char *p;
if(strcmp(x,y)==0)
return 1;
p=strchr(y,x[0]);
if(p==0)
return 0;
strcpy(x,x+1);
strcpy(p,p+1);
return anagrame(x,y);
}
int main()
{
char x[256],y[256];
cin>>x>>y;
if(strlen(x)!=strlen(y))
cout<<"nu sunt anagrame";
else
if(anagrame(x,y))
cout<<"sunt anagrame";
else
cout<<"nu sunt anagrame";
}

(direct)

#include <iostream>
#include <cstring>
using namespace std;
char s1[21],s2[21];
int i,j,vf1[256],vf2[256],ok=1;
int main()
{
cin>>s1>>s2;
for(i=0;i<=strlen(s1)-1;i++)
vf1[s1[i]]++;
for(i=0;i<=strlen(s2)-1;i++)
vf2[s2[i]]++;
for(i='A';i<='z'+1;i++)
if(vf1[i]!=vf2[i])
ok=0;
cout<<ok;
return 0;
}

10)

#include <iostream>
using namespace std;
int n;
char a[20];
void generare(int poz)
{
int i;
if (poz==n+1)
{
for(i=1;i<=n;i++)
cout<<a[i]<<" ";
cout<<endl;
}
else
{
a[poz]='.';
generare(poz+1);
a[poz]='_';
generare(poz+1);
}
}
int main()
{
cin>>n;
generare(1);
return 0;
}

11)

#include <iostream>
using namespace std;
int n,v[150];
void citire(int &n,int v[])
{
int i;
cin>>n;
for(i=0;i<n;i++)
cin>>v[i];
}
void afisare(int n,int v[])
{
int i;
for(i=0;i<n;i++)
cout<<v[i]<<' ';
}
void ordonare(int j, int i,int v[])
{
int aux;
if(j>=0) {
if(v[i]<v[j])
{
aux=v[i];
v[i]=v[j];
v[j]=aux;
}
ordonare(j - 1, i,v);
}
}
void ord(int i,int v[])
{
if(i>0) {
ordonare(i-1,i,v);
ord(i-1,v);
}
}
int main() {

citire(n,v);
ord(n - 1,v);
afisare(n,v);
return 0;
}

TEST:

1) Recursivitate directă este proprietatea funcţiilor de a se


autoapela.
2) Codul corectat

int suma(int i, int a[]) {


if (i == 0)
return 0;
else if (a[i] % 2 == 0)
return a[i] + suma(i + 1, a);
else
return suma(i - 1, a);
}

3)
b)-3

4)

#include <iostream>
using namespace std;
void citire(int n,int v[])
{
if(n>0)
{
citire(n-1,v);
cin>>v[n-1];
}
}
int suma(int n,int v[])
{
if(n==0)
return 0;
else
if(v[n-1]>=0)
return suma(n-1,v)+v[n-1];
else
return suma(n-1,v);
}
int prodneg(int n,int v[])
{
if(n==0)
return 1;
else
{
if(v[n-1]<0)
return prodneg(n-1,v)*v[n-1];
else
return prodneg(n-1,v);
}
}
int main()
{
int n,v[101];
cin>>n;
citire(n,v);
cout<<suma(n,v);
cout<<endl;
cout<<prodneg(n,v);
return 0;
}

You might also like