0
Mentor
Where exactly are you confused? I've goven you some pretty detailed instruction as to how best to proceed but you appear to be ignoring it.
0
Go4Expert Member
Quote:
Originally Posted by xpi0t0s
Where exactly are you confused? I've goven you some pretty detailed instruction as to how best to proceed but you appear to be ignoring it.
ok, well is this program able to give me the solution after some corrections???
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXCIRCLES 20

struct point {
double x;
double y;
};

struct line {
double a; /* Slope */
double b; /* y-intercept */
};

struct circle {
double cx, cy; /* Center coordinates */
double r; /* Circle radius */
};

void ReadFile( const char *filename, point *x, point *y, int *nobst )
{
FILE *file;
int i;
if (( file = fopen( filename, "r" )) == NULL )
{
printf ( "Error opening file!\n" );
exit(1);
}
fscanf( file, "%lf %lf", &point->x, &point->y );
fscanf( file, "%lf %lf", &point->x, &point->y );
fscanf( file, "%d", nobst );
for( i = 0; i <*nobst )
{
fscanf( file, "%lf %lf %1f", &circle.cx,&circle.cy,&circle.r)
}
fclose( file );
}

/* Distance between two points */
double PointToPoint(sturct point *p, struct point *q);

/* Distance between a point and a line */
double PointToLine(struct point *p, struct line *l);

int main()
{
point s;
point t;
int nobst;
circle c[MAXCIRCLES];
ReadFile( "route.dat", &x, &y, &nobst );

line l;
point close;
double d;
double xray = 0.0;
double around = 0.0;
double angle;
double travel;
int i;
double asin(),sqrt();
double distance();

points_to_line(s,t,&l);

for (i=1; i<=nobst; i++) {
closes_point(c[i].c,,l,close);
d = distance(c[i].c,close);
if ((d>=0) &&  (d<c[i].r) && point_in_box(close,s,t){
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("f\n",dist);
}
thank you very much, i hope that i will get to a solution soon
0
Mentor
Yes, but it will need a number of corrections as well as additional code. The "sturct" typo is still there so it looks like you've done nothing since you last posted it, so check my previous reply for a catalog of syntactic errors - no point me going through the code again.

Have you compiled it yet? What errors does the compiler give you?
If you haven't compiled it yet, why not?

You still haven't done what I suggested, which is to open the file, read the data and display it to the screen. Why not?
0
Go4Expert Member
Quote:
Originally Posted by xpi0t0s
Yes, but it will need a number of corrections as well as additional code. The "sturct" typo is still there so it looks like you've done nothing since you last posted it, so check my previous reply for a catalog of syntactic errors - no point me going through the code again.

Have you compiled it yet? What errors does the compiler give you?
If you haven't compiled it yet, why not?

You still haven't done what I suggested, which is to open the file, read the data and display it to the screen. Why not?
what do you mean about the struct typo? the struct make me easier to work, why is it wrong?
moreover, the Readfile is working as it should isn't it?

the compiler has many errors mainly because i haven't use some ''names'' before

thank you
0
Go4Expert Member
Quote:
Originally Posted by xpi0t0s
Yes, but it will need a number of corrections as well as additional code. The "sturct" typo is still there so it looks like you've done nothing since you last posted it, so check my previous reply for a catalog of syntactic errors - no point me going through the code again.

Have you compiled it yet? What errors does the compiler give you?
If you haven't compiled it yet, why not?

You still haven't done what I suggested, which is to open the file, read the data and display it to the screen. Why not?
I am really confused, i have done this so far and it still doesn't work!!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXCIRCLES 20
#define PI 3,1415

struct point {
double x;
double y;
};

struct line {
double a; /* Slope */
double b; /* y-intercept */
};

struct circle {
double cx, cy; /* Center coordinates */
double r; /* Circle radius */
};

void ReadFile( const char *filename, struct point *x, struct point *y, int *nobst )
{
struct circle *c;
FILE *file;
int i;
if (( file = fopen( filename, "r" )) == NULL )
{
printf ( "Error opening file!\n" );
exit(1);
}
fscanf( file, "%lf %lf", &x->x, &x->y );
fscanf( file, "%lf %lf", &y->x, &y->y );
fscanf( file, "%d", nobst );
for( i = 0; i <*nobst; i++ )
{
fscanf( file, "%lf %lf %1f", &c->cx,&c->cy,&c->r);
}
fclose( file );
}

/* Distance between two points */
double distance(sturct point *p, struct point *q)
{
double dx;
double dy;

dx = q->x - p->x;
dy = q->y - p->y;

return sqrt(dx*dx + dy*dy);
}

/* Distance between a point and a line */
double PointToLine(struct point *p, struct point *q, struct line *l)
{
l->a = (q->y -p->y)/(q->x-p->x);
l->b = p->y - (l->a*p->x);
}

int main()
{
struct point *s;
struct point *t;
int nobst;
struct circle *c;

ReadFile( "route.dat", s, t, &nobst );

struct line *l;
double dist;
double xray = 0.0;
double around = 0.0;
double angle;
double travel;
int i;
double asin(),sqrt();
double distance();

points_to_line(s,t,&l);

for (i=1; i<=nobst; i++)
{
xray += 2*sqrt(c[i].r*c[i].r) - distance(s,t);
angle = acos(distance(s,t)/c[i].r);
around += ((2*angle)/(2*PI)) * (2*PI*c[i].r);

}

dist = distance(s,t) - xray + around;
printf("f\n",dist);
}
can you help me to make this work?
0
Go4Expert Member
i have a syntax error in my previous post, i wrote 'sturct' instead of 'struct'. but although it doesn't work..please help me, what is going wrong finally??
0
Mentor
Sigh. Stop me when you hit a word you don't understand:

What.
Errors.
Does.
The.
Compiler.
Throw?
0
Go4Expert Member
Quote:
Originally Posted by xpi0t0s
Sigh. Stop me when you hit a word you don't understand:

What.
Errors.
Does.
The.
Compiler.
Throw?
it throws no erros but only one warning '' note: expected 'struct line *' but argument is of type 'struct line **''

however when i run the program it writes me 'error opening file'
that's all, i'm waiting to help me
0
Mentor
Really? There are loads of errors. What compiler are you using?

Here's what Visual Studio 2010 makes of the code:

1>c:\users\dave\documents\visual studio 2010\projects\test1\test1.cpp(92): error C2660: 'sqrt' : function does not take 1 arguments

It probably bails out early as those are fatal errors anyway. As points_to_line isn't defined, I'll comment that line out. The second error is probably due to this line:
Code:
double asin(),sqrt();
so I'll comment that out as well (and the other local function prototype, which is not necessary), and recompile:

1>c:\users\dave\documents\visual studio 2010\projects\test1\test1.cpp(83): warning C4101: 'travel' : unreferenced local variable
1>c:\users\dave\documents\visual studio 2010\projects\test1\test1.cpp(78): warning C4101: 'l' : unreferenced local variable
1>c:\users\dave\documents\visual studio 2010\projects\test1\test1.cpp(43): warning C4700: uninitialized local variable 'c' used
1>c:\users\dave\documents\visual studio 2010\projects\test1\test1.cpp(65): error C4716: 'PointToLine' : must return a value
1>c:\users\dave\documents\visual studio 2010\projects\test1\test1.cpp(76): warning C4700: uninitialized local variable 't' used
1>c:\users\dave\documents\visual studio 2010\projects\test1\test1.cpp(76): warning C4700: uninitialized local variable 's' used
1>c:\users\dave\documents\visual studio 2010\projects\test1\test1.cpp(92): warning C4700: uninitialized local variable 'c' used

"unreferenced local variable" warnings can be ignored for now.
The third is on this line:
Code:
fscanf( file, "%lf %lf %1f", &c->cx,&c->cy,&c->r);
and checking back to the last usage of c
Code:
struct circle *c;
it is indeed uninitialised.

"'PointToLine' : must return a value" is self-explanatory. It doesn't. Since this isn't called anywhere I'll just change the return type to void.

The next two relate to this line:
Code:
ReadFile( "route.dat", s, t, &nobst );
where s and t are undefined:
Code:
struct point *s;
struct point *t;
so you've declared them as pointers, but you haven't declared two ACTUAL "struct point" variables for them to point at. Based on the problem description I think these should probably not be defined as pointers; these correspond to the driver's start and stop position, correct? So I'll take out the *'s and change the call to ReadFile to take their addresses.

The final warning relates to this line
Code:
xray += 2*sqrt(c[i].r*c[i].r) - distance(s,t);
which is another consequence of leaving c undefined
Code:
struct circle *c;
With the modifications I now get
1>c:\users\dave\documents\visual studio 2010\projects\test1\test1.cpp(92): error C2664: 'distance' : cannot convert parameter 1 from 'point' to 'point *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\dave\documents\visual studio 2010\projects\test1\test1.cpp(93): error C2664: 'distance' : cannot convert parameter 1 from 'point' to 'point *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\dave\documents\visual studio 2010\projects\test1\test1.cpp(98): error C2664: 'distance' : cannot convert parameter 1 from 'point' to 'point *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

Still no sign of your compiler's message "expected 'struct line *' but argument is of type 'struct line **''. No usage of line corresponds......except possibly the call to points_to_line(). Does this compiler warning relate to a different version of the code than the latest code you posted?

'error opening file'......hmm. What error, I wonder. If you define as a new global variable:
Code:
int errno;
fopen will write the error code to this file, then you can find out what error with something like:
Code:
printf ( "Error %d opening file!\n", errno );
and by looking up the meaning of errno codes you can find out what the error is. Most likely error is "file not found" - does route.dat exist in the same directory as the program is running? (Note the careful wording. This is NOT necessarily the directory where the executable is located.)
0
Go4Expert Member
what sould i do now? i have been trying for over a week without any result!! can you help me and write me some of the code?