A programmer has been asked to implement a simple game that requires the use of a board to position and manipulate some objects represented by symbols.
The programmer has implemented the board as a two-dimensional array of characters. The following constants have been declared as global to the whole program:
const int ROWS( 20); //vertical dimension
const int COLS( 10); //horizontal dimension
The main program (i.e., top level algorithm) contains the following local variable declaration:
In the questions below you are asked to write three C++ functions that will be called from the main program. Some of them will use a 2D array such as theGrid passed as a parameter.
You may assume that when asked to enter a grid reference (column or row number) the user types in a number and that the values needed for any computation exist in the array theGrid.
Where appropriate, you should:
• write down any assumptions that you may make,
• assume that the global variables used are already declared,
• declare any new local variables used,
• use meaningful names, clear indentation and comments.
You must NOT use recursion.
Marks will be given for the style of your solutions and how easy they are to read and understand.
1. Write a C++ function, called inputValidNumber, which prompts the user for a value in a given range, reads in a number, checks that it is in a range specified and returns it. While the value read in is not in the specified range, the system repeatedly asks for another one until a valid value is input.
The function takes 2 parameters respectively representing the minimum and maximum of the valid range (both inclusive) and returns the valid value read in.
For example, the following statement sets the value of a variable number to the first number input by the user that is greater than or equal to 1 and less than or equal to20.
intnumber = inputValidNumber( 1, ROWS);
2. Write a C++ function, called findMousePosition, which looks for the position of the mouse on the board.
The mouse is represented in the array theGrid by the character '@'.
The function takes three parameters: the first one is an array that represents the current configuration of the board, the second and third onesare respectively set to the row and column numbers of the cell that contains the mouse.
You may assume that there is always a single mouse is on the board.
For example, the following statement sets the value of the integer variables rowMouseandcolMouse to the coordinates of the character '@' in the array theGrid.
findMousePosition( theGrid, rowMouse, colMouse);
3. Write a C++ function, called countApples, which counts the number ofapplescurrently on the grid, i.e., number of cells that contain the character 'O' used to represent an apple.
The function takes two parameters: the first one is an array that represents the current configuration of the board and the second one represents the number of apples it currently contains.
For example, the following statement sets the value of the integer variable totalApples to the number of times (may be 0) the character 'O' is present in the array theGrid.