Thread: Debugging help :(

  1. #46
    Registered User
    Join Date
    Dec 2012
    Posts
    17
    First, thank you for your effort esbo; god bless you.

    Actually, The code you removed is actually essential for the program to work; since it is the initial position.

    Here is my latest code that i pulled an all nighter on,

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    typedef double real;
    
    
    typedef struct
    {
        real xpos;
        real ypos;
        real zpos;
    
    
        real uvel;
        real vvel;
        real wvel;
    
    
        real omegax;
        real omegay;
        real omegaz;
    
    
        real radius;
        real mass;
    
    
    } sphere;
    
    
    void update_position(sphere *ball, real dt)
    {
       //(*ball).xpos
       ball->xpos=ball->xpos + dt*ball->vvel;
       //ypos,zpos
    }
    
    
    void detect_ball(sphere *ball1, sphere *ball2, real *pdist)
    {
    //real dist;
    
    
        *pdist=sqrt((ball2->xpos-ball1->xpos)*(ball2->xpos-ball1->xpos)+
                   (ball2->ypos-ball1->ypos)*(ball2->ypos-ball1->ypos));
    
    
    //        if ( dist<= (ball1->radius + ball2->radius) +0.0075 )
    //        {
    //            if ( dist>= ball1->radius + ball2->radius -0.0075   )
    //            {
    //            ball1->xpos=0;
    //           }
    //        }
    
    
    }
    
    
    int main(int argc, char **argv)
    {
        real dt=0.01;
        real t, dist1, tmax=0, mlimit, firstvvel, gap=1.5, ballmatxpos[2001][2],  dist[2];
        int nballs=2 ;
        int i,j,m,k;
        sphere *ball;
    
    
        mlimit=2000;
        
        //initialise positions
        ball=malloc(nballs*sizeof(sphere));
        for(i=0;i<nballs+1;i++)
        {
           
           ball[i].radius=0.5;
           ball[i].xpos=(real) i * (2*ball[i].radius + gap);
    
    
        //   ball[2].xpos=5.0;
           
           ball[i].ypos=0.0;
        //   ball[i].uvel=1.0;
           ball[i].vvel=0.0;
           ball[0].vvel=1.0; 
          
        }
           
       
        //pendulum to be released
       // ball[0].xpos=0;
        //ball[0].ypos=0;
      //  firstvvel=0;
    
    
        //timestep loop
        t=0.0;
        dist1=0.0;
    //    while(t<tmax+1)
    //    {
            for(m=0;m<mlimit+1;m++)
           {
           //update position
            for(i=0;i<nballs+1;i++)
            {
                  update_position(&(ball[i]), dt);
            }
           //collision detection
            for(i=0;i<3;i++)
            {
                for(j=1;j<3;j++)
                {
                    detect_ball(&(ball[i]),&(ball[j]), &dist1);
                    dist1=sqrt((ball[i].xpos-ball[j].xpos)*(ball[i].xpos-ball[j].xpos)+
                    (ball[i].ypos-ball[j].ypos)*(ball[i].ypos-ball[j].ypos));
                    
              //      printf( " %3.4lf %3.4lf\n ", ball[i].ypos, ball[j].ypos);
                    
                  if ( dist1 < (ball[i].radius + ball[j].radius) +0.005 )
                    {
                       if ( dist1 > (ball[i].radius + ball[j].radius) -0.005   )
                        {
                 //  ball2->uvel=ball1->vvel;
                    //yes collision, modify velocities and rotation rates
                        ball[0].xpos=0; 
                        ball[1].vvel=ball[0].vvel; 
                      //  ball[1].xpos=0;                  
                        }
                    }
                }
            }
    
    
            ballmatxpos[m][0]=ball[0].xpos;
            ballmatxpos[m][1]=ball[1].xpos;
            ballmatxpos[m][2]=ball[2].xpos;
            t+=dt;
            }
    //    }
    //             for(i=0;i<=nballs;i++) 
    //            {
    //                 for(m=0;m<=mlimit;m++)
    //              {
    //        printf( " %3.4lf \n ", ballmatxpos[m][i]);
    //              }
    //            }
    
    
    // printf("In this case, initial velocity= %3.4lf with t=%3.4lf \n", ballmatxpos[m][0], ballmatxpos[m][1] );
    
    
    //    ---------------//-------------------------
          FILE *pFile1;
           //  FILE *pFile2;
             pFile1 = fopen("graphing3.txt", "w");
            // pFile2 = fopen("graphing4.txt", "w");
    
    
             if (pFile1 != NULL)
            {
          for(m=0;m<mlimit+1;m++)
                {     
                 for(i=0;i<nballs+1;i++) 
                  {
             fprintf( pFile1, " %3.4lf", ballmatxpos[m][i]);
                  }
          fprintf( pFile1,"\n");
                }
          fprintf( pFile1,"\n");
          fclose(pFile1);
            }
          //if (pFile2 != NULL)
    //        {
    //             for(m=0;m<=mlimit;m++)
    //             {
    //         fprintf( pFile2, " %3.4lf  \n", ballmatxpos[m][1]);
    //         }
    //       fprintf( pFile2,"\n");
    //          fclose(pFile2);
    //       }
                     else
            {
                printf("inda dapat print bro");
           }
    
    
     // -----------------------------------------------------
    
    
    
    
    
    
        getchar();
        return 0;
    }
    1. i removed the while (t<tmax) part;
    2. edit the malloc and put it in the loop
    3. and adjust <= to < as advised.

    "fprintf" prints out the ball position in x dimension.

    If you try it, it works, for ball 1 and ball 2, it's movement makes sense. but when it come to ball 3 (the 3rd row in matrix) its not, since we didn't initialise its position at 0.

    This all convinced me that my memory allocation methodology is wrong.

    Ok, seems something wrong with my memory allocation for the balls... then please tell me, what is the correct way to allocate memory for the balls? and I want to have a control of the no.of balls available.

  2. #47
    Registered User
    Join Date
    Dec 2012
    Posts
    17
    Quote Originally Posted by anduril462 View Post
    Announcements - C Programming

    Check out rule #3, please don't bump your threads in the future, it's not good forum etiquette. Many of us are quite busy with our own jobs, and volunteer our time to help. A little patience is expected on your part. Furthermore, you would be much more likely to get help if your code didn't look like crap (lack of formatting) and you had a more detailed problem description. Please properly indent and format your code when you post here in the future, so we actually want to read it and help you. A more complete problem description would also help.
    .
    My bad, I apologise for my initial bump anduril, wont do it again.

  3. #48
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Zul56 View Post
    First, thank you for your effort esbo; god bless you.

    Actually, The code you removed is actually essential for the program to work; since it is the initial position.

    Here is my latest code that i pulled an all nighter on,

    1. i removed the while (t<tmax) part;
    2. edit the malloc and put it in the loop
    3. and adjust <= to < as advised.

    "fprintf" prints out the ball position in x dimension.

    If you try it, it works, for ball 1 and ball 2, it's movement makes sense. but when it come to ball 3 (the 3rd row in matrix) its not, since we didn't initialise its position at 0.

    This all convinced me that my memory allocation methodology is wrong.

    Ok, seems something wrong with my memory allocation for the balls... then please tell me, what is the correct way to allocate memory for the balls? and I want to have a control of the no.of balls available.
    I don't think you need the malloc inside the loop the way you are doing it now with some of the other bugs removed. As you were doing nballs x (the memory required per ball) and also writing just nball (not nballs + 1 was you were doing in error) it should be OK outside the loop.

    I will try and have a look at it in more detail now, I misunderstood what you were trying to do before and thought you were trying to do something else and basically accidentally got it working ( or at least not crashing).

    So that was a bit weird in a way I sort of accidentally fixed it and jumped to lot of wrong conclusions!!

  4. #49
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <conio.h>
     
     
    typedef double real;
     
     
    typedef struct
    {
        real xpos;
        real ypos;
        real zpos;
         
        real uvel;
        real vvel;
        real wvel;
         
        real omegax;
        real omegay;
        real omegaz;
         
        real radius;
        real mass;
         
    } sphere;
     
     
    void update_position(sphere *ball, real dt)
    {
       //(*ball).xpos
       ball->xpos=ball->xpos + dt*ball->vvel;
       //ypos,zpos  
    }
     
     
    void detect_ball(sphere *ball1, sphere *ball2, real *pdist)
    {
         
        *pdist=sqrt((ball2->xpos-ball1->xpos)*(ball2->xpos-ball1->xpos)+
                   (ball2->ypos-ball1->ypos)*(ball2->ypos-ball1->ypos));
                    
    //        if ( dist<= (ball1->radius + ball2->radius) +0.0075 )
    //        {
    //            if ( dist>= ball1->radius + ball2->radius -0.0075   )
    //            {  
    //            *ptime=0;
    //            }
    //        }
     
     
    }
     
     
    int main(int argc, char **argv)
    {
    
    
        real dt=0.01;
        real t, dist1, tmax=1, mlimit, firstvvel, gap=1.5,  dist[2], ballmatxpos[101][2];
        int nballs=4 ;
        int i,j,m,k;
        sphere *ball;
        ball=(sphere *)malloc(nballs*sizeof(sphere));
    
    
    char scrap[20000];
        
        mlimit=tmax/dt;
        //initialise positions
        for(i=0;i<nballs;i++)
        {
    // i--;
           ball[i].radius=0.5;
        ball[i].xpos=(real) i * (2*ball[i].radius + gap);
           ball[i].ypos=0.0; 
            printf("\n here  a /n"); fflush(stdout);
           ball[i].uvel=1.0;
           ball[i].vvel=1.0;
           ball[i].vvel=0.0;
    //i++;
        }
    
    
    printf("\n here"); fflush(stdout);
    
    ball[0].xpos=(real) 0 * (2*ball[0].radius + gap);
         printf("\n here2"); fflush(stdout);
        //pendulum to be released
       // ball[0].xpos=0;
        //ball[0].ypos=0;
      //  firstvvel=0;
         
        //timestep loop
        t=0.0;
        dist1=0.0;
        while(t<tmax)
        {
            for(m=0;m<mlimit;m++)
           {
           //update position
            for(i=0;i<nballs;i++)
            {
                  update_position(&(ball[i]), dt); 
            }
           //collision detection
            for(i=0;i<nballs;i++)
            {
                for(j=0;j<nballs;j++)
                {
                    detect_ball(&(ball[i]),&(ball[j]), &dist1);     
                                  
                  if ( dist1 <= (ball[i].radius + ball[j].radius) +0.075 )
                    {
                       if ( dist1 >= (ball[i].radius + ball[j].radius) -0.075   )
                        {
                 //  ball2->uvel=ball1->vvel;
                    //yes collision, modify velocities and rotation rates  
                        t=0;
                         }
                    }
                }
            }
             
            ballmatxpos[m][0]=t;
            ballmatxpos[m][1]=ball[0].xpos;
     
     printf("\n here3"); fflush(stdout);
       printf("In this case, initial velocity= %3.4lf with t=%3.4lf \n", ballmatxpos[m][0], t );  
                                    
            t+=dt;   
            }
        }
                FILE *pFile1;
             FILE *pFile2;
     char scra3p[20000];
    printf("\n here4"); fflush(stdout);
    //    ---------------//-------------------------
    
             pFile1 = fopen("graphing3.txt", "w");
    printf("\n here41"); fflush(stdout);
             pFile2 = fopen("graphing4.txt", "w");
    printf("\n here42"); fflush(stdout);
             if (pFile1 != NULL)
            {
    printf("\n her5e"); fflush(stdout);
                 for(m=0;m<=mlimit;m++)
                 {
             fprintf( pFile1, " %3.4lf \n ", ballmatxpos[m][0]);
                  }
          fprintf( pFile1,"\n");
          fclose(pFile1);
            }
          if (pFile2 != NULL)
            {
                 for(m=0;m<=mlimit;m++)
                 {
             fprintf( pFile2, " %3.4lf  \n", ballmatxpos[m][1]);
             }
           fprintf( pFile2,"\n");
          fclose(pFile2);     
           } 
                     else
            {
                printf("inda dapat print bro");
           }
     
     
     
     
     
     
    //  ----------------------------------------------------- 
     
     
     
     
         
        getch();
        return 0;
    }
    OK the above code seem to run fine for 4 balls and indeed 32 ( which I just tried) so should run for any number.
    I have left some debug code in which need removing ( the prinf and fflush stuff). I am not to sure what the program is doing but it does not crash. I also note one file graphing4.txt contain all 0.0's which I kind of suspect is bug as I presume that is not intended?
    Last edited by esbo; 01-12-2013 at 09:04 PM.

  5. #50
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    One other thing , in your code you have line 87?

    ball[0].vvel=1.0;
    surely that should be

    ball[i].vvel=1.0;

  6. #51
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    [
    Quote Originally Posted by Zul56 View Post
    My bad, I apologise for my initial bump anduril, wont do it again.
    Personally I am not too bothered if your code formatting looks crap, it's considerably better than mine so I am used to it!!!

    They tend to be a bit picky about things here, particularly the fact that nearly all my variable are global!!!
    I know that is supposed to be bad practise but it has never really caused me a problem.

    There is some other stuff in the program I left in which needs removing stuff like

    Code:
     char scra3p[20000];
    Which does nothing really I was just trying something.

    I am not sure why your last program didn't seem to work it seems petty similar to mine.
    I basically just changed the rest of the <= to < where appropriate.

  7. #52
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <conio.h>
     
     
    typedef double real;
     
     
    typedef struct
    {
        real xpos;
        real ypos;
        real zpos;
         
        real uvel;
        real vvel;
        real wvel;
         
        real omegax;
        real omegay;
        real omegaz;
         
        real radius;
        real mass;
         
    } sphere;
     
     
    void update_position(sphere *ball, real dt)
    {
       //(*ball).xpos
       ball->xpos=ball->xpos + dt*ball->vvel;
       //ypos,zpos  
    }
     
     
    void detect_ball(sphere *ball1, sphere *ball2, real *pdist)
    {
         
        *pdist=sqrt((ball2->xpos-ball1->xpos)*(ball2->xpos-ball1->xpos)+
                   (ball2->ypos-ball1->ypos)*(ball2->ypos-ball1->ypos));
                    
    //        if ( dist<= (ball1->radius + ball2->radius) +0.0075 )
    //        {
    //            if ( dist>= ball1->radius + ball2->radius -0.0075   )
    //            {  
    //            *ptime=0;
    //            }
    //        }
     
     
    }
     
     
    int main(int argc, char **argv)
    {
    
    
        real dt=0.01;
        real t, dist1, tmax=1, mlimit, firstvvel, gap=1.5,  dist[2], ballmatxpos[101][2];
        int nballs=32 ;
        int i,j,m,k;
     //   sphere **ball; // <--------------------------------------------------this is  achange I made
    sphere *ball[nballs]; // <--------------------------------------------------this is  achange I made
    
     //   ball=(sphere *)malloc(nballs*sizeof(sphere));
    
    
    
        
        mlimit=tmax/dt;
        //initialise positions
        for(i=0;i<nballs;i++)
        {
        ball[i]=(sphere *)malloc(sizeof(sphere));  // <--------------------------------------------------this is  achange I made
    
    // i--;
        ball[i]->radius=0.5;
    	ball[i]->xpos=(real) i * (2*ball[i]->radius + gap);
        ball[i]->ypos=0.0; 
        ball[i]->uvel=1.0;
        ball[i]->vvel=1.0;
        ball[i]->vvel=0.0;
    //i++;
        }
    
    
    
    
    ball[0]->xpos=(real) 0 * (2*ball[0]->radius + gap);
        
        //pendulum to be released
       // ball[0].xpos=0;
        //ball[0].ypos=0;
      //  firstvvel=0;
    
         
        //timestep loop
        t=0.0;
        dist1=0.0;
        while(t<tmax)
        {
            for(m=0;m<mlimit;m++)
           {
           //update position
            for(i=0;i<nballs;i++)
            {
                  update_position((ball[i]), dt); 
            }
           //collision detection
            for(i=0;i<nballs;i++)
            {
                for(j=0;j<nballs;j++)
                {
                    detect_ball((ball[i]),(ball[j]), &dist1);     
                                  
                  if ( dist1 <= (ball[i]->radius + ball[j]->radius) +0.075 )
                    {
                       if ( dist1 >= (ball[i]->radius + ball[j]->radius) -0.075   )
                        {
                 //  ball2->uvel=ball1->vvel;
                    //yes collision, modify velocities and rotation rates  
                        t=0;
                         }
                    }
                }
            }
             
            ballmatxpos[m][0]=t;
            ballmatxpos[m][1]=ball[0]->xpos;
     
    
       printf("In this case, initial velocity= %3.4lf with t=%3.4lf \n", ballmatxpos[m][0], t );  
                                    
            t+=dt;   
            }
        }
                FILE *pFile1;
             FILE *pFile2;
    
    //    ---------------//-------------------------
    
             pFile1 = fopen("graphing3.txt", "w");
    
             pFile2 = fopen("graphing4.txt", "w");
    
             if (pFile1 != NULL)
            {
    
                 for(m=0;m<=mlimit;m++)
                 {
             fprintf( pFile1, " %3.4lf \n ", ballmatxpos[m][0]);
                  }
          fprintf( pFile1,"\n");
          fclose(pFile1);
            }
          if (pFile2 != NULL)
            {
                 for(m=0;m<=mlimit;m++)
                 {
             fprintf( pFile2, " %3.4lf  \n", ballmatxpos[m][1]);
             }
           fprintf( pFile2,"\n");
          fclose(pFile2);     
           } 
                     else
            {
                printf("inda dapat print bro");
           }
     
     
     
     
     
     
    //  ----------------------------------------------------- 
     
     
     
     
         
        getch();
        return 0;
    }
    This is a bit more like how I would have done the memory allocation.
    Note I allocated space for one ball at a time. Up to 32.




    Code:
        int nballs=32 ;
        int i,j,m,k;
     //   sphere **ball; // <--------------------------------------------------this is  achange I made
    sphere *ball[nballs]; // <--------------------------------------------------this is  achange I made
    Indeed if you use
    Code:
     sphere **ball; //
    I will also work (I think) it did when I tried anyway but I think this is a bug as I am not declaring space for the pointers to the balls as I do with the
    Code:
    sphere *ball[nballs];
    declaration.

    It's useful to do it like that if you do not know at run time how many balls you will have, so you just allocate a new one when needed saving memory.

    I also need to free up the memory at the end and I will post and update to do that when I have more time.
    Do you would need a loop freeing up the memory at the end, but windows does that for you when the program terminates anyway.
    Last edited by esbo; 01-12-2013 at 10:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help Debugging (ADT)
    By minidragon in forum C Programming
    Replies: 16
    Last Post: 08-08-2011, 06:39 PM
  2. Debugging Dev C++
    By tuurb046 in forum Tech Board
    Replies: 10
    Last Post: 08-16-2007, 12:51 PM
  3. debugging
    By St0rM-MaN in forum Tech Board
    Replies: 13
    Last Post: 07-06-2007, 02:03 PM
  4. help debugging
    By MB1 in forum C++ Programming
    Replies: 6
    Last Post: 11-03-2005, 01:48 PM
  5. VC++ Debugging
    By neandrake in forum C++ Programming
    Replies: 5
    Last Post: 12-02-2003, 07:31 PM