Thread: need help with program...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    74

    ...jump to last reply

    Ok, I'm getting closer. This program should take input from user - x,y points of center of two circles, and the radius. The program determines the relationship of the circles - either Intersection, Separation or Coverage. I have a few errors and think my problems lies with my variable names. Also, one error is "pow" does not take one parameter (haven't used the power much). Please help!!

    #include <iostream.h>
    #include <math.h>

    class circle{

    public:
    double x1, y1, r1, x2, y2, r2;
    void initial();
    int distance (int x, int y);
    double relation (double r1, double r2);
    };

    void circle::initial ()
    {
    cout << "Enter circle 1: " << endl;
    cin >> x1 >> y1 >>r1;
    cout << "Enter circle 2: " << endl;
    cin >> x2 >> y2 >>r2 ;
    }

    double relation(double r1, double r2)
    {
    if (fabs(r1-r2) <= (d) <= (r1+r2))
    cout << "Intersection" << endl;
    else if
    ( d > (r1 + r2))
    cout << "Separation " << endl;
    else
    cout << "Coverage" << endl;
    }

    int circle::distance ( int x, int y)
    {
    double d;
    x= pow(x1 - x2);
    y= pow(y1 - y2);
    d = sqrt (x+y);
    }

    int main()
    {
    circle c1, c2;
    c1.initial() ;
    c2.initial() ;
    cout<<"The relations of c1 and c2 is " << relation(c1,c2)<<endl;
    return 0;
    }
    Last edited by LouB; 06-24-2002 at 09:12 PM.

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    double relation(double r1, double r2)
    {
    if (fabs(r1-r2) <= (d) <= (r1+r2)) //1
    cout << "Intersection" << endl;
    else if
    ( d > (r1 + r2)) //2
    cout << "Separation " << endl;
    else
    cout << "Coverage" << endl;
    }

    int circle::distance ( int x, int y)
    {
    double d; //3
    x= pow(x1 - x2);
    y= pow(y1 - y2);
    d = sqrt (x+y); //4
    }

    there are problems with these 2 funtions.
    1) Try this
    if( (fabs(r1-r2) <= d) && (d <= (r1+r2)) )
    1 and 2) the variable d needs to be declared in the class definition, with the other vars, so it can be accesed.
    3) move this line, as mentioned above
    4) this fuctions returns an int, well it should. try return d at the end

    Make the initial function call distance() after the user inputs the data, so that when relation is called, d contains valid data.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    74
    thanks rpimatty for your help. I'm still having problems, and since I've been at this for about 6 hours and it's 2am, I think I'll call it a night and try again tomorrow. Anyone else have any ideas in the meantime, please reply. Here's what I have now:

    class circle{
    public:
    double x1, y1, r1, x2, y2, r2, d;
    void initial();
    distance (c1, c2);
    double relation (double c1, double c2);
    };

    void circle::initial ()
    {
    cout << "Enter circle 1: " << endl;
    cin >> x1 >> y1 >>r1;
    cout << "Enter circle 2: " << endl;
    cin >> x2 >> y2 >>r2 ;
    }

    double relation(double c1, double c2)
    {
    if ( (fabs(r1-r2) <= d) && (d <= (r1+r2)) )
    cout << "Intersection" << endl;
    else if
    ( d > (r1 + r2))
    cout << "Separation " << endl;
    else
    cout << "Coverage" << endl;
    }

    int distance ( circle c1, circle c2)
    {

    x= pow(x1 - x2);
    y= pow(y1 - y2);
    d = sqrt (x+y);
    return d;
    }

    int main()
    {
    circle c1, c2;
    c1.initial() ;
    c2.initial() ;
    cout<<"The relations of c1 and c2 is " << relation(c1,c2)<<endl;
    return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > double x1, y1, r1, x2, y2, r2, d;
    This is two circles, not one
    double x, y, r;

    > void circle::initial ()
    {
    cout << "Enter circle 1: " << endl;
    cin >> x1 >> y1 >>r1;
    cout << "Enter circle 2: " << endl;
    cin >> x2 >> y2 >>r2 ;
    }

    And this inputs both circles (twice)
    Code:
    void circle::initial ()  { 
        cout << "Enter circle: " << endl; 
        cin >> x >> y >>r; 
    }
    In the class
    > distance (c1, c2);
    > double relation (double c1, double c2);
    distance doesn't have any types, and I'm pretty sure that relation needs to have two circles as input parameters

    > x= pow(x1 - x2);
    Read the manual - pow takes 2 arguments

    Finally, relation() should call distance() before doing any comparisons on d

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    74

    let's try again

    Ok, I'm doing this step by step. It compiles ok, but when running I'm getting an error "unresolved external symbol "public: class circle _ thisclass circle::initial(void)".

    #include <iostream.h>
    #include <math.h>

    class circle{

    public:
    int x, y, r;
    circle initial();
    };

    circle initial()
    {
    circle c1, c2;
    cout << "Enter first circle x point: " ;
    cin >> c1.x;
    cout << "Enter first circle y point:" ;
    cin >> c1.y;
    cout << "Enter first circle radius: ";
    cin >> c1.r;
    cout << "Enter second circle x point: " ;
    cin >> c2.x;
    cout << "Enter second y point:" ;
    cin >> c2.y;
    cout << "Enter second radius: ";
    cin >> c2.r;
    return (c1, c2);
    }

    int main()
    {
    circle c1, c2;
    c1.initial();
    c2.initial();

    cout << c1.x << endl;
    cout << c1.y << endl;
    cout << c1.r << endl;
    cout << c2.x << endl;
    cout << c2.y << endl;
    cout << c2.r << endl;
    return 0;
    }

    It works ok if I do initial(c1,c2) in main, but I want to do with c1.initial(); c2.initial();

    Pls help.
    Thanks
    Last edited by LouB; 06-23-2002 at 08:20 PM.

  6. #6
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You want to make initial a member function of circle than returns nothing.

    Code:
    class circle {
    // data
    public:
        void initial();
    };
    
    // concentrate here.  initial is a member function of circle that returns nothing..
    // should be able to understand that from how the following line looks
    void circle::initial() {
    
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    74
    thanks. I've made those changes, now it is asking me to initialize each circle two times, and it's output is the address of the data, not the data. Any ideas?

  8. #8
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    initial will be responsible for getting input for a single circle.

    So, say you implement Salem's version of initial. Then, in main, you will create two instances of circle, and each of them will be initialed.

    Code:
    int main() {
       circle a, b;
    
       a.initial();  // make a iniatilize itself
       b.initial();  // make b initialize itself
       
       // more stuff
    
       return 0;
    }
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    74
    Thanks Silenstrike. I finally got that figured out. Now I have to work on the other functions, yikes!!

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    74
    Ok, this is what I have now. I'm getting errors that r1, r2 and d in the relation function are not defined, and my distance output is gargabe. Please help!

    #include <iostream.h>
    #include <math.h>

    class circle{
    public:
    double x, y, r, d;
    void initial();
    void distance(circle& c1, circle& c2);
    friend circle relation(circle& c1, circle& c2);
    };

    void circle::initial()
    {
    cout << "Enter the (x,y) coordinates of the circle center, and the radius: " ;
    cin >> x >> y >> r;
    }

    void circle::distance(circle& c1, circle& c2)
    {
    c1.d = pow((c1.x - c2.x),2);
    c2.d = pow((c1.y - c2.y),2);
    cout << c1.d << c2.d;
    }

    circle relation (circle& c1, circle& c2)
    {
    if( (fabs(r1-r2) <= d) && (d <= (r1 + r2)) )
    cout << "Intersection" << endl;
    else if
    (d > (r1 + r2))
    cout << "Separation" << endl;
    else
    cout << "Coverage" << endl;
    }
    int main()
    {
    circle c1, c2;

    c1.initial();
    c2.initial();

    // cout << "The relations of c1 and c2 is " << relation (c1,c2)<<endl;
    return 0;
    }

  11. #11
    Registered User
    Join Date
    May 2002
    Posts
    74
    New version -- now getting "d" undeclared identified in relation, and no operator defined which takes right hand operand of type class circle in main function.

    class circle{
    public:
    double x, y, r, d;
    void initial();
    distance(circle& c1, circle& c2);
    friend circle relation(circle c1, circle c2);
    };

    void circle::initial()
    {
    cout << "Enter the (x,y) coordinates of the circle center, and the radius: " ;
    cin >> x >> y >> r;

    }

    circle::distance(circle& c1, circle& c2)
    {
    c1.d = pow((c1.x - c2.x),2);
    c2.d = pow((c1.y - c2.y),2);
    d=sqrt(c1.d = c2.d);
    }

    circle relation (circle c1, circle c2)
    {
    if( (fabs(c1.r-c2.r) <= d) && (d <= (c1.r + c2.r)) )
    cout << "Intersection" << endl;
    else if
    (d > (c1.r + c2.r))
    cout << "Separation" << endl;
    else
    cout << "Coverage" << endl;
    }
    int main()
    {
    circle c1, c2;

    c1.initial();
    c2.initial();
    cout << "The relations of c1 and c2 is " << relation(c1,c2)<<endl;
    return 0;
    }

  12. #12
    Registered User
    Join Date
    May 2002
    Posts
    74
    Please help Still getting errors "d" undeclared identified in relation, and no operator defined which takes right hand operand of type class circle in main function on the cout line. Also, I know my relation should return strings and not cout statements, but not sure how to do that. The main function cannot be changed. Thanks!!

    #include <iostream.h>
    #include <math.h>
    #include <string>
    using namespace std;

    class circle{
    public:
    double x, y, r, d;
    void initial();
    distance(circle& c1, circle& c2);
    void print();
    friend circle relation(circle& c1, circle& c2);
    };

    void circle::initial()
    {
    cout << "Enter the (x,y) coordinates of the circle center, and the radius: " ;
    cin >> x >> y >> r;
    }

    circle::distance(circle& c1, circle& c2)
    {
    x = pow((c1.x - c2.x),2);
    y = pow((c1.y - c2.y),2);
    d=sqrt(x+y);
    }

    circle relation (circle& c1, circle& c2)
    {

    if( (fabs(c1.r-c2.r) <= d) && (d <= (c1.r + c2.r)) )
    cout << "Intersection"<< endl;
    else if
    (d > (c1.r + c2.r))
    cout << "Separation" << endl;
    else
    cout << "Coverage" << endl;

    }
    int main()
    {
    circle c1, c2;
    c1.initial();
    c2.initial();
    cout << "The relations of c1 and c2 is " << relation(c1,c2)<<endl;
    return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM