( )
#include<iostream.h>
const int n=5;
void main()
{
cout<<" Bubble Sort "<<endl;
int i;
int r;
int a[n];
cout<<"enter array a:"<<endl;;
for(i=0;i<n;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
cout<<endl;
cout<<" array a:"<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<endl;
// BubbleSort
bool t=true;
while (t)
{
t=false;
for(i=0;i<n-1;i++)
if (a[i]>a[i+1])
{
r=a[i];
a[i]=a[i+1];
a[i+1]=r;
t=true;
}
}
cout<<" After sorting the array a:"<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<' ';
cout<<endl;
}
3.
#include <iostream>
using namespace std;
const int n=5;
void main()
{
int i,j,temp;;
int a[n];
cout<<"enter array a:"<<endl;
for (i = 0; i<n; i++)
{
cout<<"a["<<i<<"]=";
cin >> a[i];
}
cout<<"array a:"<<endl;
for (i = 0; i<n; i++)
cout<<a[i]<<' ';
cout<<endl;
for (i=0; i<n; i++)
{
temp = a[i];// i-
j =i-1;// i-1
while(j >= 0 && a[j] > temp)
//
// i-,
// temp
{
a[j + 1] = a[j];
//
j--;
}
a[j + 1] = temp;
// i
}
//
cout<<"sort array a:"<<endl;
for (i = 0; i<n; i++)
cout<<a[i]<<' ';
cout<<endl;
}
: ()
(.. /C++
: (. 136)
)
7
, , , - , ,
//arb2016_pavlovskaya_var7
#include <iostream>
using namespace std;
const int n=7;
void main()
{
int i,temp;;
int a[n];
cout<<"enter array a:"<<endl;
for (i = 0; i<n; i++)
{
cout<<"a["<<i<<"]=";
cin >> a[i];
}
cout<<"array a:"<<endl;
for (i = 0; i<n; i++)
cout<<a[i]<<' ';
cout<<endl;
// if n is even number n=6
/* for(i=1;i<n/2;i=i+1)
{
temp=a[i];
a[i]=a[n-1-i];
a[n-1-i]=temp;
}
*/
// if n is odd number n=7
for(i=0;i<n/2;i=i+2)
{
temp=a[i];
a[i]=a[n-i-2];
a[n-i-2]=temp;
|
|
}
cout<<" odd i in begin, even i in end a:"<<endl;
for (i = 0; i<n; i++)
cout<<a[i]<<' ';
cout<<endl;
}
8
, , 1,
//arb2016_pavlovskaya_var8
#include <iostream>
#include <cmath>
using namespace std;
const int n=7;
void main()
{
int i,j,temp;;
double a[n];
cout<<"enter array a:"<<endl;
for (i = 0; i<n; i++)
{
cout<<"a["<<i<<"]=";
cin >> a[i];
}
cout<<"array a:"<<endl;
for (i = 0; i<n; i++)
cout<<a[i]<<' ';
cout<<endl;
i=0; j=n-1;
while(i<=j)
{
for(; fabs(a[i])<=1;i++);
for(; fabs(a[j])> 1;j--);
if (i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
cout<<"new array a:"<<endl;
for (i = 0; i<n; i++)
cout<<a[i]<<' ';
cout<<endl;
}
9
, 0 .
//arb2016_pavlovskaya_var9
#include <iostream>
using namespace std;
const int n=7;
void main()
{
int i,j,temp;;
double a[n];
cout<<"enter array a:"<<endl;
for (i = 0; i<n; i++)
{
cout<<"a["<<i<<"]=";
cin >> a[i];
}
cout<<"array a:"<<endl;
for (i = 0; i<n; i++)
cout<<a[i]<<' ';
cout<<endl;
i=0; j=n-1;
while(i<=j)
{
for(; a[i]!=0;i++);
for(; a[j]==0;j--);
if (i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
cout<<"new array a:"<<endl;
for (i = 0; i<n; i++)
cout<<a[i]<<' ';
cout<<endl;
}
.
++ . .
, 5*6: int a[5][6].
. , , . , , , , .