Array and Swap
Can someone help me to use swap in this code so i can swap data[0] to data[4]. if inputs is 1 2 3 4 5, then the output will be 2 3 4 5 1.
help please
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int data[5];
int i, j, tmp;
for(i=0; i<5; i++)
{
cout<<"Enter 5 Number Value: "<<(i+1)<<" : ";
cin>>data[i];
}
for(i=0; i<9; i++)
{
for(j=i+1; j<5; j++)
{
if(data[i]>data[j])
{
tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
}
}
cout<<"Sorting Number: "<<endl;
for(i=0; i<5; i++)
{
cout<<data[i]<<" ";
}
getch();
}
|
Topic archived. No new replies allowed.