0% found this document useful (0 votes)
55 views

Program 2

The document describes a program that simulates a robot moving randomly within a rectangular grid. The program takes in parameters like the grid dimensions, starting position, and maximum number of moves. It uses functions to read in the parameters, move the robot randomly, check if all spaces were visited, print the grid with visit counts, and print the maximum visit count. The design explains the functions to be used and provides a time estimate to complete the program.

Uploaded by

api-709999921
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Program 2

The document describes a program that simulates a robot moving randomly within a rectangular grid. The program takes in parameters like the grid dimensions, starting position, and maximum number of moves. It uses functions to read in the parameters, move the robot randomly, check if all spaces were visited, print the grid with visit counts, and print the maximum visit count. The design explains the functions to be used and provides a time estimate to complete the program.

Uploaded by

api-709999921
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CS 121

Erik Cooley
2.5.2023
Program 2

Introduction:
A robot is malfunctioning and stuck in a large rectangular enclosure. This robot can only
move around at random. According to the laws of probability, this robot will eventually visit
each cell. At each position in the rectangle, the robot has eight moves it could possibly make—(i
– 1, j), (i + 1, j + 1), etc. However, the robot will only be given k moves to make its complete
tour of the rectangle.
This program will run the robot through its process, either until it visits each cell or k
number of moves have been made, whichever comes first. At the end of the robot’s journey, a
grid of numbers will be output showing the number of times each position in the rectangle was
occupied by the robot, as well as a line stating how many moves were made overall and a line
indicating the square(s) that were occupied the maximum number of times by the robot.

Design:
Functions:
First, the specifications for the robot’s journey must be read and input into a data
structure. These values are the number of columns and rows in the grid, the x and y for the
starting position of the robot, and the max number of moves. Thus, a function readIn() will be
used which takes all these values as parameters, dynamically creates a 2D array according to the
dimensions passed, and initializes the array to zero. An if-statement will handle checking row
and column parameters for values above 50. This function will then return a struct containing all
the values passed in as parameters as well as a pointer to the array. (This will require a struct
type, which I will name info, that can hold the five integer variables nCols, nRows, x, y, k, and a
pointer to the 2D array.)
Second, a function will be needed to move the robot around the coordinate grid. This
function (void move()) will take the info struct generated by readIn() as a parameter. First,
move() will determine a valid random move using the rand() function and an array storing a list
of the eight random moves (“valid” meaning within the read-in grid dimensions), update the
position of the robot, increment the contents of the 2D array at those coordinates, and keep track
of the number of moves. This function will also be responsible for terminating the robot’s
movement, either when every space in the grid has been visited or when the maximum number
of moves has been reached.
Additionally, a separate auxiliary function (bool hasVisitedAll()) will be created to
perform the task of determining if every square has been visited. This function must take the info
struct as a parameter. This function will only be run within the move() function as a condition to
the while loop that controls the function.
Third, a function (void printMax()) will be needed to print the maximum square value of
the grid after the move() function is finished running. This function will take the info struct as a
parameter and will access the nCols, nRows, and grid pointer elements. First, a nested for loop
will be used to determine the maximum value in the grid. Then, another nested for loop will
search through the grid to find each instance of that maximum value and print that instance.
Fourth, a function (void printGrid()) will be needed to print the whole grid. It will need
the information about the grid, so it will take an info struct as a parameter. Since the move()
function takes care of incrementing values in the 2D array, all this function needs is a nested loop
to print each value.
Finally, I’ll write a void function named botInRoom() that simply runs all these functions
in the correct sequence given the number of rows and columns, the x and y of the robot’s starting
point, and a maximum number of turns as parameters. This way a single line can be used to
create new situations for the robot. This function should also deal with formatting issues between
printed grids.
Time estimate:
I estimate it will take me approximately five to six hours to complete this program: one
hour for the readIn() function and figuring out the correct data structures, three hours for the
code in the functions, and one to two hours for debugging and running test cases.

