Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What I am doing wrong with simple subtraction??

Status
Not open for further replies.

Shanksta

Technical User
Jun 28, 2002
96
US
I have a game where the user picks 6 random numbers in different order, and then finally the sum of their differences are added to give a score. As an example, the user would pick the following 6 numbers in the following order (lowest to highest):

10, 12, 12, 14, 14, 14

What the script should do is subract 14 from 14, get 0 and then keep moving down the line adding the difference. The final score that should be reported for this number set is 4.

However, although I thought this was simple, I set up a quick loop to add the differences and got a score of -10:

for($i=6; $i>1; $i--){
$score = $_GET["v".($i)] - $_GET["v".($i-1)];
echo $score . ' > ';
}

The variables are named v6 through v1 based on their position. I echoed the values and got a print out as follows:

-14 > 0 > 2 > 0 > 2 >

Any ideas why that first value is -14??
Its driving me insane!

Thanks!
 
I run the following code on windows and apache.

Code:
$score = 0 ; 
$_GET["v1"] = 10 ; 
$_GET["v2"] = 12 ;
$_GET["v3"] = 12 ;
$_GET["v4"] = 14 ;
$_GET["v5"] = 14 ;
$_GET["v6"] = 14 ;

for($i=6; $i>1; $i--){
   $score += $_GET["v".($i)] - $_GET["v".($i-1)];
   echo $score . ' > ';
}
 echo "<BR>" ;
 echo $score ;

and it gives me following result
Code:
0 > 0 > 2 > 2 > 4 > 
4

Not sure why u are getting this -14 value.



--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
I do the same and get:

-14 > -14 > -12 > -12 > -10 >
-10

Is there an error / mistake in the way the variables are being passed?
 
Is there an error / mistake in the way the variables are being passed?

It seems like so. Just print the variables u r getting in $_GET array and check wheather they are same as being passed from form etc.

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Shanksta, Are you:
1) using the same code that spookie posted and getting different results?
OR
2) using your original array with spookie's for loop?

I'm guessing that YOUR array starts at zero, rather than one, which would explain the result set.
v[0] = 10 ;
v[1] = 12 ;
v[2] = 12 ;
v[3] = 14 ;
v[4] = 14 ;
v[5] = 14 ;
v[6] = null

Greg
"Personally, I am always ready to learn, although I do not always like being taught." - Winston Churchill
 
I figured out the problem.

I must run the variables through an if statement first to make sure the null value gets assigned the last picked value.

Im an idiot.
:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top