Having trouble with an equation!

So I have been teaching myself C++ for 2 weeks now since they don't offer a programming class at my highschool. I have been able to figure out most of my problems up until now :(

I want to create a program that will compute simple kinematic equations from phyics. I believe my math is correct, but C++ isn't understanding what I am trying to tell it to do.

I have :


#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])


float Vi;
float Vf;
float d;
float a;
float t;

// I want to solve the equation Vf^2 = Vi^2 - 2 (A)(D)
// ( Final velocity squared = initial velocity squared minus 2(acceleration) distance)
// so I simplified the equation so I don't have to use exponents.
// this is what I told C++ to do

float a = ( vf * vf ) - ( Vi * Vi ) / (2 * d ) ;
cout << a ;

// For the values (Vi = 1)(Vf = 5)(D = 10) I get an answer of 24.8
// This is obviously wrong :(
// P.S. C++ gets the correct values assigned for vf, vi, and d I know
// because I have error checking in the code that tells me what
// the value of the variable is after I have some one enter it.
// I can provide the whole source code to anyone who is seriously interested.

Thanks for helpin a newb!
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])

{
string exit;
float Vi;
float Vf;
float d;
float a;
float t;
int variable1;
int variable2;
int variable3;

cout << "Hello, and welcome to the kinematics equation solver program!\n";
cout << "This program was made and developed by (my name )! Enjoy!\n\n\n";
cout << "Let's get started! What three variable do you currently know from the problem?\n";
cout << "Tell me one variable at a time! Type the number to the corresponding variable.\n";
cout << " 1.) Initial Velocity \n 2.) Final Velocity\n 3.) Distance\n 4.) Acceleration\n 5.) Time\n";
cin >> variable1;
if ( variable1 == 1 )
{
cout << "Good, so you have the initial velocity.\n Please tell me what the initial velocity is now\n";
cin >> Vi;
cout << "Okay, so the initial velocity is\n"; cout << Vi;
cout << "\nNow please give me the next variable\n";
cout << " 1.) Final Velocity\n 2.) Distance\n 3.) Acceleration\n 4.) Time\n";
cin >> variable2;

if ( variable2 == 1 )

cout << "Okay, good so you also have the final velocity\n";
cout << "Please enter what the final velocity is now\n";
cin >> Vf;
cout << "Okay, so the final velocity is\n";
cout << Vf;
cout << "\nNow please give me the next variable\n";
cout << " 1.) Distance\n 2.) Acceleration\n 3.) Time\n";
cin >> variable3;

if ( variable3 == 1 )
cout << "Okay, so you have initial velocity, final velocity, and distance.\n";
cout << "Please enter the distance now\n";
cin >> d ;
cout << "\n Okay, so your distance is" << d;
cout << "\nWe will now find the remaining two variables together!\n";
cout << "But always remember your kinematic equations!\n";
cout << "For this problem we will use the two equations\n Vf^2 = Vi^2 + 2ad\n Vf = Vi + at\n";
float a = Vf * Vf - Vi * Vi / d * 2;
cout << "Your acceleration is"; cout << a ;
cout << "\nYour Time is"; cout << Vf - Vi / a ;
cout << "\n Thank you for using my program, and I hope this helped!\n";
cout << "Never forget these important equations and GO PHYSICS!";
cin >> exit;
}

}
Last edited on
Thought I would post the source code for ya guys, ty again
Hey TheOne tell me when you've fixed the problem. I'm trying to figure it out too but nothing I've tried had worked yet.
Hey, plug this one in instead. it works.

float a = ((Vf * Vf) - (Vi * Vi)) / (d * 2);
Yeah, with the mathetmatic problems, you should be extra careful. To make sure what you want it to do just add extra perenteses.

But, I still don't know why float a = (Vf * Vf) - (Vi * Vi) / (d * 2); would not work!!!

I don't get where the 24.8 came from. I know that 25 must subract .2 to make it 24.8 but

how can (Vi * Vi)) / (d * 2) in any way = .2 ???

so we will get 1 / 20. thats not .2

We get 1/10 x 1 x 2. That's not .2. WAIT, THERE WE GO. OMG, How did I not see this?
I went over that exact problem but I had it equal to something else. Jesus. !!

So, I guess without the Parenthesis, it would just do it from right to left cuz Multiplaction and division have the same equality of who comes first. But, im still pissed cuz i went over that and I got something else!
Yeah the ((Vf * Vf) - (Vi * Vi)) / (d * 2) worked and seems to be giving me accurate results. Thanks!
No idea how it would really differ from (Vf * Vf) - (Vi * Vi) / (d * 2), but hey, now I know :P
:D you are welcome!
Hello,

xTehOnex Wrote:


Yeah the ((Vf * Vf) - (Vi * Vi)) / (d * 2) worked and seems to be giving me accurate results. Thanks!
No idea how it would really differ from (Vf * Vf) - (Vi * Vi) / (d * 2), but hey, now I know :P


It is about operators precedence, order of operations..

In ((Vf * Vf) - (Vi * Vi)) / (d * 2) it is the whole (Vf * Vf) - (Vi * Vi) that is divided by 2*d.

In (Vf * Vf) - (Vi * Vi) / (d * 2) it is Vi*Vi that is divided by 2*d.

Let's make it with numbers for example:

Vf=10
Vi=6
d=4

In the first:

((10*10) - (6*6))/(4*2) = (100-36)/8 = 64/8 = 8

In the second:

(10*10) - (6*6)/4*2= 100 - 36/8 = 100 - 4.5 = 95.5

It's just like your calculator.

http://en.wikipedia.org/wiki/Order_of_operations

All my best,
Topic archived. No new replies allowed.