Jump to content

Calculating difference between points in array


dmhall0

Recommended Posts

I have elevation points in an array.  I need to calculate the difference between each point to get the total gain, total loss, and net.

 

Example:

point 2 - point 1 = +100

point 3 - point 2 = -10

point 4 - point 3 = -20

... and so on

 

So my outputs need to be:

Total Gain = +100

Total Loss = -30

Total Net = +70

 

Any help would be great.  I am still new to coding but this seems complicated.

That's what I was kind of thinking. You could do the math in the query as well.

 

SELECT point 2 - point 1 AS diff2_1, point 3 - point 2 AS diff3_2, point 4 - point 3 AS diff4_3 FROM table WHERE whatever . . . 

 

Then the calculated values would be in the array produced by whichever mysql_fetch_* function you use.

Then you're going to need to elaborate a little more, I believe. Are you trying to go through the array programatically, and just find the difference of a point and the previous point, regardless of the number of points in the array?

I am wanting to find the difference between every 2 points, just like in my first post example, then sum all the numbers > 0, sum all the numbers < 0, then give me the total.

So if there are 1000 points in the array, I would get 500 numbers.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.