简体   繁体   中英

How come for my distance i always get less than 5?

How come when I run my code it always gives me the distance as less than 6

Initialize the program by setting the XY coordinates of Flavia to (0,0) and the current distance from center to zero. Initialize the random number generator.

Ask the user for the max number of moves.

Then the main loop iterates until max moves has been reached or the distance from center exceeds the web radius.

Each iteration of the loop picks a random distance of up to 5.0 cm and a random direction of 0 to 360 degrees. From these random values, calculate the change in X and the change in Y. Add these to the current X and Y position.

After the loop terminates, determine if Flavia escaped or was eaten.

Note that as described, each move puts Flavia at a new location in a circle centered on her old location. This is not the same as directly picking a random change in X and a random change in Y.

You will need: /* return random double [0.0, 1.0) / double randDouble(); / convert input polar coordinates to output rectangular coordinates */ void polarToRect( double radius, double theta, double *x, double *y );

Use the randDouble() from the C-Puzzles. Write your own polarToRect().

This is required because part of the purpose it to practice using pointers with functions.

Calculate distance from the center of the web using the Pythagorean Theorem.

You don't need to write a function for this.

Initialize the random number generator with srand() and the current time.

Start your source file with a block of comments that gives the author and date and describes briefly what the program does.

For each function, describe briefly what it does. Include some one line comments in the body of main() that annotate the logic.

As always, avoid mixed tabs and spaces.

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

void polarToRect(double radius, double theta, double*x, double*y){
    *x = radius*cos(theta);
    *y = radius*sin(theta);
}

/* returns random double cordinates */
double randRadius(double max){
    return max* ((double)rand()/RAND_MAX);
}

double randTheta(double max){
    return max*((double)rand()/RAND_MAX);
}

int main(){
    double x = 0;
    double y = 0;
    double distance;
    double WebRadius = 10;
    double r1, t2;
    srand(time(NULL));
    double tmax=360.0;
    double rmax=5.0;

    printf("\nBug starts at (%lf, %lf)", x,y);

    int moves;
    printf("\nNumber of moves Flavia can make: ");
    scanf("%d", &moves);
    for(; moves>0; moves--){

        r1 = randRadius(rmax);
        t2 = randTheta(tmax);

        polarToRect(r1, t2, &x, &y);
        distance = sqrt((x*x) + (y*y));
        printf("\nThe bug move to: (%.2lf,%.2lf), Distance = %.2lf", x,y,distance);
    }

    if(distance > WebRadius){
        printf("\nOh no! Flavia escapes.");
    }
    else{
        printf("\nYum! Spider ate Flavia.");
    }
    return 0;

}

you should replace double tmax=360.0; by double tmax = (2.0*3.141592653589793238462643383279); because cos and sin work in radians.

then you to follow specification the function should really be

void polarToRect(double radius, double theta, double*x, double*y){
    *x += radius*cos(theta);
    *y += radius*sin(theta);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM