Teza Info
Teza Info
Teza Info
#include <iostream>
using namespace std;
void suma(int n)
{
int s=0;
while(n!=0){
s=s+n%10;
n=n/10;
}
cout<<s;
}
int main()
{
int n;
cin>>n;
suma(n);
return 0;
}
14.citire vector.
void citire(int v[100], int& n)
{
cin>>n;
for(int i=1; i<=n;i++)
cin>>v[i];
}
15.afisare vector
void afisare(int v[100], int n)
{
for(int i=1; i<=n;i++)
cout<<v[i]<<" ";
}
20.cautarea secventiala.
#include<iostream>
using namespace std;
int main()
void main()
{
int v[10], i, n, gasit=0, k, poz;
cout<<"Nr. elemente = ";
cin>>n;
for ( i=1; i<=n; i++)
{
cout<<"Elementul = ";
cin>>v[i];
}
cout<<“\n Cautam elementul : ";
cin>>k;
for (i=1; i<=n; i++)
if (v[i]==k)
{
poz=i;
gasit=1;
}
if (gast == 1)
cout<<“Gasit pe pozitia "<<poz;
else cout<<k<<“ nu e in vector";
return 0;
}
21.cautarea binara.
#include<iostream>
using namespace std;
int main()
{
int a[9], n,i, x, gasit=0, st, dr, mij;
cin>>n;
for(i=1; i<=n; i++)
cin>>a[i]<<" ";
dr=n;
st=0;
cout<<"\nElementul cautat = ";
cin>>x;
while (st<=dr && !gasit)
{
mij=(st+dr)/2;
if(a[mij]==x) gasit=1;
else if (x<a[mij]) dr=mij-1;
else st=mij+1;
}
if (gasit==0) cout<<"Element negasit !";
else cout<<"Gasit pe pozitia "<<mij;
return 0;
22.sortare crescatoare
#include <iostream>
using namespace std;
void citire(int v[100], int n)
{
for(int i=1; i<=n; i++)
cin>>v[i];
}
void sortare_crescator(int v[], int n)
{
int i, j, aux;
for (i=1; i<n; i++)
for(j=i+1; j<=n; j++)
if (v[i]>v[j])
{
aux=v[i];
v[i]=v[j];
v[j]=aux;
}
for (i=1; i<=n; i++)
cout<<" "<<v[i];
}
int main()
{
int v[100],n;
cin>>n;
citire(v,n);
sortare_crescator(v,n);
return 0;
}
23.sortare descrescatoare
#include <iostream>
using namespace std;
void citire(int v[100], int n)
{
for(int i=1; i<=n; i++)
cin>>v[i];
}
void sortare_descrescator(int v[], int n)
{
int i, j, aux;
for (i=1; i<n; i++)
for(j=i+1; j<=n; j++)
if (v[i]<v[j])
{
aux=v[i];
v[i]=v[j];
v[j]=aux;
}
for (i=1; i<=n; i++)
cout<<" "<<v[i];
}
int main()
{
int v[100],n;
cin>>n;
citire(v,n);
sortare_descrescator(v,n);
return 0;
}