Rewrite the function randommaze by substituting pointer operations for array ope
ID: 3536984 • Letter: R
Question
Rewrite the function randommaze by substituting pointer operations for array operations
// This program exercises the operations on multidimensional array
#define maxrow 100
#define maxcolumn 100
#define even(x) (x%2 == 0) ? 1 : 0
#include <stdio.h>
#include <stdlib.h>
//const int maxrow = 100, maxcolumn = 100;
char maze[maxrow][maxcolumn+1];
int lastrow = 0;
// Forward Declarations
//int even(int);
void initialization(void);
void randommaze (void);
void printmaze(void);
/*int even(int x) { // % is modulo operator.
return ( (x%2 == 0) ? 1 : 0);
}*/
void initialization(void) {
int i, j;
for (i=0; i<maxrow; i++)
for (j = 0; j <= maxcolumn; j++)
maze[i][j] = ' ';
}
void randommaze () {
int i, j, i1, j1, d, row, column;
printf("Please enter the size of the maze: two integers ");
scanf("%d %d" , &row, &column);
while ((row < 1) || (column < 1)) {
printf("both integers must be greater than 0. Please reenter ");
scanf("%d %d",&row ,&column);
}
for (i=lastrow; i < row+lastrow; i++) {
for (j = 0; j < column; j++) {
d = rand();
if (even(d))
maze[i][j] = ' ';
else
maze[i][j] = 'X';
}
maze[i][column] = ''; // add terminator
}
i = rand() % row;
j = rand() % column;
maze[i+lastrow][j] = 'S'; // define Starting point
i1 = rand() % row;
j1 = rand() % column;
while ((i1 == i) && (j1 == j) && ((row > 1) || (column > 1))) {
i1 = rand() % row;
j1 = rand() % column;
}
maze[i1+lastrow][j1] = 'G'; // define Goal point
lastrow = lastrow + row + 1;
for (j = 0; j < column; j++) // print separator
maze[lastrow-1][j]='-'; // add separators
maze[lastrow-1][column]=''; // add terminator
}
void printmaze() {
int i;
for (i=0; i < lastrow; i++) {
printf("%s ", maze[i]); // A 2-dimensional array
} // is an array of array
}
void main() {
initialization();
randommaze();
printmaze();
}
// This program exercises the operations on multidimensional array
#define maxrow 100
#define maxcolumn 100
#define even(x) (x%2 == 0) ? 1 : 0
#include <stdio.h>
#include <stdlib.h>
//const int maxrow = 100, maxcolumn = 100;
char maze[maxrow][maxcolumn+1];
int lastrow = 0;
// Forward Declarations
//int even(int);
void initialization(void);
void randommaze (void);
void printmaze(void);
/*int even(int x) { // % is modulo operator.
return ( (x%2 == 0) ? 1 : 0);
}*/
void initialization(void) {
int i, j;
for (i=0; i<maxrow; i++)
for (j = 0; j <= maxcolumn; j++)
maze[i][j] = ' ';
}
void randommaze () {
int i, j, i1, j1, d, row, column;
printf("Please enter the size of the maze: two integers ");
scanf("%d %d" , &row, &column);
while ((row < 1) || (column < 1)) {
printf("both integers must be greater than 0. Please reenter ");
scanf("%d %d",&row ,&column);
}
for (i=lastrow; i < row+lastrow; i++) {
for (j = 0; j < column; j++) {
d = rand();
if (even(d))
maze[i][j] = ' ';
else
maze[i][j] = 'X';
}
maze[i][column] = ''; // add terminator
}
i = rand() % row;
j = rand() % column;
maze[i+lastrow][j] = 'S'; // define Starting point
i1 = rand() % row;
j1 = rand() % column;
while ((i1 == i) && (j1 == j) && ((row > 1) || (column > 1))) {
i1 = rand() % row;
j1 = rand() % column;
}
maze[i1+lastrow][j1] = 'G'; // define Goal point
lastrow = lastrow + row + 1;
for (j = 0; j < column; j++) // print separator
maze[lastrow-1][j]='-'; // add separators
maze[lastrow-1][column]=''; // add terminator
}
void printmaze() {
int i;
for (i=0; i < lastrow; i++) {
printf("%s ", maze[i]); // A 2-dimensional array
} // is an array of array
}
void main() {
initialization();
randommaze();
printmaze();
}
Explanation / Answer
FOLLOW THIS
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.