12,996,367 members (67,408 online)
Rate this:
Please Sign up or sign in to vote.
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)
ListBox1.Items.Add(xArray(i).ToString())

Next

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

Thanks in advance..
Cheers
Posted 21-Nov-12 6:13am
Comments
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

## 1 solution

Rate this:
Please Sign up or sign in to vote.

## 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.
Comments
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)
ListBox1.Items.Add(xArray(i).ToString())

Next

For i = 0 To Chart1.Series("series1").Points.Count - 1
temp = Chart1.Series("series1").Points(i).YValues
yArray(i) = temp(0)
ListBox2.Items.Add(yArray(i).ToString())
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
 RickZeeland 235 ppolymorphe 180 OriginalGriff 115 Karthik Bangalore 90 Wonde Tadesse 90
 OriginalGriff 4,297 ppolymorphe 2,002 Kornfeld Eliyahu Peter 1,893 RickZeeland 1,751 Richard Deeming 1,493

Advertise | Privacy | Mobile
Web02 | 2.8.170609.2 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100