Click here to Skip to main content
15,869,940 members
Please Sign up or sign in to vote.
1.00/5 (1 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

VB
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
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

I'm not sure if temp or the ListBoxs 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.

VB
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.
 
Share this answer
 
Comments
chan200uk 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900