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

In c language (100 pts) Declare a structure to represent a circle. Each circle s

ID: 3815773 • Letter: I

Question


In c language (100 pts) 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) We all the circles to fit an area of 1000x1000. So, x nd 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>
#define PI 3.14

typedef struct {
double x;
double y;
double radius;
double area;
}circle;


double rand_float(double a, double b)
{
return ((double)rand()/RAND_MAX)*(b-a)+a;
}


circle* findLargest(circle *circleptr[], int n)
{
circle *largest = circleptr[0];
int i;
for(i = 1; i < n; i++)
{
if (largest->area < circleptr[i]->area)
{
largest = circleptr[i];
}
}
return largest;
}

int main()
{
// constant for number of circle;
int circleCount = 50;
circle *circleptr[circleCount];

int i;
for(i = 0; i < circleCount; i++)
{
circleptr[i] = (circle*)malloc(sizeof(circle));
circleptr[i]->x = rand_float(100,900);
circleptr[i]->y = rand_float(100,900);
circleptr[i]->radius = rand_float(0,100);
circleptr[i]->area = PI*(circleptr[i]->radius)*(circleptr[i]->radius);
}

circle *largest = findLargest(circleptr, circleCount);
printf("Circle with largest area (%f) has center (%f, %f) and radius %f ", largest->area, largest->x, largest->y, largest->radius);

for(i = 0; i < circleCount; i++)
{
free(circleptr[i]);
}

return 0;
}

Sample outout

Circle with largest area (29386.402844) has center (686.123529, 625.250922) and radius 96.740514

Output of valgrind

==136413== Command: ./a.out
==136413==
Circle with largest area (29386.402844) has center (686.123529, 625.250922) and radius 96.740514
==136413==
==136413== HEAP SUMMARY:
==136413== in use at exit: 0 bytes in 0 blocks
==136413== total heap usage: 50 allocs, 50 frees, 1,600 bytes allocated
==136413==
==136413== All heap blocks were freed -- no leaks are possible
==136413==
==136413== For counts of detected and suppressed errors, rerun with: -v
==136413== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Please rate positively if this solved your question.

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