Executive Summary

```~/\$ ~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.

Part 0 : Some warmup array functions

Write a program 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.
Note: For Parts 0, 1 and 2 you may assume that input elements (i.e. everything other than "N") are at most 9. That way there's no problem lining up the numbers and the letters.
```int main()
{
int N;
print(A,N);
if (isInOrder(A,N))
cout << "Is in order!" << endl;
else
cout << "Is not in order!" << endl;
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! ```

Part 1 : Welcome to PutInOrder!

Do this first: copy the `gena` program to your lab directory: `cp ~wcbrown/bin/gena .`
Or from your VM: `scp mich302csd01u.academy.usna.edu:/home/scs/wcbrown/bin/gena .`
You will implement a simple game as described below. Game instances are generated using the program `./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 in the fewest moves. 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)```
Note 1: Your solution should make nice use of functions! At least you should have functions for printing a board and determining whether a board is in order.
Note 2: For parts 1 and 2 you may assume that none of elements is a number greater than 10.

Part 2 : Reverse your fortunes

Build on your Part 1 solution by providing the player with a new move, `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```
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)```

Part 3 : Going Further — Let's get graphical

The game is more compelling if the board is printed out "graphically", which for us will mean a sort of "ASCII Art" thing. Specifically, you must produce game boards in the style depicted below.
```~/\$ ./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.

Part 4 : Going Further — Cycle and Recycle

Let's make one last "move" for the game: `cycleleft 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)```

Part 5 : Going Further — read from file as well as standard in

Modify your program so that the user can can enter a filename rather than `N = ...` 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)```

Part 6 : Going Further — reading from a graphical representation

As a real challenge, change this program so that it reads its input (whether from cin or from a file) not in the nice format we've used so far, but in the same graphical format as it prints to the screen. Running `gena` 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```