13,197,200 members (57,803 online)
Rate this:
See more:
Hi, I am working on arrays and I have a problem.I am trying to calculate the difference between data point,Xi, and its predecessor,Xi-1.
For example each element in an array or list needs to be subtracted with the previous element.

Series
1
2
4
8
3
3
3
4
2
2
.
.

Required output
1
2
4
5
0
0
1
2
0

I have tried to use two arrays ...I am using charts but the idea is the same..I tried to seperate the elements but I cant work out. I am stuct.. any example or anything would help.. thanx in advance

```For i = 0 To Chart1.Series("series1").Points.Count - 1
temp = Chart1.Series("series1").Points(i).YValues
xArray(i) = temp(0)

Next

For i = 1 To Chart1.Series("series1").Points.Count - 1
temp = Chart1.Series("series1").Points(i).YValues
yArray(i) = temp(0) - temp(i)
i += 1
Next```

Cheers
Posted 21-Nov-12 6:13am
Sergey Alexandrovich Kryukov 21-Nov-12 12:50pm

Do you understand that giving an example does not mean definition of the problem? For arbitrary X[0], what's its "predecessor"? 0? X[i]-1 is not difference between members, its X[i]-1; did you mean X[i-1]? We could use your code for definition, but you complain that it's wrong... Indeed, it look gibberish. Why "ToString()"?! Do you want to work with data, or its string presentation. What is temp? why? Why you always use temp(0), which is not "predecessor"?
--SA

Rate this:

## Solution 1

I'm not sure if `temp` or the `ListBox`s in your code example mean that this solution isn't valid for your problem, but the following code will initialise `arrayA` with your first series and then calculate `arrayB` which contains your desired result set.

```        Dim arrayA As Integer() = {1, 2, 4, 8, 3, 3, 3, 4, 2, 2}
Dim arrayB(arrayA.Length - 2) As Integer

For i As Integer = 1 To arrayA.Length - 1
arrayB(i - 1) = Math.Abs(arrayA(i) - arrayA(i - 1))
Next```

I added a `Math.Abs` call during the calculation as your required output set doesn't contain any negatives.
sana chan 22-Nov-12 6:48am

Thanks a lot. this is all the code i have

Dim xArray(Chart1.Series("series1").Points.Count) As Double
Dim yArray(Chart1.Series("series1").Points.Count) As Double
Dim zArray(Chart1.Series("series1").Points.Count) As Double

Dim temp() As Double

Dim x As New ArrayList

For i = 0 To Chart1.Series("series1").Points.Count - 1
temp = Chart1.Series("series1").Points(i).YValues
xArray(i) = temp(0)

Next

For i = 0 To Chart1.Series("series1").Points.Count - 1
temp = Chart1.Series("series1").Points(i).YValues
yArray(i) = temp(0)
i += 1
Next

Top Experts
Last 24hrsThis month
 OriginalGriff 470 Karthik Bangalore 235 CPallini 125 Dave Kreskowiak 100 Peter_in_2780 90
 OriginalGriff 7,035 ppolymorphe 1,699 Karthik Bangalore 1,663 CPallini 1,090 Jochen Arndt 1,045