Our homework is to compare N circles with each other if they are intersecting or containing.My C file for that is :

int N=0 , i=0, j, a=0, max=0;
  float circle[MAX] [3] ;
  float durum1, durum2, durum3;
  float intersection[MAX], containment[MAX];
  
  printf("Please enter the number of circles you want to compare : \n");
  scanf ("%d", &N);
  
  for(i=0; i<N; i++)
  {
  printf ("Enter the values of x, y and r for circle %d: \n", i+1);
  scanf ("%f %f %f", &circle[i] [0],&circle[i] [1],&circle[i] [2] );
}

     /*intersection situation*/
     for(i=0; i<N; i++){
     for (j=i+1; j<N; j++){
         durum1 =sqrt(((circle[i][0]-circle[j][0])*(circle[i][0]-circle[j][0]))+ ((circle[i][1]-circle[j][1])*(circle[i][1]-circle[j][1])));
         durum2 =(circle[i][2]+circle[j][2]);
              
              if ((durum1 <= durum2) && (durum1 != 0)) {
              intersection[i] = intersection[i] + 1;
              intersection[j] = intersection[j] + 1;}
               } }


     /*end of intersection*/
     /*containment situation*/
              for(i=0; i<N; i++){
              for (j=0; j<N; j++){
         durum1 =sqrt(((circle[i][0]-circle[j][0])*(circle[i][0]-circle[j][0]))+ ((circle[i][1]-circle[j][1])*(circle[i][1]-circle[j][1])));
         durum3 =(circle[i][2])-(circle[j][2]);
         if ((durum1 <= durum3) && (i!=j)){
         containment[i]=containment[i] + 1;}
               }
              }
     /*end of containment*/

     /*Print*/
              for(i=0; i<N; i++){
              if (intersection[i]==0.0){printf("Circle #%d has no intersection",i+1);}
              printf("Circle #%d intersects with %0.f circle \n", i+1, intersection[i]);
              }
              for(i=0; i<N; i++){
              if (containment[i]==0.0){printf("Circle #%d has no containment \n",i+1);} else {
              printf("Circle #%d contains %0.f circle \n", i+1, containment[i]);}
              }

     /*End of print*/

At the bottom, in the printing section i wanted it to write some explanations instead of "0" circle text if containment==0.0 or intersection==0.0. As you see below at intersection part it didn't happen. Containment was much more interesting i believe. It did wrote what i wanted but only for last 2 circles. I believe i did something wrong while writing int or float. I need your help immediately. Thank you very much.

Recommended Answers

All 2 Replies

Your 2 variables intersection and containment are float arrays that are never initialised.

You then access them by incrementing them, which in itself is undefined behaviour. Since they where not initialised they had a random initial value so the value they have after the increment is also random.

Finally you use the test == 0.0 that is never going to work with a float type. floats and double contain approximations to values the == and != operator are pretty much guaranteed to give you wrong answers a large proportion of the time. Not only that but I see not reason why you have used floats for these variables.

To sum up use an appropriate, non floating point, type for intersection and containment and initialise them before you use them.

Thank you very much Banfa.. It's done.. =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.