Hello all. Sorry to be a bother but I cannot figure out what is wrong with my homework program that's due before midnight. The assignment is to create a program with a two-dimensional array that generates a random walking path. I'll list the assignment as it is probably clearer.

Write a program that generates a "random walk" across a 10 x 10 array. The array will contain characters (all '.' initially). The program must randomly "walk" from element to element, always going up, down, left, or right by one element. The elements visited by the program will be labeled with the letters A through Z, in the order visited . . .
. . . If there is no legal move possible, then the program must terminate. Otherwise, continue (iterate) the following until a legal move is generated: generate a random number and find its remainder when divided by four (as described in the problem description), and check if that remainder specifies a legal move.
When a legal move is generated, make the move. . . To make the instructions more concrete (which is needed to make two programs produce the same output),use the following correspondence between the numbers 0, 1, 2, 3 and the legal moves . . . 0 corresponds to up, 1 to left, 2 to down and 3 to right

The printout of the output is supposed to look like this when using srand(5000) (only difference is that the program prints out this for every move before it):

[IMG]http://img128.imageshack.us/img128/3511/screenshotvz3.jpg[/IMG]

My result is off and I cannot figure out why. The entire code is below, please do not quote it because I will delete it after I fix the problem (they may accuse me of plagiarism or of helping others cheat, etc.). And thank you in advance for any help you may give.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void printarray(char array[10][10], int x, int y); /*Function prototype*/

main()
{
    int i = 0, roll = 0, row = 0, col = 0, rowcheck = 0, colcheck = 0; /*i = number of moves. roll = value generated by the random number generator. r = row location. col = column location. rowcheck and colcheck are the incremented and decremented values of row and col respectively, used to check whether a legal move can be performed*/
    char position[10][10], chtr = 'A'; /*position[10][10] = Grid for the walking. chtr = the letters of the Alphabet.*/

    for (row = 0; row < 10; row++) /*Fills the 10 x 10 array with the "." character*/
    {
        for (col = 0; col < 10; col++)
        {
            position[row][col] = '.';
        }
    }

    srand(5000); /*Seeds the random number function*/

    for (i = 0, row = 0, col = 0, rowcheck = 0, colcheck = 0; i < 25;)
    {
        rowcheck = row;
        colcheck = col;
        roll = rand() % 4;
        switch (roll) /*Determines what direction to move starting from the top left corner of the grid (bird's eye view)*/
        {
            case 0: /*Move up*/
            {
                rowcheck--;
            }
            case 1: /*Move left*/
            {
                colcheck--;
            }
            case 2: /*Move down*/
            {
                rowcheck++;
            }
            case 3: /*Move right*/
            {
                colcheck++;
            }
        }
        if ((rowcheck < 0 || rowcheck > 9) || (colcheck < 0 || colcheck > 9) || (position[rowcheck][colcheck] != '.'))
        {
            continue;
        }
        else
        {
            row = rowcheck;
            col = colcheck;
            position[row][col] = chtr;
            chtr++;
            printarray(position, row, col);
            i++;
        }
    }

    exit (0);
}

/*Function declaration*/
void printarray(char array[10][10], int x, int y)
{
    printf("CURRENT POSITION %d %d\n", x, y);
    for (x = 0; x < 10; x++) /*Prints out the values of the array*/
        {
            for (y = 0; y < 10; y++)
                {
                    printf("%c", array[x][y]);
                }
                printf("\n");
        }
    printf("\n");
}

Recommended Answers

All 7 Replies

Methinks your switch cases need breaks. And you may want to deal with painting yourself in a corner.

Wow, I can't believe I don't have any breaks in the switch, thanks.

What do you mean by "painting myself in a corner" exactly? Something such as the instance where the program moves into the edge of the grid and execute a legal move?

Aye.

I've been working on adding a check for the program to terminate for the past hour but I can't come up with anything that works.
I've tried adding an if statement to the beginning of the loop that checks for legal moves (looks to see if the value above, below, left of, and right of are ALL not equal to the . character), but that ended up doing all kinds of weird things. I've tried adding an if statements within the switch but still all kinds of weird output.

Could someone hint as to what direction I should take or confirm if either of the methods I tried should work (and that I simply made some kind of error).

Edit: I know now that the program is actually working since my output matches that of the solution, the only problem is that mine does not terminate since there is not a check for when to end the loop (only if all 25 moves are carried out)

Maybe instead of just continuing, figure out why you're always continuing. If it's because you have nowhere to go, perhaps give up trying.

do you go to uc davis?
im writing the same program

do you go to uc davis?
im writing the same program

Yes, and I'm not going to finish on time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.