$29.99
CMPSC 201
Programming Project 4
May be done with one other student from your section
Worth 55 points
Due by 11:00 P.M. on 4/15
You may choose one of the 2 following options for this project. Submit the required source
code for the option you have chosen. For both options, you should employ good
programming concepts for processing data, e.g. do not write a number of individual
statements if a loop could be used, use functions for code that will be used mulitple times,
etc.
If you choose to work with another student from your section, both your names must be in
the introductory comments and both of you must make a submission to ANGEL using your
own account.
Option 1: Navigation over Terrain
Problem: Navigation over a terrain can be an important concept. Usually you want to avoid
high areas (peaks) and low areas (valleys). For this problem, you may assume the terrain that
you want to navigate over may be represented as a 2-dimensional grid of cells. You are to
determine the number of peaks and valleys in this terrain as well as give the location of these
peaks and valley. A peak is defined as a cell for which all eight neighbors are lower than the cell
being considered. A valley is defined as a cell for which all eight neighbors are higher than the
cell being considered. An example grid is given below with some of the peaks in bold and some
of the valleys underlined. Note that no cell on the perimeter may be considered a peak or valley.
The elevation of each cell is given as an integer in the file terrain.dat which is posted on
ANGEL. The first line gives how many rows and columns are in the array. Do not edit this file
in any manner or change its name. Your program should
Use a function to input the values from the file (remember the first line in the file
contains the number of rows and columns respectively).
Use a function to locate peaks and valleys and store the locations (row and column
indices) of these peaks and valleys.
Use a function to calculate the average elevation over the entire grid and determine the
maximum gradient. The gradient is the absolute difference between two adjacent cells.
For example in the array given below, the cell containing 43 (row 2 and column 3) would
have adjacent cell containing the values 63, 45, 38 and 40. Design your code so you do
not compare the same two cells more than once.
Use a function to output the number of peaks or valleys and elevations associated with
the peak or valley and the locations of these peaks or valleys (this file will be called twice
from main). Review pages 658 through 661 on how to open an output file multiple times
without deleting the previous data stored in it.
Create an output file containing number of peaks, number of valleys, and locations ([row,
column]) of these peaks and valleys, the average height, and maximum gradient.
Programming Project 4 2 CMPSC 201 – Spring 2016
Your main function should
Declare the 2-Dimensional array (no input file will contain more than 100 rows and 100
columns).
Call the function to import the data
Call the function to locate the peaks, valleys, and locations of the peaks and valleys.
Call the function to calculate the average and maximum gradient. Call the function to
output peaks or valleys and locations with peak information.
Call the function to output peaks or valleys and locations with valley information.
Call the function to determine the overall elevation average and maximum gradient and
output these.
Example Grid (6 rows with 8 columns). Peaks are given in bold and valleys are underlined. You
could use this array as test data.
25 58 63 23 21 34 21 50
32 45 43 40 41 32 30 27
34 40 38 39 36 28 30 35
40 45 42 48 32 34 29 32
39 39 40 42 47 49 27 30
31 31 31 32 32 33 44 35
Carefully consider what each function is supposed to do, what it needs to receive from the
function call and what it will return to the function call. Do not anything to the function that it
does not need to complete its task.
Option 2: Naval Warfare
Problem: Create a program to play a variation of the “Battleship” game using computer
generated locations of the ships. The ships will be located on a 15 by 15 grid. Your program
should use random numbers (remember to seed the random number function so it does not put
the ships in the same location every game) to locate the five ships and direction of the ships (may
be up/down or right/left). The lengths of the ships are:
Frigate: 2 squares
Tender: 2 squares
Destroyer: 3 squares
Cruiser: 4 squares
Carrier: 5 squares
Your program must select the location of the first square of each ship and then choose the
direction for the remaining of squares for the particular ship using random numbers. Ships
cannot go off the grid and cannot overlap. Store the ship locations in one 2-dimensional array
that indicates ship type and location.
Once the program has generated the location of the ships, your program should ask the
player to select a possible square by coordinates of 1 through 15 for the rows and A through O
for the column. Your program should determine if the user’s guess was a hit or miss and store
Programming Project 4 3 CMPSC 201 – Spring 2016
the result in a second 2-Dimensional array. If the result was a hit, your program will need to
check whether the specific ship that was hit has been hit in all of that ship’s location and is
therefore sunk. If a ship has been sunk, your program should output which ship was sunk. After
each guess, your program should output the 2-dimensional array showing the player’s guesses,
indicating whether the guesses were hits or misses, and squares not chosen as guesses yet.
Game ends when the player sinks all 5 ships or has exhausted all 150 of his/her allotted
guesses. If all 5 ships were sunk, the program should output a message stating the player won. If
not all 5 ships were sunk, the message should state the player lost. In either case the 2-D array
showing the location of the ships should be output. Ship locations should also indicate what type
of ship was located there, i.e. use a different symbol for frigate than used for carrier. You
program should allow the player to play the game again without exiting the program.
Attach your C++ file to the appropriate drop box by 11:00 pm on Monday 4/15/16.