Please help me find where I do something wrong with my progr

Please help me find where I do something wrong with my program

I am working on creating a random walk across a grid. I already wrote most of the code, but every time I run my program, the output is only \'.\'

this is the requirement :

- using #include <iostream> , #include <cstdlib>, #include <ctime>

- const int SIZE = 10;

- typedef char Grid[SIZE][SIZE]

- The grid must be set to have all its elements be the character ’.’ before the walk is begun.

The program must contain the following function prototypes, the main function, and the function definitions for all the other functions:

****Function prototypes

bool can_move_up(int i, int j, Grid walk);

bool can_move_down(int i, int j, Grid walk);

bool can_move_left(int i, int j, Grid walk);

bool can_move_right(int i, int j, Grid walk);

void init_array(Grid walk);

void generate_random_walk(Grid walk);

void print_array(Grid walk);

int main(void)

{

Grid walk; // the grid in which the random walk occurs

srand((unsigned) time(NULL));

init_array(walk);

generate_random_walk(walk);

print_array(walk);

return 0;

}

This is what I got so far:

#include <iostream>
#include <cstdlib> // for srand and rand
#include <ctime> // for time

using namespace std;

const int SIZE = 10;

typedef char Grid[SIZE][SIZE];

// Function declaration

bool can_move_up(int i, int j, Grid walk);

bool can_move_down(int i, int j, Grid walk);

bool can_move_left(int i, int j, Grid walk);

bool can_move_right(int i, int j, Grid walk);

void init_array(Grid walk);

void generate_random_walk(Grid walk);

void print_array(Grid walk);

int main(void)
{

Grid walk; // the grid in which the random walk occurs

srand((unsigned) time(NULL));

init_array(walk); //calling init_array function

generate_random_walk(walk); //calling generate_random_walk function

print_array(walk); //calling print_array function

return 0;
}


void init_array(Grid walk) // function init_array
{
  
   for(int i = 0; i < SIZE; i++)
   {
       for(int j = 0; j < SIZE; j++)
       {
           walk[i][j] = \'.\'; //filling array with \'.\'
       }
   }
  
}


bool can_move_up(int i, int j, Grid walk)
{  
   if((i < 0) && (walk[i - 1][j] == \'.\') )
       return true;
  
   else
       return false;
  
}


bool can_move_down(int i, int j, Grid walk)
{
  
   if((i < 9) && (walk[i + 1][j] == \'.\'))
       return true;
  
   else
       return false;
}


bool can_move_left(int i, int j, Grid walk)
{
  
   if((j > 0) && (walk[i][j - 1] == \'.\'))
       return true;
  
   else
       return false;
  
}

bool can_move_right(int i, int j, Grid walk)
{
  
   if ((j > 9) && (walk[i][j + 1] == \'.\'))
       return true;
      
   else
       return false;
  
  
}

void generate_random_walk(Grid walk)
{  
   int i,j;
   int random;
   char letter;
  
   random = rand() % 4;
  
   for (letter = \'A\'; letter <= \'Z\'; letter ++)
   {
          
       switch (random)
       {
           case 0:
               can_move_up(i, j, walk);
               break;
          
           case 1:
               can_move_down(i, j, walk);
               break;
          
           case 2:
               can_move_left(i, j, walk);
               break;
          
           case 3:
               can_move_right( i, j, walk);
               break;
              
           default:
               break;
          
       }  
   }
  
}

void print_array(Grid walk)
{
   for (int i = 0; i < SIZE ; i++)
   {
       for (int j = 0; j < SIZE ; j++)
       {
           cout << walk[i][j];
       }
      
       cout << endl;
   }
}

Solution

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

const int SIZE = 10;

typedef char Grid[SIZE][SIZE];

// Function declaration

bool can_move_up(int i, int j, Grid walk);

bool can_move_down(int i, int j, Grid walk);

bool can_move_left(int i, int j, Grid walk);

bool can_move_right(int i, int j, Grid walk);

void init_array(Grid walk);

void generate_random_walk(Grid walk);

void print_array(Grid walk);

int main(void)
{

Grid walk; // the grid in which the random walk occurs

srand((unsigned) time(NULL));

init_array(walk); //calling init_array function

generate_random_walk(walk); //calling generate_random_walk function

print_array(walk); //calling print_array function

return 0;
}


void init_array(Grid walk) // function init_array
{
  
   for(int i = 0; i < SIZE; i++)
   {
       for(int j = 0; j < SIZE; j++)
       {
           walk[i][j] = \'.\'; //filling array with \'.\'
       }
   }
  
}


bool can_move_up(int i, int j, Grid walk)
{  
   if((i < 0) && (walk[i - 1][j] == \'.\') )
       return true;
  
   else
       return false;
  
}


bool can_move_down(int i, int j, Grid walk)
{
  
   if((i < 9) && (walk[i + 1][j] == \'.\'))
       return true;
  
   else
       return false;
}


bool can_move_left(int i, int j, Grid walk)
{
  
   if((j > 0) && (walk[i][j - 1] == \'.\'))
       return true;
  
   else
       return false;
  
}

bool can_move_right(int i, int j, Grid walk)
{
  
   if ((j > 9) && (walk[i][j + 1] == \'.\'))
       return true;
      
   else
       return false;
  
  
}

void generate_random_walk(Grid walk)
{  
   int i,j;
   int random;
   char letter;
  
   random = rand() % 4;
  
   for (letter = \'A\'; letter <= \'Z\'; letter ++)
   {
          
       switch (random)
       {
           case 0:
               can_move_up(i, j, walk);
               break;
          
           case 1:
               can_move_down(i, j, walk);
               break;
          
           case 2:
               can_move_left(i, j, walk);
               break;
          
           case 3:
               can_move_right( i, j, walk);
               break;
              
           default:
               break;
          
       }  
   }
  
}

void print_array(Grid walk)
{
   for (int i = 0; i < SIZE ; i++)
   {
       for (int j = 0; j < SIZE ; j++)
       {
           cout << walk[i][j];
       }
      
       cout << endl;
   }
}

Please help me find where I do something wrong with my program I am working on creating a random walk across a grid. I already wrote most of the code, but every
Please help me find where I do something wrong with my program I am working on creating a random walk across a grid. I already wrote most of the code, but every
Please help me find where I do something wrong with my program I am working on creating a random walk across a grid. I already wrote most of the code, but every
Please help me find where I do something wrong with my program I am working on creating a random walk across a grid. I already wrote most of the code, but every
Please help me find where I do something wrong with my program I am working on creating a random walk across a grid. I already wrote most of the code, but every
Please help me find where I do something wrong with my program I am working on creating a random walk across a grid. I already wrote most of the code, but every

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site