Thread: Using 2 sets of arrays for a random walk 2D program

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    2

    Question Using 2 sets of arrays for a random walk 2D program

    I'm a beginner in C. I'm writing a program to demonstrate a random walk in 2D (that is, a point up/down/left/right, y+1, y-1, x-1, x+1 ). I have written a program that randomly generates numbers of up/down/left/right for me, however as I get to the distance travelled calculation part (distance = r = sqrt(x*x+y*y) ), I realise I better off do this with arrays.

    I looked up some books and made a start by setting
    Code:
    int dx[]= { 0, 0, -1, 1 } ;
    int dy[]= { -1, 1, 0, 0 } ;
    
    
    
    but I am struggling on putting them together, and it is hard to find examples online on that.

    Any help or suggestion is appreciated.Attachment 11040

    Code:
    include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int up;
    int down;
    int left;
    int right;
    
    
    int x = 0;
    int y = 0;
    int dx[]= { 0, 0, -1, 1 } ;
    int dy[]= { -1, 1, 0, 0 } ;
    int r;
    
    
    int flip( void );
    int main( void )
    {
    int toss;
    
    srand(time(NULL));
    
    FILE *out;
    out=fopen("data.txt", "w");
    for ( toss = 1; toss <= 200; toss++ ){
        printf( "%d\n", flip( ));
        fprintf(out,"%d\n",flip( ));
    
    
        if (flip( ) == 0)
        up++;
        else if (flip( ) == 1)
        down++;
        else if (flip () == 2)
        left++;
        else
        right++;
    
    
    }
    fclose(out);
    
    
    printf( "Up had been flipped %d times\n", up );
    printf( "Down had been flipped %d times\n", down );
    printf( "Left had been flipped %d times\n", left );
    printf( "Right had been flipped %d times\n", right );
    
    
    return 0;
    
    }
    int flip( )
    {
        int i = rand() % 4;
    
    
    if (i == 0)
       return 0;
    
    else if (i == 1)
    return 1;
    
    else if (i == 2)
    return 2;
    
    else
    return 3;
    
    }
    Attached Images Attached Images Using 2 sets of arrays for a random walk 2D program-jpg-jp-jpg 

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
        printf( "%d\n", flip( ));
        fprintf(out,"%d\n",flip( ));
        if (flip( ) == 0)
    How is flip() supposed to know you want the same answer as last time, or another answer?

    Code:
        f = flip();
        printf( "%d\n", f);
        fprintf(out,"%d\n",f);
        if (f == 0)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 10-30-2011, 10:23 PM
  2. Program that generates a "random walk" across 10*10 array
    By danieldcc in forum C Programming
    Replies: 37
    Last Post: 07-23-2011, 01:19 AM
  3. Random walk solution
    By skiabox in forum C Programming
    Replies: 5
    Last Post: 11-01-2010, 10:36 AM
  4. Code for random walk in 1D
    By LLcoolc++ in forum C Programming
    Replies: 5
    Last Post: 10-18-2009, 09:33 AM
  5. Help with a random walk
    By pxleyes in forum C Programming
    Replies: 11
    Last Post: 02-27-2004, 09:11 PM

Tags for this Thread