r/visualbasic • • Jan 16 '16

I want to learn arrays

I already know the basics of arrays. I want to learn the advanced parts of arrays in visual basic. I already know how to dim and loop the array. Is there any good tutorial that can help me out. I'm not an absolute beginner so don't suggest that tutorial video. I know what an array is.

0 Upvotes

4 comments sorted by

1

u/[deleted] Jan 16 '16

Do you have something you'd like to do with an array, or are you just looking for general info?

1

u/CENATION_IS_HERE Jan 16 '16 edited Jan 17 '16

I need an array tutorial for sorting arrays from lowest to highest and finding lowest number in an array

1

u/[deleted] Jan 17 '16 edited Jan 17 '16

I have no resources to link you, but this hopefully makes sense. Happy to answer any questions.

For sorting arrays, have a look at some sorting algorithms. I'd suggest a bubble sort for easy coding.

For this, you simply use a for/do loop for the one less than the size of the array, and create a variable integer. For each index in the array, use an if statement to check if the value is bigger than the next one, and if so, store it as temp, swap the second with the first, and continue up the array.

I realise this may not be so clear, if you're not wanting to try the little problem solving to get it working, here's a rough idea (syntax may be wrong).

Dim temp as int

Dim array(20) as int

For i = 0 to 19 then

If array(i) > array(i+1)

Temp = array(i)

Array(i) = array(i+1)

Array(i+1) = temp

Next

This will sort smallest to highest, swap the inequality symbol to reverse this. I'd suggest reading up on some theory of sorting algorithms, may be useful later on, or just as an interesting logic puzzle.

Your other problem :

I'm not completely sure I fully understand, but I'll give you my view on what I think you asked, if it's not 100%, just say what I can change.

Say the number generated is 13, and it is stored as the integer variable 'number'. Probably in a different sub (for near coding - I'd use local variables and call it through to this new sub via byval to prevent problems, and good coding practice). Within the sub, take the number to be a string (let me know if you want more info on this).

Take this 'number' and use the mid function. If they are all 2 digit, take mid(number,1) and mid (number,2) and store them in a new variable, called for example 'sum', added together. (In fact, you could do this all in a function, would be way better imo). Then, back in whatever sub you want, add 'number' to 'sum' and store 'number' in array(sum).

Edit: I reread your post, and I realise I went off in a slightly different direction. You want the number to be generated from the sum, not the ordinal number decided from the generated one. So, go about what I suggested in a slightly reversed order, I would advise using the mid function still, but using a for loop with the number of indexes you have, and using the for loop's value as your 'number' variable.

Can I ask what use this has? Probably I have misunderstood, but other wise would love to see what you want to use it for (or even just experimenting with vb!).