Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Microsoft Word - 211_Assignment_2.docx Assignment #2 Due October 18th 60 points You can write your programs in the environment of your preference (e.g. Visual Studio, or Unix). Turn in your source...

1 answer below »
Microsoft Word - 211_Assignment_2.docx
Assignment #2
Due October 18th
60 points
You can write your programs in the environment of your preference (e.g. Visual Studio,
or Unix). Turn in your source code files via Blackboard by submitting a single zip file
LASTNAME_assignment_2.zip. This zip file should contain a folder containing the
source code for each problem. The zip file should also contain a
LASTNAME_README.txt file that describes how to run each program as well as what
doesn't work.
1) System Crash – 10 pts
The following C++ code has a problem where the loop can exceed the size of the a
ay if
the user inputs too many numbers.
#include
using namespace std;

int main()
{
int nums[20] = { 0 };
int a[10] = { 0 };

cout
a
endl;
cout
nums
endl;

cout
"How many numbers? (max of 10)"
endl;
cin
nums[0];
for (int i = 0; i < nums[0]; i++)
{
cout
"Enter number "
i
endl;
cin
a[i];
}

Output the numbers entered
for (int i = 0; i < 10; i++)
cout
a[i]
endl;
return 0;
}

If this program is run and we enter 255 for how many numbers, and 9 for every single
number, then the program does something like this:
How many numbers? (max of 10)
255
Enter number 0
9
Enter number 1
9
Enter number 2
9
Enter number 3
9
Enter number 4
9
Enter number 5
9
Enter number 6
9
Enter number 7
9
Enter number 8
9
Enter number 9
9
Enter number 10
9
Enter number 11
9
Enter number 12
9
9
9
9
9
9
9
9
9
9
(Program may crash at this point, or possibly just exit)
Your results may vary, depending on what compiler, operating system, and CPU that you
are using. If the program does not crash on your compiler, try it on transformer or the
Windows machines in the lab and you should get the behavior shown above.
Why didn't the program loop 255 times? Your answer should be detailed and reference
the way in which memory is organized and what is put into memory.

2) Maze Solving – 20 pts
Start with the computer maze-solving program covered in class. The source code is
posted along with the homework.
1. Change the dimensions of the maze to 20x20 with walls around the borders. Out
of the empty space in the middle, randomly fill 25% of them with walls. Then pick
a random start location (that is empty) and a random Exit location (that is empty).
Since you are using random numbers, you should get a different maze with a
different start and end. There is a small chance that it is impossible to get from the
start to the exit, but don't wo
y about that possibility (when the program runs it
should just not print any solution).
2. The algorithm we covered in class prints out the path from the start to the exit in
everse order. Modify the program so the path is output in forward order. One
way to do this is to make an a
ay (or a
ays) that store the (x,y) coordinates of the
cu
ent position as the recursive function exits. With all of the coordinates in an
a
ay, inside the main function you can now loop through the a
ay in the proper
order to output the moves from start to exit.
3. Your program should output a solution to the randomly generated maze, if one
exists.
3) Recursive Maze Generation – 20 pts

Implement the following recursive algorithm to randomly generate a maze. In the
example below I have made the maze 10x10 to save space but your maze should be
40x40. Start with a 2D a
ay of char like we used previously and assign walls to the
orders:
XXXXXXXXXX
0 X X X X X X X X X X
1 X XXXXXXXXXXX
2 X XXXXXXXXXXX
3 X XXXXXXXXXXX
4 X XXXXXXXXXXX
5 X XXXXXXXXXXX
6 X XXXXXXXXXXX
7 X XXXXXXXXXXX
8 X XXXXXXXXXXX
9 X X X X X X X X X X

Call the recursive function with parameters that specify the blank area in the middle,
in this case, highlighted by yellow below. You could do this a couple of ways, for
example, pass in the coordinates of the upper left and lower right corner, or pass in
the coordinate of the upper left corner plus the width and height of the area.
XXXXXXXXXX
0 X X X X X X X X X X
1 X XXXXXXXXXXX
2 X XXXXXXXXXXX
3 X XXXXXXXXXXX
4 X XXXXXXXXXXX
5 X XXXXXXXXXXX
6 X XXXXXXXXXXX
7 X XXXXXXXXXXX
8 X XXXXXXXXXXX
9 X X X X X X X X X X

If the width of the yellow area is <=2 cells or the height of the yellow area is <=2 cells
then the function should return, doing nothing. This is the base case that terminates
ecursion.
If the height of the yellow area is greater than or equal to the width of the yellow
area, then the yellow area is either square or taller than it is wide. In this case, the
idea is to draw a horizontal wall somewhere in the green area:
XXXXXXXXXX
0 X X X X X X X X X X
1 X XXXXXXXXXXX
2 X XXXXXXXXXXX
3 X XXXXXXXXXXX
4 X XXXXXXXXXXX
5 X XXXXXXXXXXX
6 X XXXXXXXXXXX
7 X XXXXXXXXXXX
8 X XXXXXXXXXXX
9 X X X X X X X X X X

By avoiding the top and bottom row of the yellow area, we prevent ourselves from
drawing a wall that might box ourselves in by covering up a passageway.
In the green area, randomly select a vertical position (in this case from y=2 to y=7)
and draw a horizontal wall all the way across the green. In this example, I randomly
selected y=3:
XXXXXXXXXX
0 X X X X X X X X X X
1 X XXXXXXXXXXX
2 X XXXXXXXXXXX
3 X X X X X X X X X X
4 X XXXXXXXXXXX
5 X XXXXXXXXXXX
6 X XXXXXXXXXXX
7 X XXXXXXXXXXX
8 X XXXXXXXXXXX
9 X X X X X X X X X X
Next, check if the ends of the line we just drew are next to a blank. If so, make the
end wall a blank. This is to allow passage through the blank instead of blocking it. In
this example, check if (0,3) is a blank. If it is, then make (1,3) a blank. Also check if
(9,3) is a blank. If it is then make (8,3) a blank. There is no blank to add in this case.
Next, randomly select one of the cells in the wall that was added and turn it into a
lank. This adds a hole to allow passage between the two halves of the maze
separated by the new wall. In this example, I randomly
Answered Same Day Oct 19, 2021

Solution

Aditi answered on Oct 20 2021
144 Votes
problem3.cpp
#include
#include
#include#includeusing namespace std;
const int WIDTH = 40;
const int HEIGHT = 40;
void initialize(char maze[][WIDTH]);
void printMaze(char maze[][WIDTH]);
void mazeGenarator (char maze[][WIDTH], int x1, int y1, int x2, int y2);
void initialize(char maze[HEIGHT][WIDTH])
{
    for(int i=0; i        maze[0][i] = 'X';
        maze[HEIGHT-1][i] = 'X';
    }
    for(int i=1; i        maze[i][0] = 'X';
        for(int j=1; j            maze[i][j] = ' ';
        }
        maze[i][WIDTH-1] = 'X';
    }
}
void printMaze(char maze[][WIDTH])
{
    /*cout
setw(3)
" ";
    for(int i=0; i < WIDTH; i++){
        cout
setw(3)
i;
    }*
    cout
endl;
for (int i = 0; i < HEIGHT; i++)
    {
        
cout
setw(3)
i;
        for (int j = 0; j < WIDTH; j++)
        {
...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here