0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
What does scanf do? What do you *think* scanf does? Make absolutely certain those two are the same; even RTFM if you have to.
0
sadam's Avatar, Join Date: Apr 2011
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
What does scanf do? What do you *think* scanf does? Make absolutely certain those two are the same; even RTFM if you have to.
the scanf scans(read or write) data from a file, that's what i think
what is the RTFM?
is my program correct?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Quote:
the scanf scans(read or write) data from a file, that's what i think
And I pointed it out for exactly what reason?

Quote:
what is the RTFM?
RTFM=Read The Friendly Manual. You can look that sort of stuff up on Google.

Quote:
is my program correct?
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.
0
sadam's Avatar, Join Date: Apr 2011
Go4Expert Member
look man i fixed the errors but when i try to run the program it stops working!!
so you know what's wrong?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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.
0
sadam's Avatar, Join Date: Apr 2011
Go4Expert Member
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?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Well if you change the code it helps if you post the new version.
0
sadam's Avatar, Join Date: Apr 2011
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
Well if you change the code it helps if you post the new version.
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.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
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);
0
sadam's Avatar, Join Date: Apr 2011
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
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);
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?