Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Program quits after data input? (http://www.go4expert.com/forums/program-quits-data-input-t28555/)

jose_peeterson 9Jun2012 12:22

Program quits after data input?
 
Dear all,
PLEASE READ THE QUESTION AND SEE IF YOU CAN FIGURE OUT WHY MY PROGRAM QUITS!

Suppose you got yourself drunk after a night out and you are about to go home. You
are so intoxicated that for every step you take there is an equal probability that you
either move forward by one step, or move backward by one step. Assuming that you
are at the starting position 0. If you are allowed to take exactly one step, then you
either end at at positions +1 or −1. If you are allowed exactly two steps starting at
position 0, then you either ˆ end up at positions −2: Take two steps back. ˆ 0: One step forward, followed by one step back; or one step backward, followed
by one step forward. ˆ +2. Take two steps forward.
If you are allowed exactly three steps, then you end up at positions −3, −1, 1 or 3.
In general, if you are allowed to take n steps, you would end up at n + 1 different
positions. Moreover, you will always end up in odd positions if n is odd, and even
positions if n is even.
: The getDrunk function simply performs the walk and moves forwards or backwards
without regards to whether it is out of the bounds of the array. At the end of
every trial, increment the final position by one. Every trial should begin at the
same starting location. In the main function, you have to ensure that getDrunk
receives the correct starting position in the array, such that the walk will always
be within the array.



Code:


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

void readValues(int *numTrials,int *numSteps);
void getDrunk(int *path,int numTrials,int numSteps);
void printPath(int path[],int size);


int main()
{
 int numTrials,numSteps;
 int path[30] = {0};
 srand(123);                    //  printf("%d\n",rand());
 
 readValues(&numTrials,&numSteps);  //printf("%d    %d\n",numTrials,numSteps);
       
 getDrunk(&(path[15]),numTrials,numSteps);
 
 //printPath(path,15);
 
 
 
 
system("pause > null");
return 0;
}

void readValues(int *numTrials,int *numSteps)
{
 printf("Enter the num of trials and steps(strictly <= 15 )\n");
 scanf("%d%d",numTrials,numSteps);

return;
}


void getDrunk(int *path,int numTrials,int numSteps)
{
 
 int i,steps,dir;
 
 for(i=0;i<numTrials;i++)
  {
 
  steps = rand() % numSteps;
  dir = rand() % 2;  // 0 is back and 1 is forward
 
  if(numSteps % 2 == 0) // even
    {
      if(steps % 2 != 0)
      steps = steps + 1;
     
      if(dir == 0)
      {
        path = path - steps;
        *path = *path + 1;
      }
      else
      {
        path =  path + steps;
        *path = *path + 1;       
      }
     
    }
  else // odd
    {
    if(steps % 2 == 0)
      steps = steps + 1;
   
    if(dir == 0)
      {
        path = path - steps;  // move back
        *path = *path + 1;
      }
      else
      {
        path =  path + steps;
        *path = *path + 1;       
      }
 
    }
     
  path = &(path[15]); // reset back to centre
  }
 
 
return;
}


void printPath(int path[],int size)
{
 int i;
 
 for(i=0;i<30;i++);
  {
  printf(" %d: ");               
  if(path[i] > 0)
    {
    printf("%d  ",path[i]);
    }
  }

return;
}


xpi0t0s 11Jun2012 21:26

Re: Program quits after data input?
 
Think hard about this line:
Code:

path = &(path[15]); // reset back to centre

jose_peeterson 12Jun2012 10:47

Re: Program quits after data input?
 
Hi mr xpitos,
Is it a syntax error? should it be
Code:

path = &path[15]
i am keeping the index 15 as 0(center) position.

xpi0t0s 12Jun2012 13:23

Re: Program quits after data input?
 
No it's not a syntax error. Think again. What is path pointing at when you get into this function?

jose_peeterson 12Jun2012 14:25

Re: Program quits after data input?
 
it's pointing at the 15th index in the array path.

xpi0t0s 12Jun2012 21:33

Re: Program quits after data input?
 
No, it's not.

Edit: Sorry, correction. Yes, it is. What will it be pointing at after you add ANOTHER 15 elements to the pointer?

jose_peeterson 13Jun2012 15:19

Re: Program quits after data input?
 
It will point to another either the 30th index if path is ++ or 0th index if path is --. to reset back to the 15th index i use the code
Code:

path = &path[15]
at the VERY END of the loop. Am I missing anything?

THANKS A LOT

xpi0t0s 13Jun2012 20:42

Re: Program quits after data input?
 
Why not confirm what it is doing by displaying the value of the pointer? You'll see clearly what it is doing then.

jose_peeterson 15Jun2012 11:12

Re: Program quits after data input?
 
Dear Mr xpiotos,
I have fixed the problem. You were right!.
Code:

path = &path[15]
as the name path is same in both the right and left hand side of the equation, there was the problem of path pointing to some other location so what i did was i initialized a new pointer.
Code:

int *p = path
the incoming path is pointing exactly at the 15th index which is now saved inside p. At the end to bring path to center (index 15) i used
Code:

path = p
. and it works just fine I THINK!. At least no more bloody, annoying quitting without any reason. Please try the new code if you are free!.

Code:


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

void readValues(int *numTrials,int *numSteps);
void getDrunk(int *path,int numTrials,int numSteps);
void printPath(int path[],int size);


int main()
{
 int numTrials,numSteps;
 int path[30] = {0};
 int *p;
                  //  printf("%d\n",path[10]);
 
 readValues(&numTrials,&numSteps);  //printf("%d    %d\n",numTrials,numSteps);
 
 p =  &(path[15]);     
 getDrunk(p,numTrials,numSteps);
 
 printPath(path,30);
 
 
 
 
system("pause > null");
return 0;
}


void readValues(int *numTrials,int *numSteps)
{
 printf("Enter the num of trials and steps(strictly <= 15 )\n");
 scanf("%d%d",numTrials,numSteps);

return;
}


void getDrunk(int *path,int numTrials,int numSteps)
{
 
 int i,steps,dir;
 int *p = path;
 
 
 for(i=0;i<numTrials;i++)
  {                                      srand(i*4); 
 
  steps = rand() % numSteps;
  dir = rand() % 2;  // 0 is back and 1 is forward
 
  if(numSteps % 2 == 0) // even
    {
      if(steps % 2 != 0)
      steps = steps + 1;
     
      if(dir == 0)
      {
        path = path - steps;
        *path = *path + 1;
      }
      else
      {
        path =  path + steps;
        *path = *path + 1;       
      }
     
    }
  else // odd
    {
    if(steps % 2 == 0)
      steps = steps + 1;
   
    if(dir == 0)
      {
        path = path - steps;
        *path = *path + 1;
      }
      else
      {
        path =  path + steps;
        *path = *path + 1;       
      }
 
    }
     
  path = p;
  }
 
return;
}


void printPath(int path[],int size)
{
 int i;
 
 for(i=0;i<size;i++)
  {
  printf(" %d: ",i); 
               
  if(path[i] > 0)
    {
    printf("%d ",path[i]);
    }
  else
    printf("0 ");
 
  printf("\n");
  }

return;
}


priyatendulkar 19Jun2012 13:14

Re: Program quits after data input?
 
Hi,

Can you plz brief in how getDrunk() function works ..

Along with use of
steps = rand() % numSteps;
dir = rand() % 2; // 0 is back and 1 is forward


All times are GMT +5.5. The time now is 22:14.