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
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;
}
+ 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;
}
+ 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?
+ 6
There is, but longer. I can try if you want.
+ 6
I am working on it. Approx 10 minutes please.
+ 3
Persian! I think Persian is my favourite language that I know the least about. I have a friend that's Persian. :)
+ 1
thanks a lot
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
0
is there any way that i dont need to use swap ?
0
can you speak persian?
0
ببینید من این برنامه رو نشون دادم گفتن که :
"اینی که نوشتید حل مسئله به حساب نمیاد الان من بگم ۱۰۰۰ تا خونه شما تک تک میخواین این جوری swap کنید "
- 1
where are you from