Swap and Array

Nov 28, 2014 at 4:16am
this code will return output 2 3 4 5 1, i'd like to make the output is 5 1 2 3 4, can comeone help me, 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
  #include <iostream>
#include <algorithm>
#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(int i = 0; i < 4; i++)
	{
		swap(data[i],data[i+1]);
	}
	cout<<"Sorting Number: "<<endl;
	for(i=0; i<5; i++)
	{
		cout<<data[i]<<" ";
	}
	
	getch();
}
Nov 28, 2014 at 4:24am
Nov 28, 2014 at 4:33am
can you plae the rotate algorithm at the code, please?
Nov 28, 2014 at 4:45am
feehily wrote:
can you plae the rotate algorithm at the code, please?
I do not understand your request.
Nov 28, 2014 at 5:01am
can you correct the code, please? using rotate algorihtm
Nov 28, 2014 at 5:13am
I know what you want your program to do, but I don't know how you want it to be done. Should the user enter five numbers and then enter the number of times to rotate it? Or do they enter the number they want to be the first number? Only you know.
Nov 28, 2014 at 5:24am
the user enter five number and then the program will be return output that the last number will be at the first. example: 1 2 3 4 5, then the output is 5 1 2 3 4.
or 6 7 8 9 10, the output is 10 6 7 8 9.
Nov 28, 2014 at 5:26am
You don't need arrays for this.
1
2
3
int a, b, c, d, e;
std::cin >> a >> b >> c >> d >> e;
std::cout << e << a << b << c << d << std::endl;
Nov 28, 2014 at 5:37am
but it must using array and loop for. pleasee
Nov 28, 2014 at 1:07pm
Replace you swap in line 18 with swap(data[i],data[4]);
Topic archived. No new replies allowed.