Two Plus Two Newer Archives  

Go Back   Two Plus Two Newer Archives > Other Topics > Computer Technical Help
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 11-20-2007, 02:17 AM
ThaSaltCracka ThaSaltCracka is offline
Senior Member
 
Join Date: Sep 2003
Location: Team Slayer!
Posts: 24,282
Default 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!
Reply With Quote
  #2  
Old 11-20-2007, 09:47 AM
jjshabado jjshabado is offline
Senior Member
 
Join Date: Jul 2006
Posts: 1,879
Default 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.
Reply With Quote
  #3  
Old 11-20-2007, 11:09 AM
Meech Meech is offline
Senior Member
 
Join Date: Nov 2004
Location: Meechigan
Posts: 1,159
Default 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.
Reply With Quote
  #4  
Old 11-20-2007, 11:56 AM
ThaSaltCracka ThaSaltCracka is offline
Senior Member
 
Join Date: Sep 2003
Location: Team Slayer!
Posts: 24,282
Default Re: Anyone good at VB?

thanks guys, Meech that looks like it might work!
Reply With Quote
  #5  
Old 11-20-2007, 09:16 PM
ThaSaltCracka ThaSaltCracka is offline
Senior Member
 
Join Date: Sep 2003
Location: Team Slayer!
Posts: 24,282
Default 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?
Reply With Quote
  #6  
Old 11-20-2007, 10:22 PM
Meech Meech is offline
Senior Member
 
Join Date: Nov 2004
Location: Meechigan
Posts: 1,159
Default 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
Reply With Quote
  #7  
Old 11-21-2007, 12:55 AM
BeaucoupFish BeaucoupFish is offline
Senior Member
 
Join Date: Jun 2005
Location: San Francisco
Posts: 723
Default 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
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:28 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.