0

I was just wondering what I'm doing wrong here? The errors are mostly from my first function, am I calling it wrong?

typedef struct{ //typedef and function prototype
     int x, y, radius;
}circle;
int intersect(circle c1, circle c2);

part of the main function that I need for my function

circle c1 = {5, 6, 3.2};
circle c2 = {6, 8, 1.2};

returns 1 if its two circle arguments intersect. How do I call the arrays using struct properly? I keep getting errors

int intersect(circle c1, circle c2){

    float cx, cy, r, distance;
    cx = (circle c1[0].x - circle c2[0].x) * (circle c1[0].x - circle c2[0].x);
    cy = (circle c1[1].x - circle c2[1].x) * (circle c1[1].x - circle c2[1].x);
    r = (circle c1[2].x - circle c2[2].x);
    distance = sqrt(cx + cy);
    if (r <= distance){
        return 1;
    }else{
    return 0;
    }
}

I'm preparing for finals, so help will be appreciated

2
  • 4
    What is circle c1[0].x meaning to you? It is not a valid C expression. And I don't understand why you are adding index operators like [1] or [2] ... Time to read a good C programming book. Commented Apr 19, 2013 at 5:31
  • 1
    Also, your radius field should be float given how you want to initialize your variables Commented Apr 19, 2013 at 5:41

1 Answer 1

2

There are no arrays in your code, so don't try using array notation. Also, don't declare local variables that have the same names as the function parameters.

int intersect(circle c1, circle c2)
{
    float dx, dy, r, distance;
    dx = (c1.x - c2.x) * (c1.x - c2.x);
    dy = (c1.y - c2.y) * (c1.y - c2.y);  // x changed to y throughout
    r  = (c1.r + c2.r);                  // rewritten too
    distance = sqrt(cx + cy);
    if (r <= distance)
        return 1;
    else
        return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.