Programming Log:
• 2.6.2023:
Two hours spent building basic capabilities of functions to readIn(), move(), and printGrid().
This included creation of array. Turned out it was easier to use array pointer that was technically
one-dimensional and navigate using a more complex statement.
• 2.8.2023, afternoon:
One hour spent debugging, etc. Changed grid pointer to a double pointer which points to more
pointers, which seemed better. Previous method (single grid pointer and complex navigational
statement ( *(grid + y * nCols + x) )) was returning a malloc error when the program was run
that I couldn’t resolve and which only appeared some of the time. This method returns no errors
and is easier to work with. Implemented info struct and ran test cases. Implemented
hasVisitedAll() function. Decided to use nRows = min(nRows, 50) instead of an if-statement for
the sake of cleanliness. Also fixed problem involving random move generation. I had x and y
starting at 1 and the array indexes at zero, so a minor change in readIn() (Info.x = x-1) fixed that.
• 2.8.2023, evening:
Half an hour spent building printMax() function. Also implemented final botInRoom() function,
which took maybe three minutes but ties everything together nicely.

Output:
----------------------------------------------------------------------
Number of moves made: 250
Max. value of 8 can be found at coordinates: (9, 2)
3 3 4 3 1 0 1 2 2 0
4 3 1 0 1 0 2 5 8 2
1 1 1 0 0 1 1 3 4 3
2 1 0 0 0 1 2 1 2 2
5 4 1 0 0 3 0 0 0 0
3 6 2 3 1 1 1 1 0 0
1 5 6 7 2 2 1 0 1 1
2 5 3 5 2 2 3 1 3 0
2 5 1 2 0 1 3 3 3 2
0 1 2 0 1 2 5 2 2 4
0 0 0 1 0 0 2 5 3 2
0 0 0 0 0 0 2 2 4 2
0 0 0 0 0 0 0 4 2 5
0 0 0 0 0 0 2 2 5 4
0 0 0 0 0 1 1 2 3 2
----------------------------------------------------------------------
---------------------------------------------------------------------
-
Number of moves made: 1348
Max. value of 20 can be found at coordinates: (3, 8) (7, 8) (11, 9) (12, 4)
(14, 2)
2 1 3 4 6 9 7 6 8 11 16 9 4 4 8
3 3 1 7 4 7 12 11 14 19 13 15 16 20 11
1 4 4 2 3 10 8 8 10 4 11 16 14 13 10
2 3 3 12 8 6 7 8 4 8 5 20 16 8 10
3 4 5 7 8 11 12 9 5 5 12 10 12 13 1
7 7 13 12 8 11 15 7 5 7 11 8 7 6 5
11 11 8 12 7 17 13 14 12 12 12 7 2 5 4
13 17 20 11 8 11 20 15 15 15 9 7 6 2 7
14 13 8 8 9 12 14 16 12 17 20 9 9 11 6
9 4 3 3 2 5 12 10 11 10 13 7 11 8 2
----------------------------------------------------------------------
----------------------------------------------------------------------
Number of moves made:
2500
Max. value of 17 can be found at coordinates: (18, 27)
3 5 4 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 2 2 2 1 0 1
2 0 4 2 6 6 3 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
7 10 6 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 3 1 0 0 2 1
3 6 1 7 3 5 1 1 0 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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 3 2 2 2 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
----------------------------------------------------------------------
---------------------------------------------------------------------
-
Number of moves made: 200
Max. value of 8 can be found at coordinates: (8, 7) (9, 3) (10, 2)
0 0 1 2 1 2 2 1 4 2 0 0 2
1
0 1 0 3 2 1 2 5 2 8 2 3 2 4
0 0 0 0 2 3 2 3 8 2 1 1 2 1
0 0 0 0 0 0 1 2 2 1 0 0 0 0
0 0 0 0 0 0 3 4 3 2 0 0 0 0
0 0 0 0 2 3 3 5 5 3 1 0 0 0
0 0 0 0 2 3 6 8 5 6 1 0 0 0
0 0 0 0 0 4 2 2 4 3 2 1 0 0
0 0 0 0 0 0 1 2 2 1 1 1 1 0
0 0 0 0 0 1 1 2 3 2 1 0 0 0
0 0 0 0 0 2 3 1 2 2 1 0 0 0
0 0 0 0 0 2 1 1 0 0 0 0 0 0
----------------------------------------------------------------------

Source Code:
program2.cpp:
/*
Program 2

A program that simulates a robot making random moves around a coordinate


grid.
A dynamically allocated 2D array is used to simulate grid.
*/

