Question:
please help me with my homework in visual basic using arrays.?
juz_askin
2007-11-29 18:20:42 UTC
the question was: write a program that reads a series of strings from the user and displays only those strings beginning with the letter "b".

i want to use an array that every time the user inputs a string, it will store in an array but the array don't have a fixed size (it just depends on how many strings the user wants to input). and then test whether the strings stored in that array begins with letter b.

please help me with this. im not asking for the entire code. just the logic of it. but if you can put the code, i would be very pleased.

thanks for those who will answer.
Five answers:
2007-11-29 21:49:17 UTC
If you are still stuck , you ,may contact a homework helper at websites like http://homeworkhelp.co.in/
Lie Ryan
2007-11-29 21:55:55 UTC
OK, first we should learn the building blocks.



1. Arrays

I guess you should've already learned about this, but just to make sure you know:

To create an array, you use

() As

The important thing is the parantheses after the variable name.

So to make the array you need:

Dim StringArr(100) as String



2. String manipulation.

VB has one of the strongest string manipulation library (IMHO). It is extremely easy to do most string operation in VB. For your particular need, you will want to use

Left(, )

The Left function would get the first letters from .

For your particular need, you would use

FirstLetter = Left(stringBeingChecked, 1)

Extras: Other than Left, there is also Right(, , , ).



3. Function Declaration

To declare a function, VB use this syntax:

Function ([ByVal | ByRef] As ) As



4. Loop

VB has several ways to create loop

For ... Next

Do ... Loop



For ... Next loop is used when you know beforehand how many times you need to loop. In most cases this is the loop you would use. Do ... Loop is used when you cannot determine how many times the loop would cycle. You end a Do ... Loop loop by defining a condition.



In your case, we'd use the For ... Loop since we know that the loop would cycle as much as the amount of member of the array.



The syntax for For...Next

For = To

....... CODES HERE ......

Next



In your particular need:

For i = StringArr.GetLowerBound(0) To StringArr.GetUpperBound(0)

.... CODES HERE

Next i



5. Array Resizing

Remember the

Dim StringArr(100) As String

we created previously?

This will make a static sized array, it will work, but if you're adding, deleting, modifying more array members than the specified array size, it would generate an erro.

So, to make a better function would be not to preallocate the size of the array, by using Dynamic Array.

Dim StringArr() As String

And use the ReDim commands, which resizes a the array.

ReDim Preserve StringArr(newSize)

The Preserve means that old values (before resizing) would be preserved (means carried to the new array), if you don't specify the Preserve, old values would be wiped out.





PUTTING THEM TOGETHER

I'm not exactly sure how you want the user enters the string array. Whether to parse a single, comma-separated string value, parse a single, space-separated string value, or to input single values multiple times over. So I'd not point you out how to make the input method and the parser. I'm also not sure how you want to present the output (whether to make a list box, combo box, or printed on a text box) so I wouldn't go into further details there.



=== CODE ===



Private Function FilterFirstLetter(ByVal stringArr() as String, ByVal letter as Char) As String()



Dim output() as String

ReDim output(100)

Dim counter As Integer = 1



'' Notice that I can use the declaration i As Integer

'' in the For statement, without predeclaring i

'' in any previous Dim statement.

For i As Integer = stringArr.GetLowerBound(0) To stringArr.GetUpperBound(0)



If Left(stringArr(i), 1) = letter Then



'' Resizes the array if needed

If counter = output.GetUpperBound(0) + 1 Then

ReDim Preserve output(output.GetUpperBound(0) + 100)

End If



output(counter) = stringArr(i)

counter += 1



End If



'' Resizes the array, so unneeded values (empty values)

'' are discarded.

ReDim Preserve output(counter)



Return output

Next i



End Function

=== CODE ===
A Bored Nerd
2007-11-29 18:53:48 UTC
Use the MSDN documentations



Working with Arrays and Array Elements

http://msdn2.microsoft.com/en-us/library/ms235277(VS.80).aspx



How to: Search for a String in an Array of Strings (Visual Basic)

http://msdn2.microsoft.com/en-us/library/eefw3xsy(VS.80).aspx





How to: Search Within a String (Visual Basic)

http://msdn2.microsoft.com/en-us/library/2y7ddk24(VS.80).aspx



Basic outline:



1) get the user provided strings into an array.

2) loop through the array

3) for each string, do a test to see if the first character is a "b" perhaps use strings.left(1) to find out what the first character is. You could use strComp in text mode to compare that first character with "b"

4) store any strings that do start with b in a new array (or maybe delete any non-"b" strings from the existing one). and then print out the array when you're done or simply print out b strings on the fly
2016-10-10 02:43:28 UTC
interior the question, you fail to state what variety of information should be loaded. Is it purely meant to be some form of random information? if so, evaluate utilising a undemanding loop, producing random numbers and including them to the array for the period of each and every bypass. If the innovations comes from a record, that's commonplace to apply a loop whose situation is which you have not reached the tip of the record.
Just David
2007-12-02 20:13:16 UTC
Sort of a rough draft sample



Dim arrStrings() as String

Dim timetoexit as boolean = false

While not timetoexit

dim mystring as string = inputbox("Enter another string")

If trim(mystring) = "" then

timetoexit = true

Else

redim preserve arrStrings(ubound(arrStrings) + 1)

arrStrings(ubound(arrStrings) = mystring

End If

End While



for i as integer = 0 to ubound(arrStrings)

console.writeline(i.tostring & ": " & arrstrings(i))

Next


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...