~/$ ~wcbrown/bin/gena -n 6 8 112 N = 6 : 8 1 6 6 2 4 ~/$ ~wcbrown/bin/PutInOrder Welcome to PutInOrder! board> z.6.8.112 # # # # # # # # # # # # # # # # # # # # # # # # # # # ------------- A B C D E F > swap C E # # # # # # # # # # # # # # # # # # # # # # # # # # # ------------- A B C D E F > cycleleft 1 # # # # # # # # # # # # # # # # # # # # # # # # # # # ------------- A B C D E F > reverse C E # # # # # # # # # # # # # # # # # # # # # # # # # # # ------------- A B C D E F 6 points! (3 moves)This lab will give you practice writing functions dealing with arrays — in this case arrays of ints. The end result, if you finish it all, will be a simple game. You can try the game yourself in the lab, as shown on the right.
p0.cppp0.cpp)
with main() as shown below. This will, of
course, require implementing the functions read, print and
isInOrder that you see being used in the code. The output needs
to match the examples shown below.
int main()
{
int 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;
}
~/$ ./p0 N = 4 : 1 4 5 9 1 4 5 9 --------- A B C D Is in order! |
~/$ ./p0 N = 4 : 1 5 3 2 1 5 3 2 --------- A B C D Is not in order! |
~/$ ./p0 N = 7 : 1 3 4 4 6 8 9 1 3 4 4 6 8 9 --------------- A B C D E F G Is in order! |
Submit as: ~/bin/submit -c=IC210 -p=Lab08 p0.cpp
p1.cppgena program to your lab
directory: cp ~wcbrown/bin/gena .
scp mich302csd01u.academy.usna.edu:/home/scs/wcbrown/bin/gena .
./gena, as shown in the
following example:
~/$ ./gena -n 6 8 1 Note: gena also writes the same output to a file named z.6.8.1 so you can access it later
N = 6 : 8 7 2 4 2 8
The game described by this is a list of 6 (that's "N") numbers,
and your goal is to put them in order from smallest to largest
by swapping elements, and to do it with the lowest score (each
"swap" costs 3 points).
N is never greater than 26.
The examples below ought to show you the basics of how the game
functions, including the two user-commands: help and swap.
~/$ ./p1 Welcome to PutInOrder! 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) |
~/$ ./gena -n 5 5 8 N = 5 : 2 5 3 5 2 ~/$ ./p1 Welcome to PutInOrder! board> N = 5 : 2 5 3 5 2 2 5 3 5 2 ----------- A B C D E > swip Unkown 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) |
~/$ ./gena -n 6 4 12 N = 6 : 1 3 3 1 4 4 ~/$ ./p1 Welcome to PutInOrder! board> N = 6 : 1 3 3 1 4 4 1 3 3 1 4 4 ------------- A B C D E F > help PutInOrder! The goal is to order from smallest to largest with the lowest score. The moves are: swap x y - swaps the values in column x and column y Scoring: swap = 3pts 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=Lab08 p0.cpp p1.cpp
p2.cppreverse 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
Otherwise the elements should be completely unchanged.
A "reverse" move costs two points.
Now the
player has two moves to make, and should be able to creatively
find shorter/lower-point solutions.
~/$ ./p2 Welcome to PutInOrder! board> N = 8 : 9 2 8 8 6 5 8 1 9 2 8 8 6 5 8 1 ---------------- A B C D E F G H > reverse C F 9 2 5 6 8 8 8 1 ---------------- A B C D E F G H > swap A H 1 2 5 6 8 8 8 9 ---------------- A B C D E F G H 5 points! (2 moves)
Submit as: ~/bin/submit -c=IC210 -p=Lab08 p0.cpp p1.cpp p2.cpp
p3.cpp~/$ ./p3 Welcome to PutInOrder! 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=Lab08 p0.cpp p1.cpp p2.cpp p3.cpp
p4.cppcycleleft k.
A "cycleleft by one" move shifts every column one position to
the left, with the leftmost column wrapping around to become the
rightmost column. A "cycleleft by k" is just applying the
cycleleft-by-one k-times over. I strongly recommend you
implement it this way, i.e. get cycleleft by one working and
then call it multiple times to get cycleleft by k working.
Note, a cycleleft k move costs only 1 point.
~/$ ./p4 Welcome to PutInOrder! board> N = 6 : 8 1 4 8 5 4 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ------------- A B C D E F > cycleleft 1 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ------------- A B C D E F > swap C E # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ------------- A B C D E F 4 points! (2 moves) |
$ ./p4 Welcome to PutInOrder! board> N = 6 : 6 2 8 1 7 3 # # # # # # # # # # # # # # # # # # # # # # # # # # # ------------- A B C D E F > cycleleft 3 # # # # # # # # # # # # # # # # # # # # # # # # # # # ------------- A B C D E F > swap B E # # # # # # # # # # # # # # # # # # # # # # # # # # # ------------- A B C D E F 4 points! (2 moves) |
$ ./p4 Welcome to PutInOrder! board> N = 8 : 3 4 5 6 7 8 1 2 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ----------------- A B C D E F G H > cycleleft 6 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ----------------- A B C D E F G H 1 points! (1 moves) |
Submit as: ~/bin/submit -c=IC210 -p=Lab08 p0.cpp p1.cpp p2.cpp p3.cpp p4.cpp
p5.cppN = ... and the game will read the
input (in the same format) from the file rather than cin.
~/$ ./gena -n 4 7 99 N = 4 : 3 2 7 1 ~/$ ./p5 Welcome to PutInOrder! board> N = 4 : 3 2 7 1 # # # # # # # # # # # # # --------- A B C D > cycleleft 3 # # # # # # # # # # # # # --------- A B C D > reverse B C # # # # # # # # # # # # # --------- A B C D 3 points! (2 moves) |
~/$ cat z.4.7.99 N = 4 : 3 2 7 1 ~/$ ./p5 Welcome to PutInOrder! board> z.4.7.99 # # # # # # # # # # # # # --------- A B C D > cycleleft 3 # # # # # # # # # # # # # --------- A B C D > swap B C # # # # # # # # # # # # # --------- A B C D 4 points! (2 moves) |
Submit as: ~/bin/submit -c=IC210 -p=Lab08 p0.cpp p1.cpp p2.cpp p3.cpp p4.cpp p5.cpp
p6.cppgena without the -n option
produces inputs in this format, e.g.
~/$ ./gena 8 12 122
N = 8
#
# #
# #
# #
# # #
# # #
# # #
# # #
# # # # #
# # # # # #
# # # # # # #
# # # # # # # #
-----------------
A B C D E F G H
Submit as: ~/bin/submit -c=IC210 -p=Lab08 p0.cpp p1.cpp p2.cpp p3.cpp p4.cpp p5.cpp p6.cpp