Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
328 views4 pages

A. Scrieti Un Program C++ Cu Funcţii Recursive Pentru Citirea Si Afisarea Elementelor Unui Vector de Numere Intregi

The document discusses 8 problems involving recursive functions in C++ for processing elements in an integer array: 1) Reading and printing array elements 2) Finding the minimum element 3) Finding the maximum element 4) Finding the sum of positive elements 5) Finding the number of even elements 6) Calculating the average of odd elements 7) Finding the greatest common divisor of elements 8) The code provided solves each problem recursively

Uploaded by

Aurel Nechiforel
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)
328 views4 pages

A. Scrieti Un Program C++ Cu Funcţii Recursive Pentru Citirea Si Afisarea Elementelor Unui Vector de Numere Intregi

The document discusses 8 problems involving recursive functions in C++ for processing elements in an integer array: 1) Reading and printing array elements 2) Finding the minimum element 3) Finding the maximum element 4) Finding the sum of positive elements 5) Finding the number of even elements 6) Calculating the average of odd elements 7) Finding the greatest common divisor of elements 8) The code provided solves each problem recursively

Uploaded by

Aurel Nechiforel
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/ 4

a.

Scrieti un program C++ cu funcţii recursive pentru citirea si afisarea


elementelor unui vector de numere intregi.

#include<iostream>
using namespace std;

int n,v[101];

void citire(int i)
{
if(i==0){cout<<"n=";cin>>n;
citire(i+1);}
else if(i<=n)
{
cout<<"v["<<i<<"]=";
cin>>v[i];
citire(i+1);
}
}

void tipar(int i)
{
if(i<=n)
{ cout<<v[i]<<' ';
tipar(i+1);}
}

int main()
{
citire(0);
tipar(1);
return 0;
}

b. determinarea elementului minim din vector;

#include<iostream>
using namespace std;

int n,v[101];

void citire(int i)
{
if(i==0) int main()
{cout<<"n=";cin>>n; {
citire(i+1);} citire(0);
else if(i<=n) cout<<minim(1,v[1]);
{ return 0;
cout<<"v["<<i<<"]="; }
cin>>v[i];
citire(i+1);
}
}

int minim(int i,int a)


{
if(i>n)
return a;
else
if(v[i]>a) return minim(i+1,a);
else return minim(i+1,v[i]);
}
c. determinarea elementului maxim din vector;
#include<iostream>
using namespace std;

int n,v[101];

void citire(int i)
{
if(i==0)
{cout<<"n=";cin>>n;
citire(i+1);}
else if(i<=n)
{
cout<<"v["<<i<<"]=";
cin>>v[i];
citire(i+1);
}
}

int maxim(int i,int a)


{
if(i>n)
return a;
else
if(v[i]<a) return maxim(i+1,a);
else return maxim(i+1,v[i]);
}

int main()
{
citire(0);
cout<<maxim(1,v[1]);
return 0;
}

d. determinarea sumei elementelor pozitive din vector;

#include<iostream>
using namespace std;

int n,v[101];

void citire(int i)
{
if(i==0) int main()
{cout<<"n=";cin>>n; {
citire(i+1);} citire(0);
else if(i<=n) cout<<suma_pozitive(1);
{ return 0;
cout<<"v["<<i<<"]="; }
cin>>v[i];
citire(i+1);
}
}

int suma_pozitive(int i)
{
if(i>n) return 0;
else
if(v[i]>0) return v[i]+ suma_pozitive(i+1);
else return suma_pozitive(i+1);
}
f) determinarea numarului de elemente pare din vector;

#include<iostream>
using namespace std; int nr_pare(int i)
{
int n,v[101]; if(i>n) return 0;
else
void citire(int i) if(v[i]%2==0) return 1+ nr_pare(i+1);
{ else return nr_pare(i+1);
if(i==0) }
{cout<<"n=";cin>>n;
citire(i+1);} int main()
else if(i<=n) {
{ citire(0);
cout<<"v["<<i<<"]="; cout<<nr_pare(1);
cin>>v[i]; return 0;
citire(i+1); }
}
}

g) determinarea mediei aritmetice ale elementelor impare din vector;

#include<iostream> float media_impare(int i,int s, int nr)


using namespace std; {
if(i>n)
int n,v[101]; if (nr==0) return 0;
else return (float)s/nr;
void citire(int i) else
{ if(v[i]%2==1)
if(i==0) return media_impare(i+1,s+v[i],nr+1);
{cout<<"n=";cin>>n; else return media_impare(i+1,s,nr);
citire(i+1);} }
else if(i<=n)
{ int main()
cout<<"v["<<i<<"]="; {
cin>>v[i]; citire(0);
citire(i+1); cout<<media_impare(1,0,0);
} return 0;
} }
h) determinarea cmmdc al elementelor din vector;

#include<iostream>
int cmmdc(int a, int b)
using namespace std;
{
if(b==0)
int n,v[101]; return a;
else
void citire(int i) return cmmdc(b,a%b);
{ }
if(i==0)
{cout<<"n=";cin>>n; int cmmdc_vector(int i,int a)
citire(i+1);} {
else if(i<=n) if(i>n) return a;
else
{
return
cout<<"v["<<i<<"]=";
cmmdc_vector(i+1,cmmdc(a,v[i]));
cin>>v[i]; }
citire(i+1);
}
}

int main()
{
citire(0);
cout<<"cmmdc="
<<cmmdc_vector(1,v[1]);
return 0;
}

You might also like