Question
Please help with C
Thanks
ooooo T-Mobile LTE 8:12 PM Please help with Cand thanks 1. (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 dymamically 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)randO/RAND.MA (b-a)+a X) 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 After you insert the random circles, find the circle with the largest area and print the information for this circle. You can use 314 for PI and area of a circle is given as PI. radius. radius. Dont 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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C code
#include <stdio.h>
#include <stdlib.h>
#define PI 3.14


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) \nhas center (%lf,%lf) and radius %lf\n", maxArea, arr[maxIndex].x, arr[maxIndex].y, arr[maxIndex].radius);
free(arr); //prevent memory leak
}

/*
sample output:
Circle with largest area (29386.402844)
has center (686.123529,625.250922) and radius 96.740514

*/

Add a comment
Know the answer?
Add Answer to:
Please help with C Thanks Declare a structure to represent a circle. Each circle stores information...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with C, please don't copy other answers!! Thanks Declare a structure to represent...

    I need help with C, please don't copy other answers!! Thanks Declare a structure to represent a circle. Each circle stores information about the center and the radius. Center is represents with x and y coordinates. Both center and radius uses doubles. Allocate an array of 502 circle pointers and dynamically allocated 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) *...

  • please answer the second part first part answered. the answer to the first part is at...

    please answer the second part first part answered. the answer to the first part is at the bottom bring the rest of the place HAVE TO BE SOLVED WİTH C PROGRAMMİNG A programming task is given in below (i.e., you are supposed to write a single program involving all 5 tasks in below). You should write a C program and upload your c/cpp file . Students are allowed to use MAK104E course (powerpoint) slights. 1) Three geometric entities (a circle,...

  • Using separate files, write the definition for a class called Point and a class called Circle. Th...

    Using separate files, write the definition for a class called Point and a class called Circle. The class point has the data members x (double) and y (double) for the x and y coordinates. The class has the following member functions: a) void set_x(double) to set the x data member b) void set_y(double) to set the y data member c) double get_x() to return the the x data member d) double get_y() to return the the y data member e)...

  • Please use C++. Write a class named Circle that has a double data member named radius...

    Please use C++. Write a class named Circle that has a double data member named radius and a static double data member named maxRadius. It should have a default constructor that initializes the radius to 1.0. It should have a constructor that takes a double and uses it to initialize the radius. It should have a method called calcArea that returns the area of the Circle (use 3.14159 for pi). It should have a static set method for the maxRadius....

  • Please write below code in C++ using Visual Studio. Write program that uses a class template...

    Please write below code in C++ using Visual Studio. Write program that uses a class template to create a set of items. The program should: 1. add items to the set (there shouldn't be any duplicates) • Example: if your codes is adding three integers, 10, 5, 10, then your program will add only two values 10 and 5 • Hint: Use vectors and vector functions to store the set of items 2. Get the number of items in the...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • Please help, provide source code in c++(photos attached) develop a class called Student with the following protected...

    Please help, provide source code in c++(photos attached) develop a class called Student with the following protected data: name (string), id (string), units (int), gpa (double). Public functions include: default constructor, copy constructor, overloaded = operator, destructor, read() to read data into it, print() to print its data, get_name() which returns name and get_gpa() which returns the gpa. Derive a class called StuWorker (for student worker) from Student with the following added data: hours (int for hours assigned per week),...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT