Please use C and please add notes to explain what is happening! Thank you! Decla
ID: 3816136 • Letter: P
Question
Please use C and please add notes to explain what is happening! Thank you!
Declare a structure to represent a circle. Each circle stores information about the center and the radius. Center is represented with x and y coordinates. Both center and radius uses doubles. Allocate an array of 50 circle pointers and dynamically allocate memory to store a circle pointed by each array entry. Randomly generate circles using the following function. double rand_floats (double a, double b) {return ((double)rand ()/RAND MAX) * (b-a) +a;} We want all the circles to fit an area of 1000 times 1000. So, x and y coordinates are randomly selected from (100, 900 and radius is randomly selected from (0, 100) using above function. After you insert the random circles, find the circle with the largest area and print the information for this circle. You can use 3.14 for PI and area of a circle is given as PI * radius * radius. Don't forget to free the pointers when you are done with the array. Use valgrind to find any memory leaks. Sample execution is given below Circle with largest area (31380.837301) has center (774.922941, 897.436445) and radius 99.969481Explanation / Answer
Below is a C program for given problem which consists of following structures and functions:
File: circle.c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Point {
double x;
double y;
} Point;
typedef struct Circle {
Point center;
double radius;
} Circle;
#define NUM_CIRCLES 50
struct Circle *circles_array[NUM_CIRCLES];
double rand_float(double a, double b) {
return ((double) rand() / RAND_MAX) * (b-a) + a;
}
void create_circles() {
double x;
double y;
double radius;
srand(time(NULL));
for (int i = 0; i < NUM_CIRCLES; i++) {
x = rand_float(100, 900);
y = rand_float(100, 900);
radius = rand_float(0, 100);
circles_array[i] = malloc(sizeof(Circle));
circles_array[i]->center.x = x;
circles_array[i]->center.y = y;
circles_array[i]->radius = radius;
}
}
void destroy_circles() {
for (int i = 0; i < NUM_CIRCLES; i++) {
free(circles_array[i]);
}
}
double get_circle_area(Circle *circle) {
const double PI = 3.14;
return (PI * circle->radius * circle->radius);
}
Circle *find_max_area_circle() {
Circle *maxAreaCircle = circles_array[0];
double maxArea = get_circle_area(maxAreaCircle);
double area;
for (int i = 1; i < NUM_CIRCLES; i++) {
area = get_circle_area(circles_array[i]);
if (area > maxArea) {
maxArea = area;
maxAreaCircle = circles_array[i];
}
}
return maxAreaCircle;
}
int main() {
create_circles();
Circle *maxAreaCircle = find_max_area_circle();
printf("Circle with largest area (%lf) has center (%lf,%lf) and radius %lf ",
get_circle_area(maxAreaCircle), maxAreaCircle->center.x, maxAreaCircle->center.y, maxAreaCircle->radius);
destroy_circles();
}
Compilation: gcc circle.c
Sample Execution: ./a.out
Circle with largest area (29882.658445) has
center (760.047205,500.181478) and radius 97.553935