School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,139 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,758 people online right now. Registration is fast and FREE... Join Now!




Working with arrays in VB6

 
Reply to this topicStart new topic

> Working with arrays in VB6, Adding at different positions in an array, sorting them in ascending/d

Nikhil_07n
Group Icon



post 21 Jan, 2009 - 09:39 PM
Post #1


First of all we have declared the array, it contains 10 elements(0 to 9).

CODE
Dim arr(9) As Single


Add the numbers in any manner you wish, the best is here.

CODE

Private Sub cmdAddAll_Click()
For i = 0 To 9
arr(i) = InputBox("enter a no.")
Next i
End Sub


Adding Number at different positions

CODE

Private Sub cmdAddatPos_Click()
no = InputBox("no to enter")
pos = InputBox("position")
For i = 9 To pos Step -1
arr(i) = arr(i - 1)
Next i
arr(pos - 1) = no
End Sub


Explaination : no is a varaible in which the desired no is stored.
pos is a variable in which we are storing the position of entering.

Now , the elements of aray above the position are unaffected, all we are doing is that we are Shifting the elements below the desired position by one place. For ex. the element at position 9 is erased and in takes the value of element 8.

Now, After shifting we are entering the desired number(no) in the required position.
NOTE: (pos-1) is used because the array starts with 0.

Sorting in Ascending Order

CODE

Private Sub cmdAscending_Click()
For k = 0 To 8
For j = k + 1 To 9
If arr(j) < arr(k) Then
a = arr(j)
arr(j) = arr(k)
arr(k) = a
End If
Next j
Next k


Explanation:
This one is the best example for understanding nested loops.
The outer loop selects one element for comparing.
The inner loops starts with the next element and compares it with the outer loop element.

If outerloop element is GREATER then inner loop element, their positions are interchanged, otherwise nothing is done.

at the end of both loops the the array gets sorted in ascending order.

Just print the array to see what happens:

CODE

For i = 0 To 9
Picture1.Print arr(i)
Next i
End Sub


Hope this helped you understand the concept of loops and arrays.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 03:28PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month