Dear Mr xpiotos,
I have fixed the problem. You were right!.
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.
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
. 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;
}