This is the code and I am supperconfused how to do any of this. /* Local Include
ID: 3683487 • Letter: T
Question
This is the code and I am supperconfused how to do any of this.
/* Local Includes */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <errno.h>
/* Local Defines */
#define DEFAULT_ITERATIONS 1000000
#define RADIUS (RAND_MAX / 2)
/* Local Types */
typedef struct coordinate {
double x;
double y;
} coordinate_t;
/* Local Functions */
double dist(const coordinate_t* pt_a, const coordinate_t* pt_b){
return sqrt(pow((pt_a->x - pt_b->x), 2) + pow((pt_a->y - pt_b->y), 2));
}
double zeroDist(const coordinate_t* other_pt){
/* Local vars */
double d = 0.0;
coordinate_t* zero_pt = NULL;
/* Create new zero point */
zero_pt = malloc(sizeof(*zero_pt));
if(!zero_pt){
fprintf(stderr, "Malloc failed ");
return NAN;
}
zero_pt->x = 0.0;
zero_pt->y = 0.0;
/* Calculate Distance */
d = dist(zero_pt, other_pt);
/* Return distance */
return d;
}
/* Main */
int main(int argc, char* argv[]){
/* Local vars */
long i;
long iterations;
coordinate_t* pt = NULL;
long inCircle = 0;
long inSquare = 0;
double pCircle = 0.0;
double piCalc = 0.0;
/* Process program arguments to select iterations */
if(argc < 2){
/* Set default iterations if not supplied */
iterations = DEFAULT_ITERATIONS;
}
else{
/* Set iterations if supplied */
iterations = atol(argv[1]);
if(iterations < 1){
fprintf(stderr, "Bad iterations value ");
exit(EXIT_FAILURE);
}
}
/* Seed RNG */
srand(time(NULL));
/* Calculate pi using statistical methode across all iterations*/
for(i=0; i<iterations; i++){
/* todo: remember to allocate pt */
pt->x = (rand() % (RADIUS * 2)) - RADIUS;
pt->y = (rand() % (RADIUS * 2)) - RADIUS;
if(zeroDist(pt) < RADIUS){
inCircle++;
}
inSquare++;
}
/* Finish calculation */
pCircle = (double) inCircle / (double) inSquare;
piCalc = pCircle * 4.0;
/* Print result */
fprintf(stdout, "pi = %f ", piCalc);
return 0;
}
What is the problem and how do we fix it? Hint: look at the similar working code in zeroDist().
I have to run the code via GDB.
When you reach the breakpoint, print the current x and y values of the other_pt argument. What GDB commands did you use?
Delete the breakpoint and continue. What GDB commands did you use?
Now I have to run the code via valgrind.How much code is leaking memory? How much? What are the problematic line numbers?
Open the code in an editor. Find the problem lines. What is the problem and how do we fix it?
Use /usr/bin/time to calculate how long it takes ./pi to run. Play with the number of iteration until you have it taking ~1s of real time.Record the number of iterations you are using and how long the program takes to run.
Now generate a callgrind profile by running valgrind --tool=callgrind ./pi <your iterations>
Open the resulting profile output using kcachegrind: e.g.kcachegrind callgrind.out.?????
Explore the information kcachegrind provides. Starting with main(),what are the top 5 places the program spends the largest percentage if its time? What percentage of time does it spend in each place?
Close kcachegrind.
Using the information from 4, can you make the code faster? How?
Run the code again via /usr/bin/time. How does the new runtime compare to the runtime from 1?
Explanation / Answer
To run the above code use likebelow
$ gcc main.c -lm -o main // This will tell gcc to link your code against the math lib. Just be sure to put the flag after the objects you want to link.
invoking gdb
%gdb main.out
To delete braek points basic syntax is
disable [breakpoints] [range...]
To find the memory leakage in the above code is to run in the respective terminal but that is not available now
Basically memory leakahge is where malloc() is used there might be a chance to leakage of memory.
If we use pointers there is alos a chance to the memory leak.
While i compiling the code i got the like below:
this is a loinker problem we can link manullay in the respective IDE's .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.