I have an array with 50 random integers, all ranging from 0 to 9(they are all displayed in one textbox). How can I add all the 1's together and display them in a label? Same with the 2's, 3's etc. Thanks!How do you add duplicate integers in an Array in VB?
There are many ways to skin a cat.
Here is another method which is compact and very efficient.
Create a second array called Total which has 10 elements. (VB initializes these to 0)
Dim Total(9) as integer (VB initializes these to 0)
Then loop through your original array reading the stored values (0 to 9). These values are then used as an index value to increment the appropriate element (0 to 9) in array Total.
For x = 0 to 49
idx = myArray(x) 'read the contents of the array
total(idx) = total(idx) + 1
next x
That's it.....How do you add duplicate integers in an Array in VB?
you can use a select case statement, and a loop. Since you knwo you have 50 numbers, the loop fwill need 50 intervals,
so
dim counter0,counter1,counter2,counter3,coun鈥?as intereger
for x = 0 to 49 (0 being the first index in the array)
{
select case array[x]
case 0:
counter0 = counter 0 + 1
case 1:
counter1 = counter 1 + 1
case 2:
counter2 = counter 2 + 1
case 3:
counter3 = counter 3 + 1
case 4:
counter4 = counter 4 + 1
case 5:
counter5 = counter 5 + 1
case 6:
counter6 = counter 6 + 1
case 7:
counter7 = counter 7 + 1
case 8:
counter8 = counter 8 + 1
case 9:
counter9 = counter9 + 1
}
label1.text = counter0
ect....
No comments:
Post a Comment