|
-
Jan 24th, 2006, 05:06 PM
#1
Thread Starter
Hyperactive Member
Shifting an array
How would I shift the elements of an array to the right. For example, I want five shifts of the array to look like this:
0 0 0 0 0
0 0 0 0 5
0 0 0 5 2
0 0 5 2 1
0 5 2 1 8
5 2 1 8 7
Hope this makes sense. This shift will happen once per second in a thread that will update data to graph.
-
Jan 24th, 2006, 05:45 PM
#2
Re: Shifting an array
VB Code:
Dim arr() As String = {"0", "0", "0", "0", "0"}
For i As Int32 = 1 To 5
For ii As Int32 = 0 To arr.Length - 2
arr(ii) = arr(ii + 1)
Next
arr(arr.Length - 1) = i.ToString
Next
-
Jan 24th, 2006, 06:17 PM
#3
Re: Shifting an array
VB Code:
For i As Integer = myArray.GetUpperBound(0) To 1 Step -1
myArray(i) = myArray(i - 1)
Next i
myArray(0) = 0
-
Jan 24th, 2006, 07:14 PM
#4
Thread Starter
Hyperactive Member
Re: Shifting an array
Thanks guys
I knew it would be simple. I'm in a bit a of rush because my last day at my current job is Friday and I don't want to leave them hanging too much.
-
Jan 25th, 2006, 07:21 AM
#5
Re: Shifting an array
If you hadn't already guessed, JMC's version shifts to the right.
I don't live here any more.
-
Jan 25th, 2006, 08:54 AM
#6
Thread Starter
Hyperactive Member
Re: Shifting an array
If you hadn't already guessed, JMC's version shifts to the right.
Yeah, I figured that out. You would think that for what I paid for his advice, it would have been correct. Shifted things around and all is well. Thanks for the help.
-
Jan 25th, 2006, 10:10 AM
#7
Re: Shifting an array
He asked for right shift, but the example looked like left shift.
-
Jan 25th, 2006, 10:12 AM
#8
Re: Shifting an array
Should look at these things longer shouldn't I? I assumed that you were shifting elements out, but it would make more sense to be shifting them in. It also seemed a bit odd that you had the iterations upside down but I didn't give it too much thought (not much to spare you understand). You probably already have it but I'd do it like this:
VB Code:
For i As Integer = 0 To myArray.Length - 2 Step 1
myArray(i) = myArray(i + 1)
Next i
myArray(myArray.GetUpperBound(0)) = newValue
-
Jan 25th, 2006, 12:22 PM
#9
Thread Starter
Hyperactive Member
Re: Shifting an array
He asked for right shift, but the example looked like left shift.
I'm an engineer so I occasionally get my left and right confused. Thanks again.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|