Oronarea crescatoare a elementelor unui vector
Varianta "do...while"
#include<iostream>
using namespace std;
int main()
{int a[100],n,i,aux,ordonat=0;
cout<<"n=";cin>>n;
for(i=0;i<n;i++)
{cout<<"a["<<i<<"]=";cin>>a[i];}
do{ordonat=1;
for(i=0;i<n-1;i++)
if(a[i]>a[i+1])
{aux=a[i];
a[i]=a[i+1];
a[i+1]=aux;
ordonat=0;}
}
while(ordonat==0);
for(i=0;i<n;i++)cout<<a[i]<<endl;
return 0;
}
Varianta "while"
#include<iostream>
using namespace std;
int main()
{int a[100],n,i,aux,ordonat=0;
cout<<"n=";cin>>n;
for(i=0;i<n;i++)
{cout<<"a["<<i<<"]=";cin>>a[i];}
while(ordonat==0)
{ordonat=1;
for(i=0;i<n-1;i++)
if(a[i]>a[i+1])
{aux=a[i];
a[i]=a[i+1];
a[i+1]=aux;
ordonat=0;}
}
for(i=0;i<n;i++)cout<<a[i]<<endl;
return 0;
}
Varianta R0il
#include<iostream>
using namespace std;
int main()
{int a[100],n,i;
cout<<"n=";cin>>n;
for(i=0;i<n;i++)
{cout<<"a["<<i<<"]=";cin>>a[i];}
sort(a,n+a);
for(i=0;i<n;i++)cout<<a[i]<<endl;
return 0;
}
vineri, 9 iulie 2010
by DlMuresan
Categories:
info,
vectori
|