0% found this document useful (0 votes)
96 views12 pages

C++ (Atat Am Gasit)

The document contains C++ code snippets for sorting and manipulating arrays and vectors. Specifically: 1) The code shows how to sort a 2D array in ascending order using bubble sort by swapping rows. 2) Binary search is implemented to search for a value in a sorted 1D array. 3) Two sorted arrays are merged together into a single sorted output array. 4) Adjacent averages are calculated by inserting the average of adjacent elements into a new vector.

Uploaded by

raffyunicatul
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views12 pages

C++ (Atat Am Gasit)

The document contains C++ code snippets for sorting and manipulating arrays and vectors. Specifically: 1) The code shows how to sort a 2D array in ascending order using bubble sort by swapping rows. 2) Binary search is implemented to search for a value in a sorted 1D array. 3) Two sorted arrays are merged together into a single sorted output array. 4) Adjacent averages are calculated by inserting the average of adjacent elements into a new vector.

Uploaded by

raffyunicatul
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

SENT:

Sa se ordoneze crescator coloana:

#include<iostream>
using namespace std;
int a[30][30],n,i,j,aux,gasit;
int main (void)
{cout<<"n=";cin>>n;
for(i=0;i<n;i++)
 for(j=0;j<n;j++)
 { cout<<"a["<<i+1<<"]["<<j+1<<"]=";
 cin>>a[i][j];
 }
for(i=0;i<n;i++)
{for(j=0;j<n;j++)
     cout<<a[i][j]<<" " ;
     cout<<endl;
}
do{gasit=0;
for(i=0;i<n-1;i++)
 if(a[i][i]>a[i+1][i+1])
  {for(j=0;j<n;j++)
  {aux=a[i][j];
  a[i][j]=a[i+1][j];
  a[i+1][j]=aux;
  }
 for(j=0;j<n;j++)
    {aux=a[j][i];
  a[j][i]=a[j][i+1];
  a[j][i+1]=aux;}
  gasit=1;
 } 
}while(gasit);
for(i=0;i<n;i++)
{ for(j=0;j<n;j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
return 0;
}
Cautare binara

#include<iostream>
using namespace std;
int A[101],x,k,i,n,gasit=0,li,ls;
int main(void)
{cout<<"Introduceti nr de elemente al vectorului A= ";
cin>>n;
cout<<"x= ";
cin>>x;
for(i=0;i<n;i++)
 {cout<<"A["<<i<<"]= ";
    cin>>A[i];
 }
li=0;ls=n-1;gasit=0;
while(li<=ls&&!gasit)
{k=(li+ls)/2;
      if(x=A[k])
         {  cout<<"S-a gasit pe pozitia"<<k+1;
         gasit=1;}
     else if(A[k]<x)
      li=k+1;
   else ls=k-1;
     }
if(li<ls)
 cout<<"Nu s-a gasit numarul cautat";
return 0;
}
2 foldere intercalate text

#include<fstream>
using namespace std;
int A[100],B[100],C[200],m,n,i,j,k;
int main (void)
{ifstream f("intrare.in");
 ofstream g("iesire.out");
 f>>m;
 f>>n;
 for(i=0;i<m;i++)
 f>>A[i];
for(j=0;j<n;j++)
 f>>B[j];
i=0;
j=0;
k=0;
while (i<m&&j<n)
{if (A[i]<B[j])
    {C[k]=A[i];
     k++;
     i++;
 }
else
     {C[k]=B[j];
      k++;
      j++;
 }
}
if(i<m)
 for(j=i;j<m;j++)
  {C[k]=A[j];
      k++;
 }
else for(i=j;i<n;i++)
      {C[k]=B[i];
      k++;
   }
for(i=0;i<k;i++)
     g<<C[i]<<" " ;
return 0;
}
Interclasarea

#include<iostream>
using namespace std;
int A[100],B[100],C[200],m,n,i,j,k;
int main (void)
{
 cout<<"introduceti m= ";cin>>m;
    cout<<"introduceti n= ";cin>>n;
for(i=0;i<m;i++)
 {cout<<"A["<<i<<"]"<<endl;
    cin>>A[i];}
for(j=0;j<n;j++)
 {cout<<"B["<<j<<"]"<<endl;
    cin>>B[j];}
i=0;
j=0;
k=0;
while (i<m&&j<n)
{if (A[i]<B[j])
    {C[k]=A[i];
     k++;
     i++;
 }
else
     {C[k]=B[j];
      k++;
      j++;
 }
}
if(i<m)
 for(j=i;j<m;j++)
  {C[k]=A[j];
      k++;
  }
else for(i=j;i<n;i++)
      {C[k]=B[i];
      k++;
   }
for(i=0;i<k;i++)
 cout<<C[i]<<" " ;
return 0;
}
Componente alaturate

#include<iostream>
using namespace std;
int x[20],y[20];
int i,j,n;
int main (void)
{cout<<"n= ";cin>>n;
  for(i=1;i<n;i++)
{cout<<"x["<<i<<"]=";
     cin>>x[i];}
    i=1;
    j=1;
while(i<=n)
{ y[j]=x[i];
  y[j+1]=(x[i]+x[i+1])/2;
  i=i+1;
  j=j+2;
}
for(i=1;j<j-1;i++)
    cout<<y[i]<<" ";
return 0;
}
No subject 

#include<iostream>
using namespace std;
int a[50],n,i,j,k,minim,aux;
int main(coid)
{ cin>>n;
  for(i=0;i<n;i++)
  {cout<<"a["<<i+1<<"]=";
   cin>>a[i];}
   for(i=0;i<n-1;i++)
   {  minim=a[i];k=1;//k-pozitia minimului
      for(j=i+1;j<n;j++)
        if(a[j]<minim}
        {minim=a[j];
          k=j;
         }
   aux=a[k];
   a[k]=a[i];
   a[i]=aux;
for(i=0;i<n;i++)
cout<<a[i]<<" " ;
return 0;
}
No subject

#include<iostream>
using namespace std;
int a[40],n,i,gasit=0,aux;
int main(void)
{cout<<"n= ";cin>>n;
for(i=0;i<n;i++)
    cout<<"a"<<i+1<<":";
cin>>a[i];
for(i=0;i<n;i++)
    cout<<a[i]<<" " ;
    do{gasit=0;
    for(i=0;i<n;i++)
        if(a[i]>a[i+1])
        {aux=a[i];
        a[i]=a[i+1];
        a[i+1]=aux;
        gasit=1;}
        while(gasit);
        for(i=0;i<n;i++)
            cout<<a[i]<<" ";
        return 0;
    }
       
No subject

#include<fstream>
using namespace std;
int main(void)
{
    int v[20],i,maxim,medie,nr=0,n;
    ifstream f("intrare.int");
    ofstream g("iesire.out");
    f>>n;
    for(i=0;i<n;i++)
        f>>v[i];
    maxim=v[0];
    for(i=0;i<n;i++)
    {if(v[i]%2==0)
    {nr=nr+1;
    medie=medie+v[i];
    }
    if(v[i]>maxim) maxim=v[i];
    }
    g<<"Media aritmetica: "<<medie/nr;
    g<<"Componenta maxima: "<<maxim;
    return 0;
}
No subject

#include<iostream>
using namespace std;
int a,b,c,r;
int main (void)
{cout<<"introduceti valori ptr a si b"<<endl;
cout<<"a=";cin>>a;
cout<<"b=";cin>>b;
if(a,b) c=a;
a=b;
b=c;
r=a%b;
while(r!=0)
{a=b;
b=r;
r=a%b;
}
cout<<"c.m.m.d.c="<<b;
return 0;
}
No subject

#include<iostream>
using namespace std;
int a,b;
int main (void)
{cout<<"a=";cin>>a;
cout<<"b=";cin>>b;
if(a==b) cout<<"c.m.m.d.c.="<<a;
while(a!=b)
{if(a>b) a=a-b;
    else b=b-a;
}
cout<<"c.m.m.d.c.="<<a;
return 0;
}
No subject

#include<iostream>
using namespace std;
int n,S,i,a;
int main (void)
{cout<<"Introduceti o valoarea pentru n"<<endl;
cout<<"n=";cin>>n;
i=1;
S=0;
while(i<=n)
{cout<<"Introduceti o valoare pentru a"<<endl;
cout<<"a=";cin>>a;i=i+1;
if(a>=0) S=S+a;
}
cout<<"Suma numerelor pozitive este egala cu="<<S;
return 0 ;
}

#include<iostream>
using namespace std;
int a,b,c;
int main(void)
{cout<<"a=";cin>>a;
b=c;
c=0;

while(b!=0)
{c=c*10+b&10;
b=b/10;
}
if(a==c)cout<<"a este palindrom";
else cout<<"a nu este palindrom";
return 0;
}
Tema: cu metoda bubble sort

#include<iostream>
using namespace std;
float x[1000],y[1000],minim,aux;
int n,i,j,t,k;
int main(void)
{
 cout<<"introduceti numarul de nr= "; cin>>n;
 for(i=1;i<=n;i++)
 { cout<<"x["<<i<<"]=";
 cin>>x[i];
 }
 i=1; j=1;
 while(i<=n)
 {
  y[j]=x[i];
  y[j+1]=(x[i]+x[i+1])/2;
  i=i+1;
  j=j+2;
 }
 cout<<"numerele din vector sunt: ";
 for(i=1;i<j-1;i++)
  {cout<<y[i];
 if(i<j-2)
     cout<<", ";}

  cout<<endl<<"numerele din vector in ordine crescatoare sunt: ";

  for(i=1;i<j-1;i++)
{ minim=y[i];k=i;//k e pozitia minimului
for(t=i+1;t<j-1;t++)
    if(y[t]<minim)
    {minim=y[t];
    k=t;}
    aux=y[k];
    y[k]=y[i];
    y[i]=aux;}

for(i=1;i<j-1;i++)
  {cout<<y[i];
 if(i<j-2)
     cout<<", ";}
 
 return 0;
}

You might also like