# Math within code is incorrect

So, let me start by saying thanks for the help, you guys are awesome.
My problem is that my math within the code is incorrect… The first problem if you look at the code below is that ‘vf’ is smaller than ‘vi’ and when I subtract ‘vi’ form ‘vf’ i get a positive answer, this should be negative. If I turn the two variables around, I get the negative number; however, this value is still incorrect. I am inputting 10 for ‘vi’, 2.5 for ‘vf’ and 1 for t. (2.5-10)/(1*60) should be -0.125 however I am getting .06 out of the program. I am sure my notation is right, but if the notation was right then my answer would be right wouldn’t it?

``````// Initialize program needs
void setup(){
Serial.begin(9600);
Serial.println("This program is designed to calculate acceleration and stop time.");
Serial.println("Please make sure the final velocity is less than the initial velocity");
}

// loop that calcutates acceleration
void loop(){
float vf,vi,a,t;                //initialize variables and set to
int minutes, seconds;
Serial.println("Please enter the initial velocity in mph");
while (Serial.available()<=0);     // wait for user input
vi=Serial.parseFloat();          // Assign value to inital velocity
Serial.println(vi);
Serial.println("Please enter the final velocity in mph");
while (Serial.available()<=0);  // wait for user input

Serial.println(vf);
Serial.println("Please enter the tansition time in minutes");
while (Serial.available()<=0);  // wait for user input
Serial.println(t);
a=acceleration(vf,vi,t);        // call acceleration function
minutes=minutestop(a,vi);             // call minutestop function
seconds=secondstop(a,vi);            // call secondstop function\
Serial.print("");
Serial.println("The cyclist is accelerating at a rate of ");
Serial.print(a);
Serial.print(" mi/hr^2");
Serial.println(" and will come to a rest in ");
Serial.print(minutes);
Serial.print(" minutes and " );
Serial.print(seconds);
Serial.println(" seconds.");
Serial.println("");
}

/*
acceleration fucntion
Calculates acceleration with user inputs of time, final velocity and initial velocity
*/

float acceleration(float t,float vf,float vi){
float a;
a=(vf-vi)/(t*60);
return (a);
}

/*
minutestop function
Calculates the minutes as to how long it will take to stop
*/

int minutestop(float a, float vi){    // output is intiger inputs are float
float stm;
int minutes;
stm=(vi/-a);                      // stoptime calculation for minutes
minutes=stm/60;
return (minutes);                      // return minutes value
}

/*
secondstop function
Calculates the seconds as to how long it will take to stop
*/
int secondstop(float a, float vi){    // output is intiger inputs are float
int seconds, sts;
sts=(vi/-a);
seconds=sts%60;
return (seconds);
``````

while my inputs are: vi=10, vf=2.5, t=1 output for acceleration is .06, it should be -.125. This in turn starts my time functions with false data and its all downhill from there.

Any thoughts?

You have confused yourself:

``````  a=acceleration(vf,vi,t);        // call acceleration function

...

float acceleration(float t,float vf,float vi){
``````

Notice how you swapped the arguments around?

Thanks for the reply Nick, I will update my code and see what gives. But as long as I have your attention, what does it matter what order the variables are listed in? Sorry, I'm pretty new to this and I'm obviously missing an important concept. Just trying to learn.

Thanks again
Jason

what does it matter what order the variables are listed in?

C (and C++) is a pass-by-value language. The value in the the calling argument list is passed to the called function, not the address of the variable, and certainly NOT the name.

The first value in the call is placed in the memory location associated with the first argument. The second value is placed in the memory location associated with the second argument. And so on.

The fact that the names of the arguments are the same as the names of some local variables in the calling function is irrelevant.

And, a bad idea. As you've discovered. Doing that led you to make some invalid assumptions.