xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
What are the supposedly wrong messages? why won't you answer my questions directly?
I'm the expert and you're the beginner, remember, so maybe I have a clue what I'm asking...just a thought...

Fact is you have way more than three problems. Your syntax is all over the place.
You need to follow my first suggestion. Write a SMALL amount of code to start. Then
get that code working BEFORE you try to go onto anything new. I'm not going to reply again until you
show evidence of having done that because I'm bored with repeating myself, and you would solve this
problem a whole lot quicker if you listened to me. I know it feels like a retrograde step to throw all
this wonderful code away and start over, but it's not really.

Anyway while I'm here. I'll see what Visual Studio 2010 will do with the code. Get a syntax-highlighting
editor by the way, it will make your life a LOT easier, and ALWAYS ALWAYS ALWAYS strictly indent code.

points_to_line() has too many braces
closest_point() doesn't have enough

By the way always define a return type. The default is int. Return int if that's what you want, otherwise
return void, then it's clear what your intention is.

points_to_line() first argument is wrong. The correct syntax is TYPE<space>VARIABLE.
You have TYPE<dot>VARIABLE.

In distance(), p and q are defined as struct point *, i.e. pointer to struct point, but you're
trying to use local semantics (p.x instead of p->x).

In point_and_slope_to_line(), p is defined as struct point and you're trying to use array semantics
to access x and y, i.e. p[x] instead of p.x. Also I've no idea what p_c[X] is meant to mean. Do you
mean p_c.x? Don't forget C and C++ are case sensitive.

