Question
I need help with C, please don't copy other answers!!
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 cirele 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 )/RAID-MAX) (b-a) a; We want all the eireles to fit an area of 1000x1000. So, x and y coordinates are randomly selected from (100,000) and radius is randomly Neleeted from (0,100) using above function. After you insert the random eireles, find the eirele with the largest area and print the information for this circle. You can use 3.14 for PI and aren of a circle is given as PI e 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 vith 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

Dear Student,

Here is the program..

NOTE: Please note that the following program has been tested on ubuntu 14.04 system and compiled under gcc compiler.

-------------------------------------------------------------------------------------------------------------------------------------

program...

------------------------------------------------------------------------------------------------------------------------------------

//Header files DEclaration

#include<stdio.h>

#include<stdlib.h>

#include<math.h>

//Structure Declarartion

struct Circle

{

//structure member declarartion
double x; //x coordinate

double y; //y coordinate

double r; //radius

};

//function Prototype Declaration

double rand_float(double a, double b);


//starting of main function

int main()

{

//Varibale Data Type declaration

int i;

double Area[100];

double PI = 3.14, x, y, max, r;

struct Circle *C;

//Memory allocation for 50 array

C = (double*)malloc(sizeof(struct Circle) * 50);

//Call to rand_float function 50 times to obtain the value of r, x, y

for(i=1;i<=50;i++)

{

C[i].x = rand_float(100, 900);

C[i].y = rand_float(100, 900);

C[i].r = rand_float(0, 100);

//Calculation of Area;

Area[i] = PI* pow(C[i].r, 2);

}

//Display all dimensions for 50 circles and the resultant area

for(i=1;i<=50;i++)

{

printf("CIrcle %d, Center: x = %lf, y = %lf, radius = %lf, Area = %lf\n",i, C[i].x, C[i].y, C[i].r, Area[i]);

}

//finding Maximum Area;

for(i = 1; i<=50;i++)

{

if(Area[1] > Area[i])

{

max = Area[i];

x = C[i].x;

y = C[i].y;

r = C[i].r;

}

}

printf("\n");

//Display Max area

printf("Circle withe largest area (%lf) has center (%lf, %lf) and radius %lf\n",max, x, y, r);

printf("\n");

free(C);

return 0;

}

//function defination to return the x, y, r;

double rand_float(double a, double b)

{

return ((double)rand()/RAND_MAX) * (b-a) + a;

}


---------------------------------------------------------------------------------------------------------------------------------------

Here i have attached the output of the program as a screen shot...

Output:

-------------------------------------------------------------------------------------------------------------------------------------

nirnalsharma@ubuntu:-/HomeworkLib_solutions$./a.out Circle 1, Center: x 772.150174, y 415.506341, radius = 78.309922, Area = 19255.

circle 41, center: x = 182.536951, y = 200.860271, radius = 49.544407, Area = 7707.595446 Circle 42, Center: x 788 . 380183,

----------------------------------------------------------------------------------------------------------------------------------------------

To check the memory leak we use the valgrind command.

below command i have used to check the memory leak in my case..

valgrind --tool=memcheck ./a.out          (here ./a.out is the executable file)

here i have attached the output of the valgrind command. In the last screen shot you can see that no memory leak found.

----------------------------------------------------------------------------------------------------------------------------------

nirmalsharma@ubuntu:-/HomeworkLib solutionss valgrind tool-mencheck./a.out 3550 Mencheck, a menory error detector 3550Copyright (C)

Circle 12, Center: x 336 . 825294, y 610.041814, radius = 52.428719, Area 8631.139611 Circle 13, Center: x 494.866390, y 878.

==3550== at 0x4C2AB80 : malloc (in /usr/lib/valgrind/vgpreload-memcheck-and64-linux . so) 3550 by 0x400783: main (in /hone/ni

------------------------------------------------------------------------------------------------------------------------------------------

Kindly Check and Verify Thanks...!!!

Add a comment
Know the answer?
Add Answer to:
I need help with C, please don't copy other answers!! Thanks Declare a structure to represent...
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
  • Please help with C Thanks Declare a structure to represent a circle. Each circle stores information...

    Please help with C Thanks 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 cash array eatery. 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...

  • 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,...

  • Please need help, programming in C - Part A You will need to create a struct...

    Please need help, programming in C - Part A You will need to create a struct called Team that contains a string buffer for the team name. After you've defined 8 teams, you will place pointers to all 8 into an array called leaguel], defined the following way: Team leaguel8]. This must be an aray of pointers to teams, not an array of Teams Write a function called game) that takes pointers to two teams, then randomly and numerically determines...

  • c++ Please help! i keep getting an error message. I think my main problem is not...

    c++ Please help! i keep getting an error message. I think my main problem is not understanding the circle calculations function and how both the area and the circumference is returned from the function. I know & needs to be used, but I am unsure how to use it. Copy the following code and run it. You should break it into the following 3 functions getValidinput - which asks the user to enter the radius and then make sure that...

  • Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solu...

    Write a C++ program for the instructions below. Please read the instructions carefully and make sure they are followed correctly.   please put comment with code! and please do not just copy other solutions. Instructions 1. Read instructions carefully! 2. Use C++ syntax only, C syntax will not be accepted. 3. Always use braces to define blocks. 4. Indent all lines within a block. Each block requires one more tab. 5. Organize your code well with proper formatting and a single...

  • Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers...

    Hello I need help with this program. Should programmed in C! Program 2: Sorting with Pointers Sometimes we're given an array of data that we need to be able to view in sorted order while leaving the original order unchanged. In such cases we could sort the data set, but then we would lose the information contained in the original order. We need a better solution. One solution might be to create a duplicate of the data set, perhaps make...

  • 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...

  • I have updated my previously posted C++ question to ensure that I have included all of...

    I have updated my previously posted C++ question to ensure that I have included all of the necessary documentation in order to complete Part 2 - Bank Account of the assigned lab in its entirety. Please note that the lab below is somewhat lengthy, and I do not expect you to answer each question! If coding the full lab is too much to ask, please focus soley on Part 2 of the lab, titled Bank Account and the subsections that...

  • I need to implement a program that requests a x,y point and the radius of a...

    I need to implement a program that requests a x,y point and the radius of a circle. then it figures out the area and circumference using an overloaded operation. The full instructions, what I have and the errors I have are below. A point in the x-y plane is represented by its x-coordinate and y-coordinate. Design a class, pointType, that can store and process a point in the x-y plane. You should then perform operations on the point, such as...

  • I NEED ALL QUESTIONS OR ELSE THUMBS DOWN! C++ Implement a structure Employee. An employee has...

    I NEED ALL QUESTIONS OR ELSE THUMBS DOWN! C++ Implement a structure Employee. An employee has a name (a char *) and a salary (a double). Write a default constructor, a constructor with two parameters (name and salary), and methods char* getName() double getSalary() to return the name and salary. Write a small global function TestEmployee() to test your structure. Creating a new employee. Please type the name: Larry Bird Please specify the salary: 200000 New employee has been created....

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