Declare a structure to represent a circle. Each circle stores information about
ID: 3811789 • Letter: D
Question
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_float(double a,double b) {
return ((double)rand()/RAND_MAX)*(b-a)+a;
}
We want all the circles to fit an area of 1000x1000. 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 P I and area of a circle is given as P I 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.969481
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. Random generate circles using the following function. double rand_float (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.969481 Submit your program electronically using the blackboard systemExplanation / Answer
#include<stdio.h>
#define PI 3.14
#define RAND_MAX 35000
typedef struct circle{
double x;
double y;
double radius;
} Circle;
double rand_float(double a,double b) {
return ((double)rand()/RAND_MAX)*(b-a)+a;
}
double getArea(double r) {
return PI * r * r;
}
void main() {
Circle *arr = malloc(sizeof(Circle)*50); //create array
int i, maxIndex;
double maxArea;
for (i=0; i<50; i++) { //poulate array
arr[i].x = rand_float(100,900);
arr[i].y = rand_float(100,900);
arr[i].radius = rand_float(0,100);
}
maxArea = getArea(arr[0].radius);
maxIndex = 0;
for (i=1; i<50; i++) //find max area among all circle
if ( getArea(arr[i].radius) > maxArea) {
maxArea = getArea(arr[i].radius);
maxIndex = i;
}
printf("Circle with largest area (%lf) has center (%lf,%lf) and radius %lf", maxArea, arr[maxIndex].x, arr[maxIndex].y, arr[maxIndex].radius);
free(arr); //prevent memory leak
}
very simple program to meet your need. If you still have any question, please comment below.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.