5 Replies - 612 Views - Last Post: 12 July 2014 - 10:00 PM Rate Topic: -----

#1 ChokeOnThis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 02-June 14

How to set an array with inputbox and then input into array system.

Posted 01 July 2014 - 04:52 AM

So, I can't really find much information about using an inputbox to set the array and then use another inputbox to input integers into the set array that has been created to convert that unsorted array into a sorted array system.

- A method should accept an array to sort from the user. The size of the array is also to
be determined by the user. Use Input Box’s for this functionality – one to get the size of
the array and then one for each string in the array.
- You need to display the content of the array in an output text box before and after the
sort.
- The program should also display the partially sorted solutions of the array as well.
These partial steps should clearly show how the algorithm sorts elements of the array.

So what I have so far, is for the setting the array system. The ReDim would be setting the array right?

Dim Numbers() As Integer
    Dim value As Integer = InputBox("Enter size of Array", "Array Size", 0)
    Dim value2 As Integer = InputBox("Enter numbers in your Array", "Numbers in Array", 0)
    ReDim Numbers(value - 1)


Now I have an example of the sorting array system that doesn't include the inputboxes, so I tried to change it to incorporate them, but the outcome isn't working correctly.

txtOutput.Text = "Unsorted array: " & vbCrLf
    For Each i As Integer In Numbers
            Numbers(value - 1) = value2
            txtOutput.Text &= i & vbTab
        Next i
        txtOutput.Text &= vbCrLf & "Sorted array: " & vbCrLf
        mergeSort(Numbers, 0, Numbers.Length - 1)
        For Each i As Integer In Numbers
            txtOutput.Text &= i & vbTab
        Next


MergeSort sytem.
Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: How to set an array with inputbox and then input into array system.

#2 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 182
  • View blog
  • Posts: 663
  • Joined: 03-February 10

Re: How to set an array with inputbox and then input into array system.

Posted 01 July 2014 - 05:06 AM

You don't really say what the problem is with the InputBox. An InputBox is used to get textual information from the user. Anything you enter will be returned as a single string. So you will either need to enter the values of the array with a delimiter or loop and input individual values to add to the array.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3823
  • View blog
  • Posts: 13,545
  • Joined: 12-December 12

Re: How to set an array with inputbox and then input into array system.

Posted 01 July 2014 - 05:15 AM

    Numbers(value - 1) = value2

I think that you are expecting this to show the InputBox that you used here:
    Dim value2 As Integer = InputBox("Enter numbers in your Array", "Numbers in Array", 0)

It won't, it will just assign the single value that you already entered for value2.

You need to use the InputBox within the loop, to ask for a set of values.
Was This Post Helpful? 0
  • +
  • -

#4 ChokeOnThis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 02-June 14

Re: How to set an array with inputbox and then input into array system.

Posted 07 July 2014 - 05:38 AM

Thanks for that update, I changed it and now it works to set the array, but I am stuck now with trying to use the second input box to make the values in the array. I'm not sure how to loop it in so it collects all of those values I'm inputting. Would I have to have a seperate storage for each of the values that are input to collect them or could you just input them and they are looped in? Any examples or information would be muchly appreciated.

Dim Numbers() As Integer
Dim value As Integer = InputBox("Enter size of Array", "Array Size", 0)
ReDim Numbers(value - 1) ' You need to minus 1 value as the array starts at 0

txtOutput.Text = "Unsorted array: " & vbCrLf

For Each i As Integer In Numbers
        Numbers(value - 1) = InputBox("Enter numbers in your Array", "Numbers in Array", 0)
        txtOutput.Text &= i + 1 & vbTab ' +1 in there because last value is 1 less than input.
    Next i

    txtOutput.Text &= vbCrLf & "Sorted array: " & vbCrLf

    mergeSort(Numbers, 0, Numbers.Length - 1)

    For Each i As Integer In Numbers
        txtOutput.Text &= i & vbTab
    Next

Was This Post Helpful? 0
  • +
  • -

#5 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 182
  • View blog
  • Posts: 663
  • Joined: 03-February 10

Re: How to set an array with inputbox and then input into array system.

Posted 09 July 2014 - 06:35 AM

I would approach it as such:
'Get the total length of the array
        Dim numberOfInputValues As Integer
        Int32.TryParse(InputBox("Enter size of Array", "Array Size", CStr(0)), numberOfInputValues)

        'Determine array length
        Dim lengthOfArray As Integer = numberOfInputValues - 1

        'set the size of the array
        Dim numbers(lengthOfArray) As Integer

        'Define the output variable
        Dim outputText As New Text.StringBuilder()

        'AppendLine will add a vbCrLf to the end for us
        outputText.AppendLine("Unsorted array:")

        'For index As Integer = 0 To numbers.GetUpperBound(0)
        For index As Integer = 0 To lengthOfArray

            Dim nextInputValue As Integer

            'get the next value
            Int32.TryParse(InputBox("Please enter value " & (index + 1) & " of " & numberOfInputValues, "Numbers in Array", CStr(0)), nextInputValue)

            'Add the inputted value to the array at the current index
            numbers(index) = nextInputValue

            'add to the output text
            outputText.AppendFormat("{0}" & vbTab, nextInputValue)

        Next

        'add a blank line in the outputText
        outputText.AppendLine()

        outputText.AppendLine("Sorted array:")

        mergeSort(numbers, 0, numbers.Length - 1)

        'loop to add each sorted value to outputText
        For Each arrayValue As Integer In numbers
            outputText.AppendFormat("{0}" & vbTab, arrayValue)
        Next

        'display outputText
        txtOutput.Text = outputText.ToString()



I used Option Strict On at the top of my vb file which is why the InputBox methods have CStr(0) - because there is implicit conversions taking place there.

The Int32.TryParse method will attempt to parse the string value to an integer, if it is not valid it will throw an error that should be handled. A Try .. Catch block would be great to handle any errors.

I used a stringbuilder to build the output text because I just like stringbuilder when manipulating strings. :)/>
Was This Post Helpful? 1
  • +
  • -

#6 ChokeOnThis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 02-June 14

Re: How to set an array with inputbox and then input into array system.

Posted 12 July 2014 - 10:00 PM

Hi demausdauth, thanks for your reply and sorry I got back to you late, have been busy working. So I tried your code out and I had to change your stringbuilder so I had to Imports System.Drawing and also change it to System.Text.StringBuilder() to get it working, which it does now. But, thank you so much for the help. It is a lot better than what I was getting results with. I wasn't expecting a full code, more just little examples, but you went beyond that and thanks. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1