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

Vector I

The document discusses several common problems involving vectors in C++, including finding the minimum and maximum elements, sorting elements, removing an element, calculating sums and products, generating random elements, and displaying elements. Code snippets are provided as examples for solving each type of problem.

Uploaded by

Jason Lopez
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)
34 views5 pages

Vector I

The document discusses several common problems involving vectors in C++, including finding the minimum and maximum elements, sorting elements, removing an element, calculating sums and products, generating random elements, and displaying elements. Code snippets are provided as examples for solving each type of problem.

Uploaded by

Jason Lopez
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
You are on page 1/ 5

Vectori

Probleme de numrare:

Min si max:
#include <iostream>
using namespace std;
int main()
{
int n,i,v[101],max=0,min=10000;
cin>>n;
for(i=0;i < n;i++)
{
cin>>v[i];
}
for(i = 0; i < n; i++)
{
if(max < v[i])
{
max = v[i];
}
if(min > v[i])
{
min = v[i];
}
}
cout<<"Minimul este : "<<min<<endl<<"Maximul este : "<<max<<'\n';
return 0;
}

Probleme de sortare/ordonare:
#include <iostream>
using namespace std;
int main()
{
int v[100];
int n, i, j;
cin>>n;
for (i=0; i<n; i++)
{
cout<<"v["<<i<<"] = ";
cin>>v[i];
}
for (i=0; i<n-1; i++)
for (j=i+1; j<n; j++)

if (v[i] < v[j])


swap (v[i],v[j]);
for (i=0; i<=n; i++)
cout<<v[i]<<'\n';
for (i=0; i<n-1; i++)
for (j=i+1; j<=n; j++)
{
if (v[i] > v[j])
{
swap (v[i],v[j]);
}
}
for (i=0; i<n; i++)
cout<<v[i]<<'\n';
return 0;
}

Eliminarea unui element din vector:


#include <iostream>
using namespace std;
int main (){
int n, a[100], i, k;
cin>>n;
cin>>k;
for (i=0;i<n;i++){
cout<<"a["<<i<<"]="; cin>>a[i];
}
for (i=k;i<n;i++)
a[i-1]=a[i];

for (i=0;i<n-1;i++)
cout<<a[i]<<" ";
return 0;
}

Calculul sum/produs:
Suma elementelor:
#include <iostream>
using namespace std;
int main ()
{
int a[100],n,I,s=0;

for (i=1;i<=n;++i)
s=s+a[i];
cout<<s;
}

Produsul elementelor:
#include <iostream>
using namespace std;
int main ()
{
int a[100],n,i,p=1;
for (i=1;i<=n;++i)
p=p*a[i];
cout<<p;
}

Generarea elementelor unui vector:


#include <iostream>
using namespace std;
int imax=10;
int fr[imax];
int main(){
int x,n,imax=0;
cin>>n;
for (int i=0;i<n;++i){
}
cin>>x;
++fr[i];
}
for (int i=0;i<=9;++i)
if (imax<fr[i])
{imax=fr[i];
}
for (int i=0;i<=9;++i)
{
if (fr[i]==g)
cout<<i<<' ';
}
cout<<'\n';
cout<<imax;
return 0;
}

Afisare elemente:

You might also like