As shown on the right, the game starts with a board and by using swap and reverse. The game ends when the player sorts out the bars on the board in increasing order (check the sample run on the right).
p1.cpp) with main() as shown
below.
int main()
{
char c;
int N;
cin >> c >> c >> N;
int* A = read(N);
print(A,N);
if (isInOrder(A,N))
cout << "Is in order!" << endl;
else
cout << "Is not in order!" << endl;
delete [] A;
return 0;
}
Requirement: Implement the following function that you see being used in the code.
~/$ ./p1 N = 4 : 1 4 5 9 1 4 5 9 --------- A B C D Is in order! |
~/$ ./p1 N = 6 : 1 3 4 4 6 8 1 3 4 4 6 8 ------------- A B C D E F Is in order! |
~/$ ./p1 N = 6 : 1 3 4 4 6 5 1 3 4 4 6 5 ------------- A B C D E F Is not in order! |
Submit as:
~/bin/submit -c=IC210 -p=lab07 p1.cpp
~/$ ./p2 Welcome to SWARE! board> N = 6 : 8 7 2 4 2 8 8 7 2 4 2 8 ------------- A B C D E F > swap A E 2 7 2 4 8 8 ------------- A B C D E F > swap B C 2 2 7 4 8 8 ------------- A B C D E F > swap C D 2 2 4 7 8 8 ------------- A B C D E F 9 points! (3 moves) |
~/$ ./p2 Welcome to SWARE! board> N = 5 : 2 5 3 5 2 2 5 3 5 2 ----------- A B C D E > swip Unknown move 'swip' 2 5 3 5 2 ----------- A B C D E > swap B E 2 2 3 5 5 ----------- A B C D E 3 points! (1 moves) |
~/$ ./p2 Welcome to SWARE! board> N = 6 : 1 3 3 1 4 4 1 3 3 1 4 4 ------------- A B C D E F > swap B D 1 1 3 3 4 4 ------------- A B C D E F 3 points! (1 moves) |
Submit as:
~/bin/submit -c=IC210 -p=lab07 p1.cpp p2.cpp
reverse x y , where x and y are column positions. Column x should
come before column y, and the command should reverse the values in cloumns x
through y. Like this:
9 1 2 5 9 8 7 6 4 reverse E H 9 1 2 5 6 7 8 9 4 ------------------- ------------> ------------------- A B C D E F G H I A B C D E F G H I
Examples are given below:
~/$ ./p3 Welcome to SWARE! board> N = 8 : 9 2 8 7 6 5 8 1 9 2 8 7 6 5 8 1 ----------------- A B C D E F G H > reverse C F 9 2 5 6 7 8 8 1 ----------------- A B C D E F G H > swap A H 1 2 5 6 7 8 8 9 ----------------- A B C D E F G H 5 points! (2 moves)
Submit as:
~/bin/submit -c=IC210 -p=lab07 p1.cpp p2.cpp p3.cpp
~/$ ./p4 Welcome to SWARE! board> N = 5 : 4 3 2 4 3 # # # # # # # # # # # # # # # # ----------- A B C D E > swap A E # # # # # # # # # # # # # # # # ----------- A B C D E > reverse A C # # # # # # # # # # # # # # # # ----------- A B C D E 5 points! (2 moves)Hint: This is made vastly easier by defining a function to find the max element in an array.
Submit as:
~/bin/submit -c=IC210 -p=lab07 p1.cpp p2.cpp p3.cpp p4.cpp