0

I have a array of 50 pointers that point to a circle struct that contains the x and y center coordinates, and the radius for a circle. I allocated all of the memory and used rand_float to create random x, y, and z for the circles. The point to my program is the find the circle with the largest area and print it out. I am experiencing problems with my program, I understand that with rand results will not be the same every time but my values are no where near the intended output. I am also not seeing the printf output from largestcircle in the output. Lastly I receive a error when I try running the program with free(circleptr).

#include <stdio.h>
#include <stdlib.h>
#define PI 3.14
double rand_float(double a,double b){        //function provided my teacher.
    return ((double)rand()/RAND_MAX)*(b-a)+a;  
}
struct circle{
    double x;
    double y;
    double z;
};
 void largestcircle(struct circle **circleptr){
    float max = 0, radius =0, x= 0, y=0;
    int i;
    for(i = 0; i<50; i++){
        if(circleptr[i]->z *circleptr[i] ->z *PI > max){
            max = circleptr[i]->z*2*PI;
            radius = circleptr[i]->z;
            x = circleptr[i] ->x;
            y = circleptr[i] ->y;
        }
    }
    printf("Circle with largest area (%f) has center (%f, %f) and radius %f\n", max,x,y,radius);
}
int main(void) {
    struct circle *circleptr[50];
                             //dynamically allocate memory to store a circle
    int i;
    for(i=0; i<50; i++){
        circleptr[i] = (struct circle*)malloc(sizeof(struct circle));
    }
                             //randomly generate circles
    for(i=0; i<50; i++){//x
        circleptr[i]->x = rand_float(100, 900);
        circleptr[i]->y = rand_float(100, 900);
        circleptr[i]->z = rand_float(0, 100);
        //printf("%11f %11f %11f \n", circleptr[i] ->x, circleptr[i]->y, circleptr[i]->z);
    }
    largestcircle(circleptr);
    for(i=0; i<50; i++){
        free(circleptr[i]);
    }
    return 0;
}

the output should look something like:

Circle with largest area (31380.837301) has center (774.922941,897.436445) and radius 99.969481

My current x y and z values look like:

1885193628 -622124880 -622124884 
1885193628 -622124868 -622124872 
1885193628 -622124856 -622124860 
1885193628 -622124844 -622124848 
1885193628 -622124832 -622124836 
1885193628 -622124820 -622124824 
1885193628 -622124808 -622124812 
1885193628 -622124796 -622124800 
1885193628 -622124784 -622124788 
1885193628 -622124772 -622124776......etc.

Thoughts?

below_avg_st
  • 187
  • 15
  • You don't call `largestcircle`. So how do you expect to see that printf? – Arash Apr 11 '17 at 04:18
  • 1
    `max = circleptr[i]->z*2*PI;` That's not the formula for the area of a circle. Should be `max = circleptr[i]->z*circleptr[i]->z*PI;`. And suggest you use better variable names than `z` - `radius` would make much more sense. – kaylum Apr 11 '17 at 04:20
  • 1
    `free(circleptr)` Of course you can't do that. Where do you have `circleptr = malloc()`? Nowhere. So if you didn't allocate that pointer you can't free it. Try a loop with `free(circleptr[i])`. – kaylum Apr 11 '17 at 04:23
  • 4
    I don't even see a need to calculate the area of the circle, the one with the maximum radius will also have the maximum area – Nic Apr 11 '17 at 04:28
  • Note that the area of a circle is proportional to the square of the radius, but you only need to compare radii since the larger radius corresponds to the larger area, regardless of where the circle is located on the plane. It's curious you used `z` instead of `r` for the radius; the latter makes it look like you're playing with 3D coordinates. – Jonathan Leffler Apr 11 '17 at 04:53
  • @below_avg_st, don't worry, that will happen again a number of times along the learning curve, and not just to you I might add... – David C. Rankin Apr 11 '17 at 05:05

3 Answers3

1

The problem where you are seeing some garbage random values for circle->x, y and z is is with your format specifier, while you are printing the circle->x, circle->y and circle->z. Use %f instead of %d since you are using double and not an int.

printf("%d %d %d \n", circleptr[i]->x, circleptr[i]->y, circleptr[i]->z);

Change above to

printf("%f %f %f \n", circleptr[i]->x, circleptr[i]->y, circleptr[i]->z);
Sumit Trehan
  • 3,985
  • 3
  • 27
  • 42
  • 1
    To be syntactically correct, it should be `"%lf %lf %lf \n"` (for `double` not `float`). – David C. Rankin Apr 11 '17 at 05:03
  • 1
    @DavidC.Rankin-- `%lf` in `scanf()` format strings, but `%f` in `printf()` format strings for `double` arguments. It is legal, but the `l` modifier has no effect on `%f`. – ad absurdum Apr 11 '17 at 05:26
0

Here is your problem, you re using %d, use %llf

printf("%llf %llf %llf \n", circleptr[i] ->x, circleptr[i]->y, circleptr[i]->z);
printf("Circle with largest area (%llf) has center (%llf, %llf) and radius %llf\n", max, x, y, radius);

also you have a bit missclick here I instead of i while free memory

bobra
  • 615
  • 3
  • 18
  • 1
    `%llf` is not a valid conversion specifier for `printf()` format strings. The `ll` modifier is for `long long` integer types. – ad absurdum Apr 11 '17 at 05:30
0

There is a problem with this function:

 void largestcircle(struct circle **circleptr){
     float max = 0, radius =0, x= 0, y=0;
     int i;

     for(i = 0; i<50; i++){
         if(circleptr[i]->z *circleptr[i] ->z *PI > max){
             max = circleptr[i]->z*2*PI;
             radius = circleptr[i]->z;
             x = circleptr[i] ->x;
             y = circleptr[i] ->y;
         }
      }
      printf("Circle with largest area (%f) has center (%f, %f) and radius %f\n", max,x,y,radius);
  }

The if() statement compared for the area of a circle which is pie * r * r. But within if(), max is set to circumference of a circle which is 2 * pie * r.

This will give you the wrong answer.

Nguai al
  • 958
  • 5
  • 15