0

Shift array

I want to write a program that get a number(n) and shift the array for (n) time like this how can i write it ? please help me input : 3 array : [1,2,3,4,5] first shift : 5 , 1 , 2 ,3 ,4 second shift : 4 , 5 , 1 , 2 , 3 third shift : 3 , 4 , 5 , 1 , 2

2nd Feb 2017, 4:03 PM
a.balagar
a.balagar - avatar
12 Antworten
+ 8
// Hatsy Rei, Senpai Co-op #include <iostream> #include <algorithm> using namespace std; int main() { char program_continue = 'Y'; int array[5] = {1, 2, 3, 4, 5}; int shift_count; while (program_continue == 'Y') { cin >> shift_count; for (int i = 0; i < shift_count; i++) { swap(array[4], array[0]); swap (array[3], array[4]); swap (array[2], array[3]); swap (array[1], array[2]); } for (int j = 0; j < 5; j++) { cout << array[j]; } cout << "Input Y to continue" << endl; cout << "Input any other character to quit" << endl; cin >> program_continue; } return 0; }
2nd Feb 2017, 5:21 PM
Hatsy Rei
Hatsy Rei - avatar
+ 8
// The code is completed! =^= // Hatsy Rei, Senpai Co-op #include <iostream> using namespace std; int main() { int array[5] = {1,2,3,4,5}; int shift_count; int temp; cin >> shift_count; for (int i = 0; i < shift_count; i++) { temp = array[0]; array[0] = array[4]; array[4] = array[3]; array[3] = array[2]; array[2] = array[1]; array[1] = temp; cout << "Shift " << i + 1 << " : "; for (int j = 0; j < 5; j++) { cout << array[j] << ", "; } cout << endl; } return 0; }
3rd Feb 2017, 12:24 PM
Hatsy Rei
Hatsy Rei - avatar
+ 6
Updated. Edit: OK, I think I understand what you mean. I'll edit the code again later. You mean you want the all the shifts to be displayed? From first shift to last shift?
3rd Feb 2017, 11:16 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
There is, but longer. I can try if you want.
3rd Feb 2017, 11:21 AM
Hatsy Rei
Hatsy Rei - avatar
+ 6
I am working on it. Approx 10 minutes please.
3rd Feb 2017, 12:10 PM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Persian! I think Persian is my favourite language that I know the least about. I have a friend that's Persian. :)
3rd Feb 2017, 11:33 AM
J.G.
J.G. - avatar
+ 1
thanks a lot
3rd Feb 2017, 12:26 PM
a.balagar
a.balagar - avatar
0
but i have a problem with it this code just give me one shift but i want to have numbers in the time that i give to the shift_count
3rd Feb 2017, 11:08 AM
a.balagar
a.balagar - avatar
0
is there any way that i dont need to use swap ?
3rd Feb 2017, 11:20 AM
a.balagar
a.balagar - avatar
0
can you speak persian?
3rd Feb 2017, 11:25 AM
a.balagar
a.balagar - avatar
0
ببینید من این برنامه رو نشون دادم گفتن که : "اینی که نوشتید حل مسئله به حساب نمیاد الان من بگم ۱۰۰۰ تا خونه شما تک تک میخواین این جوری swap کنید "
3rd Feb 2017, 11:58 AM
a.balagar
a.balagar - avatar
- 1
where are you from
3rd Feb 2017, 11:23 AM
a.balagar
a.balagar - avatar