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?