Hi. My teacher has given my class this assignment:
Try writing a program that simulates the following scenario. A robot is placed at the 50 yard line of a football field. The robot travels at 0.05 yards/second towards one of the end zones (i.e. from 50 yards to zero). Every second, the robot tries to shoot a ball into a soccer goal located in the end zone. However, the robot has a slight flaw, and each ball is shot out between +30◦ and −30◦. All angles in this range are equally probable, and the soccer goal is 3 yards wide. By the time the robot reaches the end zone, how many balls have gone into the goal? Try running your simulation many times with different random number seeds. What can you say about the statistical error in your simluation?
I wrote this code (sorry about the weird tabbing, xming wouldn't let me copy so I had to take it from notepad):
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main() {
int location = 50;
int goals = 0;
int counter = 0;
float angle, width;
float pi = M_PI;
srand48(42);
for(counter = 0; counter < 1000; counter++) {
//find a random angle between -pi/6 and pi/6
angle = ((pi/3) * drand48() - (pi/6));
//find where the ball will land
width = location * tan(angle);
//take the absolute value
width = abs(width);
//see if it made it into the goal
if (width <= 1.5) {
goals ++;
}
//move the robot
location = location - 0.05;
}
printf("Goals: %d\n", goals);
}
I'm getting an error saying that I didn't declare the tan function, but none of the sources I've found online have agreed with how it needs to be declared. Can anybody help? Thank you!