UP | HOME

Exercise 2 solutions

Table of Contents


Python

e02.py

from math import tan, cos

g = float(raw_input('enter a value for g: '))
v0 = float(raw_input('enter a value for v0: '))
theta = float(raw_input('enter a value for theta: '))
x = float(raw_input('enter a value for x: '))
y0 = float(raw_input('enter a value for y0: '))

y = (x * tan(theta)) - (1.0/(2.0*(v0**2)) * ((g*(x**2))/(cos(theta)**2))) + y0

print "The vertical height of the ball is: ", str(y)

MATLAB / Octave

e02.m

g = input('enter a value for g: ');
v0 = input('enter a value for v0: ');
theta = input('enter a value for theta: ');
x = input('enter a value for x: ');
y0 = input('enter a value for y0: ');

y = (x * tan(theta)) - (1.0/(2.0*(v0^2)) * ((g*(x^2))/(cos(theta)^2))) + y0;

disp(['The vertical height of the ball is: ',num2str(y)]);

R

e02.R

g <- as.numeric(readline("enter a value for g: "))
v0 <- as.numeric(readline("enter a value for v0: "))
theta <- as.numeric(readline("enter a value for theta: "))
x <- as.numeric(readline("enter a value for x: "))
y0 <- as.numeric(readline("enter a value for y0: "))

y <- (x * tan(theta)) - (1.0/(2.0*(v0**2)) * ((g*(x**2))/(cos(theta)**2))) + y0

cat("The vertical height of the ball is:", y,"\n")

C

e02.c

// compile with: gcc -o e02 e02.c

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

int main(int argc, char *argv[]) {

  float g, v0, theta, x, y0, y;
  printf("enter a value for g: ");
  scanf("%f", &g);
  printf("enter a value for v0: ");
  scanf("%f", &v0);
  printf("enter a value for theta: ");
  scanf("%f", &theta);
  printf("enter a value for x: ");
  scanf("%f", &x);
  printf("enter a value for y0: ");
  scanf("%f", &y0);

  y = (x * tan(theta)) - (1.0/(2.0*(v0*v0)) * ((g*(x*x))/(cos(theta)*cos(theta)))) + y0;

  printf("The vertical height of the ball is: %.2f\n", y);

  return 0;
}

Paul Gribble | fall 2014
This work is licensed under a Creative Commons Attribution 4.0 International License
Creative Commons License