13,054,779 members (63,172 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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Graeme_Grant 193 OriginalGriff 150 Jochen Arndt 110 Dave Kreskowiak 85 RickZeeland 60
 OriginalGriff 5,731 RickZeeland 2,014 ppolymorphe 1,858 F-ES Sitecore 1,646 Dave Kreskowiak 1,494

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 4 Dec 2012