Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Declare a structure to represent a circle. Each circle stores information about

ID: 3814535 • 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 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

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>

struct center
{
   double x;
   double y;
};

struct circle
{
   struct center ctr;
   double radius;
};

/* Method Provided in Question to Use*/
double rand_float(double a, double b)
{
   return ( (double) rand() / RAND_MAX ) *(b-a) + a;
}

double getArea(double radius)
{
   return 3.14 * radius * radius;
}

main()
{
   int i;
   struct circle *circles[100];

   /* Initialize the radius and center for 100 circle*/
   for ( i = 0 ; i< 100 ;i++)
   {
       circles[i] = (struct circle *) malloc(sizeof(struct circle));
       struct center ctr = {rand_float(100, 900),rand_float(100, 900)};
       circles[i] -> ctr = ctr;
       circles[i] -> radius = rand_float(0, 100);
   }

   // find the circle with largest area
   struct circle *temp = circles[0];

   for( i = 1; i< 100 ;i++)
   {
       if (getArea(temp->radius) < getArea(circles[i] -> radius))
       {
           temp = circles[i];
       }
   }

   //print the result
   struct center tempctr = temp->ctr;
   printf("Circle with largest area (%lf) has center (%lf,%lf) and radius %lf ",
       getArea(temp->radius), tempctr.x, tempctr.y, temp->radius);

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote