1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

a tricky program

Discussion in 'C++' started by sadam, Apr 21, 2011.

  1. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    What does scanf do? What do you *think* scanf does? Make absolutely certain those two are the same; even RTFM if you have to.
     
  2. sadam

    sadam New Member

    Joined:
    Apr 21, 2011
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    the scanf scans(read or write) data from a file, that's what i think
    what is the RTFM?
    is my program correct?
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    And I pointed it out for exactly what reason?

    RTFM=Read The Friendly Manual. You can look that sort of stuff up on Google.

    Well what do you think? Yes or no? Have a guess at the answer, and tell me why you think that answer is more correct (or less wrong) than the other one, and I'll tell you what I think and why.
     
  4. sadam

    sadam New Member

    Joined:
    Apr 21, 2011
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    look man i fixed the errors but when i try to run the program it stops working!!
    so you know what's wrong?
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Yes I know exactly what's wrong. And it seems I have to spell it out to you.

    scanf DOES NOT DO WHAT YOU THINK IT DOES. NOW GO AND READ THE ******* MANUAL.
     
  6. sadam

    sadam New Member

    Joined:
    Apr 21, 2011
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    come on man, i have corrected this. i am using the fscanf because i read from file but again the program stops working just before the fscanf's in the readfile start. do you know why?
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Well if you change the code it helps if you post the new version.
     
  8. sadam

    sadam New Member

    Joined:
    Apr 21, 2011
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define EPSILON    0.000001        /* a quantity small enough to be zero */
    #define    MAXN    20                /* maximum number of circles */
    #define DIMENSION 2            /* dimension of points */
    #define X 0                /* x-coordinate index */
    #define    Y 1                /* y-coordinate index */
    #define PI 4*atan(1.0)
    #define max(A, B) ((A) > (B) ? (A) : (B))
    #define min(A, B) ((A) < (B) ? (A) : (B))
    
    typedef double point[DIMENSION];
    
    typedef struct {
        double a;                    /* x-coefficient */
        double b;                    /* y-coefficient */
        double c;                    /* constant term */
    } line;
    
    typedef struct {
        point c;                    /* center of circle */
        double r;                    /* radius of circle */
    } circle;
    
    
    point s;                         /* initial position */
    point t;                         /* target position */
    int ncircles;                     /* number of circles */
    circle c[MAXN];                     /* circles data structure */
    
    void ReadFile( const char *filename )
    {
        FILE *file;
        int i;
        if (( file = fopen( filename, "r" )) == NULL )
        {
            printf ( "Error opening file!\n" );
            exit(1);
            }
    
        fscanf(file, "%f %f",s[X],s[Y]);
        fscanf(file, "%f %f",t[X],t[Y]);
        fscanf(file, "%d",ncircles);
        for (i=1; i<=ncircles; i++){
            fscanf(file, "%f %f %f",c[i].c[X],c[i].c[Y],c[i].r);
        }
        close( file );
    }
    
    void points_to_line(point p1, point p2, line *l)
    {
        if(p1[X] == p2[X])
        {
            l->b = 1;
            l->a = -(p1[Y]-p2[Y])/(p1[X]-p2[X]);
            l->c = -(l->a * p1[X]) - (p1[Y]);
        }
    }
    
    void point_and_slope_to_line(point p, double m, line *l)
    {
        l->a = -m;
        l->b = 1;
        l->c = -((l->a*p[X]) + (l->b*p[Y]));
    }
    
    void intersection_point(line l1, line l2, point p)
    {
        p[X] = (l2.b*l1.c - l1.b*l2.c) / (l2.a*l1.b - l1.a*l2.b);
    
    
        p[Y] = -(l2.a * p[X] + l2.c) / l2.b;
    }
    
    
    void closest_point(point p_in, line l, point p_c)
    {
        line perp;                                       // perpendicular to l through (x,y)
    
        if(fabs(l.b) <= EPSILON)                         // vertical line
        {
            p_c[X]=-(l.c);
            p_c[Y]=p_in[Y];
        return;
        }
    
        if(fabs(l.a) <= EPSILON)                         // horizontal line
        {
            p_c[X]=p_in[X];
            p_c[Y]=-(l.c);
        return;
        }
    point_and_slope_to_line(p_in,1/l.a,&perp);
    intersection_point(l,perp,p_c);
    }
    
    double distance(point p1, point p2)
    {
        return sqrt(((p1[Y]-p2[Y])*(p1[Y]-p2[Y])) + ((p1[X]-p2[X])*(p1[X]-p2[X])));
    }
    
    
    
    int main()
    {
        line l;
        point s;                        /* initial position */
        point t;                        /* target position */
        int ncircles =4;                    /* number of circles */
        circle c[MAXN];                    /* circles data structure */
        point close ;                    /* closest point */
        double d ;                        /* distance from circle-center */
        double xray =0.0;                /* length of intersection with circles */
        double around =0.0;             /* length around circular arcs */
        double angle;                    /* angle subtended by arc */
        double dist;                    /* total travel distance */
        int i;
    
        ReadFile( "route.dat");
    
        points_to_line(s,t,&l);
        for (i=1; i<=ncircles; i++) {
            closest_point(c[i].c,l,close);
            d = distance(c[i].c,close);
            if (d < c[i].r) {
                xray += 2*sqrt(c[i].r*c[i].r - d*d);
                angle = acos(d/c[i].r);
                around += ((2*angle)/(2*PI)) * (2*PI*c[i].r);
            }
        }
    
    
        dist = distance(s,t) - xray + around;
        printf("reeee %f\n", dist);
    }
    i used the printf as you told me before in order to find where the problem is, and i found out that it is just before the first scanf in the readfile function begin.
     
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Good, so add some more printfs so you can find out where the new version of the program stops working. You know how to do this now, so let me know at which line the program fails.

    One of the problems with using printfs in this way is that output is buffered, so if the program crashes, it may not have displayed all the printf'd data before it crashed. For example:
    Code:
    printf("note 1\n"); // this is printed
    // some code A
    printf("note 2\n"); // this is buffered
    // some code B
    printf("note 3\n"); // this is buffered
    // some code C, which crashes
    
    Since the buffer contents are lost when the program crashes, the output is
    note 1
    and "note 2" is not displayed, which could lead you to think it was some code A that crashed.

    This can lead to confusion in debugging. So make sure every debug printf is followed by a flush, which will ensure all data is printed out. eg:
    Code:
    printf("note 1\n"); fflush(stdout);
    // some code A
    printf("note 2\n"); fflush(stdout);
    // some code B
    printf("note 3\n"); fflush(stdout);
    // some code C, which crashes
    
    Now the output will be
    note 1
    note 2
    note 3
    and from this you'll know it was some code C that crashed, particularly if it is followed by
    Code:
    printf("note 4\n"); fflush(stdout);
    
     
  10. sadam

    sadam New Member

    Joined:
    Apr 21, 2011
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    i already told you that i have found out that the program stops working when the first scanf in the ReadFile function starts!!!
    i am pretty sure that the rest program is correct but because of the problem that has occurred is doesn't work!!
    do you know how to fix it?
     
  11. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    There aren't any scanf calls in the ReadFile function. And when I changed them previously from scanf to fscanf this worked fine, so if your fscanfs are broken then that's a compiler bug I can't help you with.
     
  12. sadam

    sadam New Member

    Joined:
    Apr 21, 2011
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    you mean that the program in your computer works correctly?
    can you tell me what result does it post out?

    dist = ???
     
  13. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    I haven't got that far with it. And I'm pretty sure there are other errors as well. What distance do you expect it to print out from calculating the value manually from the test data?
     
  14. sadam

    sadam New Member

    Joined:
    Apr 21, 2011
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    i don't know, i haven't calculated the distance yet, what other errors are there?
    i think it's correct except the readfile
     
  15. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    You need to find the errors yourself. I'm not going to do it for you, but I will help you learn how to find them. Basically it's very easy: just add printf statements to the code, displaying where it's up to and what values it's calculated, then you can see where it goes wrong (if at all). You WILL DEFINITELY need to have calculated the value manually, because if you can't do that then you have no chance of being able to program a computer to do it.

    It's easier with a debugger of course, particularly the visual debugger provided with Visual Studio 2010 but I realise not everyone has access to such products. Hence the printf suggestion, which I still use even with VS, when building a program it really helps me to focus on what the code should do if I've already added a bunch of printf's that display what the program should do.
     
  16. sadam

    sadam New Member

    Joined:
    Apr 21, 2011
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    ok i have done what you told me by adding several printf's in the program and i found out that the main() starts working fine and when i call the readfile it stops working
    also in the readfile it posts me only the printf that i have add before the scanf

    so what should i understand with this? maybe there are no errors except the readfile??
     
  17. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Post the code.

    no errors: nope, there is definitely still at least one error that will definitely cause you to get wrong results. It'll be up to you to find it.
     
  18. sadam

    sadam New Member

    Joined:
    Apr 21, 2011
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    where man?? help me to find it
     
  19. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Stop replying immediately and THINK.
     
  20. sadam

    sadam New Member

    Joined:
    Apr 21, 2011
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    help me man, the deadline is tomorrow
     

Share This Page