#include <iostream>
#include <ctime>

using namespace std;

/*------main information structure: initialized in readIn() and passed to


subsequent functions------*/

struct info {

int nRows;

int nCols;

int ** grid;

int x;

int y;

int k;

};

info readIn(int, int, int, int, int);

bool hasVisitedAll(info);

void printMax(info);

void printGrid(info);

void botInRoom(int, int, int, int, int);

int main() {

srand(time(NULL));
/*-------prescribed test cases-------*/

botInRoom(15, 10, 4, 7, 250);

botInRoom(10, 15, 1, 1, 2500);

botInRoom(55, 50, 1, 1, 2500);

/*------my small test case------*/

botInRoom(12, 14, 2, 2, 200);

return 0;

/*------readIn(): dynamically allocates grid and initializes info structure--


----*/

info readIn(int nRows, int nCols, int x, int y, int k) {

int ** gridArr;

gridArr = new int*[nCols];

for (int i = 0; i < nCols; i++) {

gridArr[i] = new int[nRows];

nRows = min(nRows, 50);

nCols = min(nCols, 50);

info Info;

Info.nRows = nRows;

Info.nCols = nCols;

Info.grid = gridArr;

Info.x = x-1;

Info.y = y-1;
Info.k = k;

return Info;

/*------hasVisitedAll(): to check if each square has a value of at least one-


-----*/

bool hasVisitedAll(info Info) {

int ** grid = Info.grid;

int nCols = Info.nCols;

int nRows = Info.nRows;

int numS = nRows * nCols;

int numV = 0;

for (int i = 0; i < nCols; i++) {

for (int j = 0; j < nRows; j++) {

if(grid[i][j] > 0) {

numV++;

if (numV == numS)

return true;

else

return false;

/*-------move(): runs random moves and checks for stops------*/

void move(info Info) {

int nCols = Info.nCols;

int nRows = Info.nRows;


int ** grid = Info.grid;

int x = Info.x;

int y = Info.y;

int k = Info.k;

int nummoves = 0;

grid[x][y] += 1;

while(!hasVisitedAll(Info) && nummoves < k){

int moves[8][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1},
{1, 0}, {1, -1}, {0, -1}};

/*---do-while loop checks for valid random move---*/

int i;

do {

i = rand() % 8;

} while ((x + moves[i][0]) >= nCols || (x + moves[i][0]) < 0 ||


(y + moves[i][1]) >= nRows || (y + moves[i][1]) < 0);

x = x + moves[i][0];

y = y + moves[i][1];

grid[x][y] += 1;

nummoves++;

cout << “Number of moves made: “ << nummoves << endl;

/*-------printMax(): finds max value stored in grid and prints all


coordinates holding that value-------*/

void printMax(info Info) {


int ** grid = Info.grid;

int nCols = Info.nCols;

int nRows = Info.nRows;

int max = grid[0][0];

for (int i = 0; i < nCols; i++) {

for (int j = 0; j < nRows; j++) {

if (grid[i][j] > max) {

max = grid[i][j];

cout << “Max. value of “ << max << “ can be found at coordinates: “;

for (int i = 0; i < nCols; i++) {

for (int j = 0; j < nRows; j++) {

if (grid[i][j] == max) {

cout << “(“ << i+1 << “, “ << j+1 << “) “;

cout << endl;

/*--------printGrid(): iterates through grid and prints it-------*/

void printGrid(info Info) {

int nRows = Info.nRows;

int nCols = Info.nCols;

int ** grid = Info.grid;


for (int i = 0; i < nRows; i++) {

for (int j = 0; j < nCols; j++) {

cout << grid[j][i] << “ “;

if (grid[j][i] < 10) {

cout << “ “;

cout << endl;

/*--------botInRoom(): prints borders between grids and runs all grid


functions-------*/

void botInRoom(int nRows, int nCols, int x, int y, int k) {

cout << “--------------------------------------------------------------


--------“ << endl;

info grid = readIn(nRows, nCols, x, y, k);

move(grid);

printMax(grid);

printGrid(grid);

cout << “--------------------------------------------------------------


--------“ << endl;

You might also like