// This exercise is to print an initialized 2D grid of positive // integer value
ID: 3913678 • Letter: #
Question
// This exercise is to print an initialized 2D grid of positive // integer values from a specified position. // You should prompt the user for a row and column value // in the grid. From this location, you need to print // each element value in north, west, south, and east. //
Such that ask the user for a position on the grid and then it prints the elements north of it, then west, then south and lastly east.For example, let's assume that the user inputs grid[2][3]. In this way, the elements that must be be printed are: 23 13 4 3 2 1 10 20 30 40 50 51 41 ... you get the idea( look at the 2D array down below for reference. Most importantly, the elements should not be repeated once they are printed. The part that we would be working on is the function spiralShowGrid: #include <iostream> #include <fstream> #include <iomanip> using namespace std; // Global constants const int COLS = 4; // Number of columns in each array const int ROWS = 6; // Number of rows in each array void showGrid(int[][COLS], int); bool finished(int [][COLS]); // utility function to tell whether // still work to do for the spiral printing void spiralShowGrid(int [][COLS], int, int); // Function prototype int main() { int grid[6][4] = {{ 1, 2, 3, 4}, {10, 11, 12, 13}, {20, 21, 22, 23}, {30, 31, 32, 33}, {40, 41, 42, 43}, {50, 51, 52, 53}}; showGrid(grid, ROWS); cout << "Type in the current position in the grid: " << flush; int row, col; cin >> row >> col; cout << "Your current position: " << "grid[" << row << "]" << "[" << col << "]" << endl; cout << " The spiral printing contents of initialized grid are: "; spiralShowGrid(grid, row, col); return 0; } void showGrid(int g[][COLS], int r) { cout << fixed << setw(4); for (int i = 0; i < r; ++i) { for (int j = 0; j < COLS; j++) { cout << fixed << setw(4) << g[i][j]; } cout << endl; } }; bool finished(int s[][COLS]) { for (int i = 0; i < ROWS; i++) for (int j = 0; j < COLS; j++) if (s[i][j] == 0) return false; return true; } void spiralShowGrid(int g[][COLS], int r, int c) { cout << "YOU NEED TO IMPLEMENT spiralShowGrid "; int states[ROWS][COLS] = {0}; // initialize every element to 0 int steps = 1; states[r][c] = steps; do { // need to implement this part } while (finished(states)); }
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
// Global constants
const int COLS = 4; // Number of columns in each array
const int ROWS = 6; // Number of rows in each array
void showGrid(int[][COLS], int);
bool finished(int [][COLS]); // utility function to tell whether
// still work to do for the spiral printing
void spiralShowGrid(int [][COLS], int, int); // Function prototype
int main()
{
int grid[6][4] = {{ 1, 2, 3, 4},
{10, 11, 12, 13},
{20, 21, 22, 23},
{30, 31, 32, 33},
{40, 41, 42, 43},
{50, 51, 52, 53}};
showGrid(grid, ROWS);
cout << "Type in the current position in the grid: " << flush;
int row, col;
cin >> row >> col;
cout << "Your current position: "
<< "grid[" << row << "]"
<< "[" << col << "]" << endl;
cout << " The spiral printing contents of initialized grid are: ";
spiralShowGrid(grid, row, col);
return 0;
}
void showGrid(int g[][COLS], int r) {
cout << fixed << setw(4);
for (int i = 0; i < r; ++i) {
for (int j = 0; j < COLS; j++) {
cout << fixed << setw(4) << g[i][j];
}
cout << endl;
}
};
bool finished(int s[][COLS]) {
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLS; j++)
if (s[i][j] == 0)
return false;
return true;
}
void spiralShowGrid(int g[][COLS], int r, int c) {
cout << "YOU NEED TO IMPLEMENT spiralShowGrid ";
int states[ROWS][COLS] = {0}; // initialize every element to 0
int steps = 1;
states[r][c] = steps;
int grid[ROWS][COLS] ={0};//to print grid in spiral fashion from index r,c
grid[r][c] = g[r][c];
showGrid(grid,ROWS);
do {
// need to implement this part
//finding north element.....
int n=r,k=c;
int m,l;
while(n>=0 || k<COLS)
{
if(states[n][k]!=1)
break;
if(n-1>=0)
n--;
if(k+1<COLS)
k++;
}
m=n,l=k;
//printing north elements
while(k!=c)
{
states[n][k]=1;
grid[n][k]=g[n][k];
cout<<" ";
showGrid(grid,ROWS);//printing north element
k--;
}
states[n][c]=1;
grid[n][c]=g[n][c];
cout<<" ";
showGrid(grid,ROWS);//printing north element
//moving towards north west....
int nc=c;
while(nc>=0)
{
if(nc-1>=0)
{
nc=nc-1;
states[n][nc]=1;
grid[n][nc]=g[n][nc];
cout<<" ";
showGrid(grid,ROWS);//printing north west element
}
else
break;
if(states[n+1][nc]==0)break;
}
//moving towards south west...
while(n<ROWS)
{
if(n+1<ROWS)
{
n=n+1;
states[n][nc]=1;
grid[n][nc]=g[n][nc];
cout<<" ";
showGrid(grid,ROWS);//printing north west element
}
else break;
if(states[n][nc+1]==0)
break;
}
//printing south elements
while(nc<COLS)
{
if(nc+1<COLS)
{
nc=nc+1;
states[n][nc]=1;
grid[n][nc]=g[n][nc];
cout<<" ";
showGrid(grid,ROWS);//printing south element
}
else
break;
if(states[n-1][nc]==0)break;
}
//printing east elements
while(n>=0)
{
if(n-1>=0)
{
n=n-1;
states[n][nc]=1;
grid[n][nc]=g[n][nc];
cout<<" ";
showGrid(grid,ROWS);//printing east element
}
else break;
if(n==m)
break;
}
} while (!finished(states));
}
output:
1 2 3 4
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
Type in the current position in the grid: 3 2
Your current position: grid[3][2]
The spiral printing contents of initialized grid are:
YOU NEED TO IMPLEMENT spiralShowGrid
0 0 0 0
0 0 0 0
0 0 0 0
0 0 32 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 23
0 0 32 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 22 23
0 0 32 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 21 22 23
0 0 32 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 21 22 23
0 31 32 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 21 22 23
0 31 32 0
0 41 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 21 22 23
0 31 32 0
0 41 42 0
0 0 0 0
0 0 0 0
0 0 0 0
0 21 22 23
0 31 32 0
0 41 42 43
0 0 0 0
0 0 0 0
0 0 0 0
0 21 22 23
0 31 32 33
0 41 42 43
0 0 0 0
0 0 0 0
0 0 0 0
0 21 22 23
0 31 32 33
0 41 42 43
0 0 0 0
0 0 0 0
0 0 0 13
0 21 22 23
0 31 32 33
0 41 42 43
0 0 0 0
0 0 0 0
0 0 12 13
0 21 22 23
0 31 32 33
0 41 42 43
0 0 0 0
0 0 0 0
0 11 12 13
0 21 22 23
0 31 32 33
0 41 42 43
0 0 0 0
0 0 0 0
10 11 12 13
0 21 22 23
0 31 32 33
0 41 42 43
0 0 0 0
0 0 0 0
10 11 12 13
20 21 22 23
0 31 32 33
0 41 42 43
0 0 0 0
0 0 0 0
10 11 12 13
20 21 22 23
30 31 32 33
0 41 42 43
0 0 0 0
0 0 0 0
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
0 0 0 0
0 0 0 0
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 0 0 0
0 0 0 0
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 0 0
0 0 0 0
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 0
0 0 0 0
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
0 0 0 0
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
0 0 0 0
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
0 0 0 0
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
0 0 0 0
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
0 0 0 4
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
0 0 3 4
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
0 2 3 4
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
1 2 3 4
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
1 2 3 4
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
1 2 3 4
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
1 2 3 4
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
1 2 3 4
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
1 2 3 4
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
1 2 3 4
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
1 2 3 4
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
1 2 3 4
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
1 2 3 4
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
1 2 3 4
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
1 2 3 4
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
1 2 3 4
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
1 2 3 4
10 11 12 13
20 21 22 23
30 31 32 33
40 41 42 43
50 51 52 53
Process exited normally.
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.