Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Tutorial: Access a 2-dimensional array using array and pointer operations Assume

ID: 640088 • Letter: T

Question

Tutorial: Access a 2-dimensional array using array and pointer operations

Assume that we have declared a 2-dimensional array

#define maxrow 50

#define maxcolumn 50

char maze[maxrow][maxcolumn];

We want to initialize the array (maze) as follows:

XXXXXXXXXXXXXXXX

X              X

X             X

X             X

X             X

X             X

X             X

X             X

X             X

X             X

X             X

XXXXXXXXXXXXXXXX

The following code uses array operations to initialize the maze:

void initialization(int r, int c) {

       int i, j;

       for (i = 0; i < r; i++){

              maze[i][0] = 'X';     // add left border

              maze[i][c - 1] = 'X'; // add right border

              maze[i][c] = '';    // add string terminator

              for (j = 1; j < c - 1; j++)

              {

                      if ((i == 0) || (i == r - 1))

                             maze[i][j] = 'X'; // add top and bottom borders

                      else

                             maze[i][j] = ' '; // initialize the inner maze with space

              }

       }

}

We can also use pointer operations to initialize the maze. The equivalent code is given as follows:

void initialization(int r, int c) {

       int i, j;

       char *p = 0;

       for (i = 0; i <= r; i++){

              p = &maze[i][0];      // pointing to initial address of the ith row of the maze

              *p = 'X';             // add left border

              *(p + c - 1) = 'X';   // add right border

              *(p + c) = '';      // add string terminator

              for (j = 1; j < c - 1; j++)

              {

                      if ((i == 0) || (i == r - 1))

                             *(p + j) = 'X';       // add top and bottom borders

                      else

                             *(p + j) = ' ';       // initialize the inner maze with space

              }

       }

}Compare the two versions of the initialization code. You will follow this example to complete your programming assignment in the next section.

Programming Assignment (50 points)

Given the following C code, try to understand what it does and make the changes given in the following questions.        [50 points]

#include

#pragma warning(disable: 4996)

// This program exercises the operations of pointers and arrays

#define maxrow 50

#define maxcolumn 50

char maze[maxrow][maxcolumn]; // Define a static array of arrays of characters.

int lastrow = 0;

// Forward Declarations

int triple(int);

void initialization(int, int);

void randommaze(int, int);

void printmaze(int, int);

int triple(int x) {          // % is modulo operator.

       return ((x % 3 == 0) ? 1 : 0);

}

void initialization(int r, int c) {

       int i, j;

       for (i = 0; i < r; i++){

              maze[i][0] = 'X';     // add border

              maze[i][c - 1] = 'X'; // add border

              maze[i][c] = '';    // add string terminator

              for (j = 1; j < c - 1; j++)

              {

                      if ((i == 0) || (i == r - 1))

                             maze[i][j] = 'X'; // add border

                      else

                             maze[i][j] = ' '; // initialize with space

              }

       }

}

// Add 'X' into the maze at random positions

void randommaze(int r, int c) {

       int i, j, d;

       for (i = 1; i < r - 1; i++) {

              for (j = 1; j < c - 2; j++) {

                      d = rand();

                      if (triple(d))

                      {

                             maze[i][j] = 'X';

                      }

              }

       }

       i = rand() % (r - 2) + 1;

       j = rand() % (c - 3) + 1;

       maze[i][j] = 'S'; // define Starting point

       do

       {

              i = rand() % (r - 2) + 1;

              j = rand() % (c - 3) + 1;

       } while (maze[i][j] == 'S');

      

       maze[i][j] = 'G'; // define Goal point

}

// Print the maze

void printmaze(int r, int c) {

       int i, j;

       for (i = 0; i < r; i++) {

              for (j = 0; j < c; j++)

                      printf("%c", maze[i][j]);

              printf(" ");

       }

}

void main() {

       int row, column;

       printf("Please enter two integers, which must be greater than 3 and less than maxrow and maxcolomn, respectively ");

       scanf("%d %d", &row, &column);

       while ((row <= 3) || (column <= 3) || (row >= maxrow) || (column >= maxcolumn)) {

              printf("both integers must be greater than 3. Row must be less than %d, and column less than %d. Please reenter ", maxrow, maxcolumn);

              scanf("%d %d", &row, &column);

       }

       initialization(row, column);

       randommaze(row, column);

       printmaze(row, column);

       //encryptmaze(row, column);

       //printmaze(row, column);

       //decryptmaze(row, column);

       //printmaze(row, column);

}

1.Rewrite int triple(int) the function using macro definition.                                         [5]

2.Rewrite the function randommaze by substituting pointer operations for all array operations. You may not use indexed operation like maze[i][j], except getting the initial value of the pointer.                                                   [10]

3.Rewrite the function printmaze by substituting string operations for all character operations.         [10]

4.Write the function encryptmaze based on the pointer operations. The function will encrypt the maze in the following secrete rules:                                                                                                                                            [15]

a.An integer i will be added to each space character, where i is the row number of the character.

b.An integer j will be added to each non-space character (X, S, and G), where j is the column number of the character. Do not encrypt the terminator character

#include

#pragma warning(disable: 4996)

// This program exercises the operations of pointers and arrays

#define maxrow 50

#define maxcolumn 50

char maze[maxrow][maxcolumn]; // Define a static array of arrays of characters.

int lastrow = 0;

// Forward Declarations

int triple(int);

void initialization(int, int);

void randommaze(int, int);

void printmaze(int, int);

int triple(int x) {          // % is modulo operator.

       return ((x % 3 == 0) ? 1 : 0);

}

void initialization(int r, int c) {

       int i, j;

       for (i = 0; i < r; i++){

              maze[i][0] = 'X';     // add border

              maze[i][c - 1] = 'X'; // add border

              maze[i][c] = '';    // add string terminator

              for (j = 1; j < c - 1; j++)

              {

                      if ((i == 0) || (i == r - 1))

                             maze[i][j] = 'X'; // add border

                      else

                             maze[i][j] = ' '; // initialize with space

              }

       }

}

// Add 'X' into the maze at random positions

void randommaze(int r, int c) {

       int i, j, d;

       for (i = 1; i < r - 1; i++) {

              for (j = 1; j < c - 2; j++) {

                      d = rand();

                      if (triple(d))

                      {

                             maze[i][j] = 'X';

                      }

              }

       }

       i = rand() % (r - 2) + 1;

       j = rand() % (c - 3) + 1;

       maze[i][j] = 'S'; // define Starting point

       do

       {

              i = rand() % (r - 2) + 1;

              j = rand() % (c - 3) + 1;

       } while (maze[i][j] == 'S');

      

       maze[i][j] = 'G'; // define Goal point

}

// Print the maze

void printmaze(int r, int c) {

       int i, j;

       for (i = 0; i < r; i++) {

              for (j = 0; j < c; j++)

                      printf("%c", maze[i][j]);

              printf(" ");

       }

}

void main() {

       int row, column;

       printf("Please enter two integers, which must be greater than 3 and less than maxrow and maxcolomn, respectively ");

       scanf("%d %d", &row, &column);

       while ((row <= 3) || (column <= 3) || (row >= maxrow) || (column >= maxcolumn)) {

              printf("both integers must be greater than 3. Row must be less than %d, and column less than %d. Please reenter ", maxrow, maxcolumn);

              scanf("%d %d", &row, &column);

       }

       initialization(row, column);

       randommaze(row, column);

       printmaze(row, column);

       //encryptmaze(row, column);

       //printmaze(row, column);

       //decryptmaze(row, column);

       //printmaze(row, column);

}

Explanation / Answer

bump: please help!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote