Fortran Fortran: determine the horizontal distance of a ball

AI Thread Summary
The discussion focuses on troubleshooting a Fortran program designed to calculate the horizontal distance a ball travels. Key errors identified include the use of the wrong operator for exponentiation, incorrect integer arithmetic for the constant (1/2), and improper variable declarations, particularly for arrays and types. The program also contains an infinite loop that prevents it from functioning correctly after user input. Additionally, the calculation of distance should not require a READ statement since it is computed within the program. Overall, the suggested fixes resolved the initial errors, but further adjustments are necessary for proper execution.
CCguru
Messages
2
Reaction score
0
Im writing a program to determine the horizontal distance of a ball but getting this error:

Y(t)=Y0+VY0*t+(1/2)*g*t^2
1
Error: Unclassifiable statement at (1)

Distance=(X(t))^2+(Y(t))^2
1
Error: Unclassifiable error at (1)



My code is as follows:

PROGRAM as_the_ball_is_thrown

IMPLICIT NONE

INTEGER::Y0, X0, V0, g, theta, t
CHARACTER::Distance, VY0, VX0, X(t), Y(t)


X0=0
Y0=0
V0=20
g=9.81


WRITE(*,*) "Please enter amount of time the ball traveled"
READ(*,*) t

WRITE(*,*) "Please enter the angle to which the ball was thrown"
READ(*,*) theta

DO
Y(t)=Y0+VY0*t+(1/2)*g*t^2
X(t)=X0+VX0*t
VX0=V0*COS(theta*3.14159/180)
VY0=V0*SIN(theta*3.14159/180)
Distance=(X(t))^2+(Y(t))^2
END DO


WRITE(*,*) "The horizontal distance the ball traveled is"
READ(*,*) Distance
WRITE(*,*) "meters"

END PROGRAM as_the_ball_is_thrown



Can anyone tell me why this is wrong?

Thanks!
 
Technology news on Phys.org


^ is not the Fortran operator for "raise to a power". It should be **.

Also (1/2) will be evaluated in integer arithmetic since 1 and 2 are both integers, and the integer division will give 0 which is not what you want. The best fix is to replace (1/2) by 0.5.

Declaring Y0, X0, V0, g, theta, t as INTEGER is probably not a good idea, especially since you then try to set g = 9.81 (which will actually set it to 9).
 


In addition to what AlephZero said, you have problems with your declarations for X(t) and Y(t). For one thing, this is not how you declare arrays. For another thing, why are these character-type variables? That doesn't make any sense.
 


Awesome thanks! That fixed all the errors. Now for some reason the program just stops and doesn't print anything after a person enters the angle. Do you guys know why?
 


There are a couple things wrong.
1) You have what I believe is an infinite loop below where you shouldn't be using a loop at all. OTOH, if you are trying to set the values of an array to certain values, maybe that's what you're trying to do. If that's the case, the following code won't work, because array indexes have to be integer values 1, 2, 3, and so on.

It's hard to tell what you're trying to do, though.
Code:
DO
 Y(t)=Y0+VY0*t+(1/2)*g*t^2
 X(t)=X0+VX0*t
 VX0=V0*COS(theta*3.14159/180)
 VY0=V0*SIN(theta*3.14159/180)
 Distance=(X(t))^2+(Y(t))^2 
END DO

2. Distance is computed, so you don't need to READ it, just WRITE it.
Code:
WRITE(*,*) "The horizontal distance the ball traveled is"
 READ(*,*) Distance 
WRITE(*,*) "meters"
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
8
Views
2K
Replies
12
Views
1K
Replies
4
Views
2K
Back
Top