In closest_point, p_c is defined as point, not point*. Remember the difference between pass by copy and pass
by reference. This is pass by copy, so if your aim here is to modify the caller's p_c, you need to change
the definition to point* (and use pointer semantics to access the attributes, e.g. p_c->x.

In distance(), remember that TYPE FUNCTION_NAME(ARGS)<SEMI-COLON> is a function PROTOTYPE, not a function definition.
So
Code:
double distance(struct point *p, struct point *q);
{
is wrong.

Maybe closest_point is missing a brace because you haven't finished writing it yet. It handles vertical and horizontal lines,
but what about the rest?

distance() uses automatic semantics not pointer semantics for accessing point's x and y members, i.e. point.x instead of point->y.

ReadFile doesn't initialise nobst.

Code:
points_to_line(s,t,&l);
Great use of meaningless variable names. Take a leaf out of the IOCCC's book and rename all your variables to combinations of
underscores, letters O and digits 0.
Code:
points_to_line(_OO,_O0,&_0O);
is considerably more unreadable than this feeble attempt.

ncircles is undefined.

c[i].c is undefined.

point_in_box is undefined.

SO. Abandon this lot, there are so many errors and things wrong with it that rescuing it is going to be harder work than my
approach. For the very last time, as a starting point you should write a program that reads the file and displays what it has found, with
relevant labels. This way you can be sure you've understood the data format. So the data you've given as an example might result
in the following output:
Driver drives from (0.0, 10.0) to (15.0, 0.0)
There are 4 blocks
Block 1 is at (2.0, 7.5) and has radius 1.75
Block 2 is at (3.0, 3.0) and has radius 1.0
Block 3 is at (7.0, 5.0) and has radius 2.0
Block 4 is at (12.0, 1.7) and has radius 1.5

Then consider putting the data into data structures. Use consistent naming, and use proper names not one letter characters. You're not programming
a 1K ZX81 you know, where 1-character variable names were pretty much essential. Then start adding BIT BY BIT, no more than 5-10 lines at a time,
new functionality. Use lots of printfs so the program displays everything it is doing and why, and you'll be able to debug it from the output.
Eventually when it's all written you can start removing the printfs so that it displays the required output only.
sadam's Avatar, Join Date: Apr 2011
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
What are the supposedly wrong messages? why won't you answer my questions directly?
I'm the expert and you're the beginner, remember, so maybe I have a clue what I'm asking...just a thought...

Fact is you have way more than three problems. Your syntax is all over the place.
You need to follow my first suggestion. Write a SMALL amount of code to start. Then
get that code working BEFORE you try to go onto anything new. I'm not going to reply again until you
show evidence of having done that because I'm bored with repeating myself, and you would solve this
problem a whole lot quicker if you listened to me. I know it feels like a retrograde step to throw all
this wonderful code away and start over, but it's not really.

Anyway while I'm here. I'll see what Visual Studio 2010 will do with the code. Get a syntax-highlighting
editor by the way, it will make your life a LOT easier, and ALWAYS ALWAYS ALWAYS strictly indent code.

points_to_line() has too many braces
closest_point() doesn't have enough

By the way always define a return type. The default is int. Return int if that's what you want, otherwise
return void, then it's clear what your intention is.

points_to_line() first argument is wrong. The correct syntax is TYPE<space>VARIABLE.
You have TYPE<dot>VARIABLE.

In distance(), p and q are defined as struct point *, i.e. pointer to struct point, but you're
trying to use local semantics (p.x instead of p->x).

In point_and_slope_to_line(), p is defined as struct point and you're trying to use array semantics
to access x and y, i.e. p[x] instead of p.x. Also I've no idea what p_c[X] is meant to mean. Do you
mean p_c.x? Don't forget C and C++ are case sensitive.

In closest_point, p_c is defined as point, not point*. Remember the difference between pass by copy and pass
by reference. This is pass by copy, so if your aim here is to modify the caller's p_c, you need to change
the definition to point* (and use pointer semantics to access the attributes, e.g. p_c->x.

In distance(), remember that TYPE FUNCTION_NAME(ARGS)<SEMI-COLON> is a function PROTOTYPE, not a function definition.
So
Code:
double distance(struct point *p, struct point *q);
{
is wrong.

Maybe closest_point is missing a brace because you haven't finished writing it yet. It handles vertical and horizontal lines,
but what about the rest?

distance() uses automatic semantics not pointer semantics for accessing point's x and y members, i.e. point.x instead of point->y.

ReadFile doesn't initialise nobst.

Code:
points_to_line(s,t,&l);
Great use of meaningless variable names. Take a leaf out of the IOCCC's book and rename all your variables to combinations of
underscores, letters O and digits 0.
Code:
points_to_line(_OO,_O0,&_0O);
is considerably more unreadable than this feeble attempt.

ncircles is undefined.

c[i].c is undefined.

point_in_box is undefined.

SO. Abandon this lot, there are so many errors and things wrong with it that rescuing it is going to be harder work than my
approach. For the very last time, as a starting point you should write a program that reads the file and displays what it has found, with
relevant labels. This way you can be sure you've understood the data format. So the data you've given as an example might result
in the following output:
Driver drives from (0.0, 10.0) to (15.0, 0.0)
There are 4 blocks
Block 1 is at (2.0, 7.5) and has radius 1.75
Block 2 is at (3.0, 3.0) and has radius 1.0
Block 3 is at (7.0, 5.0) and has radius 2.0
Block 4 is at (12.0, 1.7) and has radius 1.5

Then consider putting the data into data structures. Use consistent naming, and use proper names not one letter characters. You're not programming
a 1K ZX81 you know, where 1-character variable names were pretty much essential. Then start adding BIT BY BIT, no more than 5-10 lines at a time,
new functionality. Use lots of printfs so the program displays everything it is doing and why, and you'll be able to debug it from the output.
Eventually when it's all written you can start removing the printfs so that it displays the required output only.
Can you do the program for me please? or at least some functions?
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Sure thing. My consultancy rates are UKP150/hour, minimum one 8-hour day, payable in advance.
sadam's Avatar, Join Date: Apr 2011
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
Sure thing. My consultancy rates are UKP150/hour, minimum one 8-hour day, payable in advance.
Ok, well let's take the whole program from the beggining. So far it's ok?

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.t>

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 x;
    Point y;
    int nobst;
    ReadFile( "route.dat", &x, &y, &nobst );
}
Are there any other functions that i should include in my program or it's fine?

Last edited by shabbir; 30Apr2011 at 09:04.. Reason: Code blocks
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Well, does it work? You don't need me to look over the code before you can determine that. Compile it, if that works, run it and see what output you get. Is it what you expect? If so then it's probably fine.
sadam's Avatar, Join Date: Apr 2011
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
Well, does it work? You don't need me to look over the code before you can determine that. Compile it, if that works, run it and see what output you get. Is it what you expect? If so then it's probably fine.
well are there any other function i should include in my program or it's fine?'
thank you
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
What output do you get if you run the program?
What output do you expect to get?
sadam's Avatar, Join Date: Apr 2011
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
What output do you get if you run the program?
What output do you expect to get?
i don't get anything yet, i expect to get the distance. so there should be another function called distance?
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
You seem to be using this forum as a substitute for doing your own thinking. Analyse the following answer:

No, you don't need a function called distance. The computer will be able to guess what you want if you just put
Code:
cout << distance;
sadam's Avatar, Join Date: Apr 2011
Go4Expert Member
Quote:
Originally Posted by xpi0t0s View Post
You seem to be using this forum as a substitute for doing your own thinking. Analyse the following answer:

No, you don't need a function called distance. The computer will be able to guess what you want if you just put
Code:
cout << distance;
look man, will you help me or not? every answer you have given me confuses me more!! tell me something in C (and not in C++ as you wrote) or just ignore this program