|
#1
|
|||
|
|||
Anyone good at VB?
I am currently in a VB class and I am confused to all [censored]. Curious if anyone can maybe lend a hand.
Current directions: Define an array named data() of 30 integers. Assign integer values to the elements of data(i) with Basic statements, not be reading information from an input file. Use a symbolic constant named max for the number 30 of elements. Initialize the first 10 elements of data(i) to i*11; note that VB uses a zero-based lower bound. Initialize the next 10 elements of data(i) to –i, and set the last 10 elements of data(i) to i. When the initialization is complete, print the whole array, prefixed by the string "initial data:" and then the 30 numbers, 10 numbers per output line. Use the previously established method of first concatenating all substrings into a String object, including vbNewlines and blanks for separation. Here's what I have so far: Dim a(30) As Integer Dim i, j As Integer Dim s As String = "initial data:" & vbNewLine Const max As Integer = 30 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim s As String = a(i) & " " For i = 0 To 9 a(i) = -11 * i Next For i = 10 To 19 a(i) = -i Next For i = 20 To 29 a(i) = i Next s = s & i & " " If i Mod 10 = 0 Then s = s & vbNewLine End If MsgBox("Initial data:" & s) End Sub I later have to sort the values in non-descending order and print that result. I think I can do that, I am just having problems computing the values and getting them to output in a message box. Any help would be very appreciated! |
#2
|
|||
|
|||
Re: Anyone good at VB?
I don't know the language or the syntax, but if your for loop syntax is correct you're just missing a final loop. The variable i is probably only valid between the lines
For i = ... ... Next So at the end you say: s = s & i & " " If i Mod 10 = 0 Then s = s & vbNewLine End If But that isn't inside of a for loop and so the value i is meaningless. You probably want to put that inside a loop counting from 0 to 29. |
#3
|
|||
|
|||
Re: Anyone good at VB?
You could do something like this for the output
s = "initial data: " for i = 0 to 29 s = s & a(i) & " " If (i+1) Mod 10 = 0 Then s = s & vbNewLine End If next i msgbox s and for the first loop you could do it all in one, ala: for i = 0 to 29 if i < 10 then a(i) = 11 * i if i >=10 and i <=19 then a(i) = -i if i > 19 then a(i) = i next i Or you could use a case statement. |
#4
|
|||
|
|||
Re: Anyone good at VB?
thanks guys, Meech that looks like it might work!
|
#5
|
|||
|
|||
Re: Anyone good at VB?
thanks again Meech. I got everything to work right, EXCEPT the sorting. Could you possibly point me in the right direction?
|
#6
|
|||
|
|||
Re: Anyone good at VB?
I'm a database guy so a simple "SORT BY" or more recently a [img]/images/graemlins/blush.gif[/img]rder=> gets the job done.
But sort 101 is the bubble sort. Here is an example I nabbed off the net. If you need some help adapting it, lemme know. ' Bubble Sort Dim I, J, IntArray(), Temp as Integer ' Declare Variables For I = 1 To Length - 1 ' Supposed to be the smaller value For J = I To Length ' Supposed to be the larger value If IntArray(I) > IntArray(J) Then ' Compare Cells Temp = IntArray(I) ' \ IntArray(I) = IntArray(J) ' | Swap Cells IntArray(J) = Temp ' / End If Next Next http://en.wikipedia.org/wiki/Bubble_sort |
#7
|
|||
|
|||
Re: Anyone good at VB?
You were told to declare max as a constant, but you didn't use it to initialise the array:
Const max As Integer = 30 Dim a(max) As Integer Scope: you have declared the string s globally and again locally. You should lose all the global declarations, make them all local. Also, note that in VB at least, the following declaration makes i a variant, and j an integer Dim i, j As Integer Either do Dim i As Integer, j As Integer or better yet Dim i As Integer Dim j As Integer Actually, you are not even using the second variable. Finally, from your supplied code, I cannot imagine you are getting much output. Why not populate your string as you loop through setting the array values? Also, please note spacing and indentation for awesome readability [img]/images/graemlins/smile.gif[/img] (only shows up using .... sorry) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ....Const max As Integer = 30 ....Dim a(max) As Integer ....Dim i As Integer ....Dim s As String ....s = "initial data:" & vbNewLine ....For i = 0 To 9 ........a(i) = i * -11 ........s = s & a(i) & " " ....Next ....s = s & vbNewLine ....For i = 10 To 19 ........a(i) = -i ........s = s & a(i) & " " ....Next ....s = s & vbNewLine ....For i = 20 To 29 ........a(i) = i ........s = s & a(i) & " " ....Next ....s = s & vbNewLine ....Call MsgBox(s) End Sub |
|
|