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:
chartheGrid[ ROWS+1][COLS+1];
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.
countApples( theGrid,totalApples);

Recommended Answers

All 3 Replies

sub("exciting","oh no, not again, when will people learn to read intro threads")

> A programmer has been asked
Again, "YOU have been asked"

> write down any assumptions that you may make,
Are you going to put
"I assumed I could get some schmuck on the web to do it for me".
"I was wrong".

Show some effort, then we'll help.

int inputValidNumber (int cols, int rows)

{

int cols, rows;

 

cout<<"enter cols number 1 – 10";

cin>>cols;

 

cout<<"enter rows number 1 – 20";

cin>>rows;

 

     while (!((cols > 0 && cols < 11) && (rows > 0 && rows < 21)))

 

    {

 

    cout<<"invalid  cols or rows number";

    cout<<"try again


    cout<<"enter cols number 1 – 10";

    cin>>cols;

 

    cout<<"enter rows number 1 – 20";

    cin>>rows;

    }

return 0;

}

Brizhou, Is that all have you got so far. The was a poor attempt. And where is your code tag. No one of gonna ook at your took.

Let me ask, have you understood the question throughly? If not you need to sit down and look at it and make sure you understand what they are really expecting from you.

ssharish

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.