1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Program quits after data input?

Discussion in 'C' started by jose_peeterson, Jun 9, 2012.

  1. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    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;
    }
    
    
    
    
    
    
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Think hard about this line:
    Code:
    path = &(path[15]); // reset back to centre
    
     
  3. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    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.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    No it's not a syntax error. Think again. What is path pointing at when you get into this function?
     
  5. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    it's pointing at the 15th index in the array path.
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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?
     
  7. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    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
     
    Last edited: Jun 13, 2012
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Why not confirm what it is doing by displaying the value of the pointer? You'll see clearly what it is doing then.
     
  9. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    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;
    }
    
    
    
    
    
     
  10. priyatendulkar

    priyatendulkar New Member

    Joined:
    Jun 20, 2011
    Messages:
    20
    Likes Received:
    1
    Trophy Points:
    0
    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
     

Share This Page