Results 1 to 7 of 7

Thread: Working with Arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    5

    Working with Arrays

    I'm having some more trouble with working with arrays. My assignment is to take an input file of 10 numbers and display the original array, the array reversed (1st element is last element, etc.), the average of the parallel elements (nth element of both arrays), the average of the original array, and the elements greater than the average.

    I know I need to load the file into an array. However, I can't seem to figure out how to reverse it, nor average the two even if I can get it reversed. Anyone have any suggestions?

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Working with Arrays

    Since you clearly stated "assignment", can you post what code you have? And have tried?

  3. #3
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    407

    Re: Working with Arrays

    There's like list (of item) structures that can do all that stuff for you. But first things first, try! Since you are in school, lets school you a little bit. Attitude: You'll miss 100% of the time if you don't try!
    My Websites
    SharpMP3 - MP3 Design Articles www.sharpmp3.com
    Yobbers - Job Search www.yobbers.com
    Lets Trend - Methods For Riding Stock Trends www.letstrend.com

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    5

    Re: Working with Arrays

    First, I know how to get the file actually loaded as input.

    Code:
    If IO.File.Exists(filespec) Then
    	sr = IO.File.OpenText(filespec)
    Else
    	msgbox(msg)
    End If
    I can also get the average of the array given.

    Code:
    total = 0
    For cnt = 0 to UBound(numbers)
    	total += numbers(cnt)
    Next
    avg1 = total/UBound(numbers)
    And I can get the items greater than the average.

    Code:
    Do While num < UBound(numbers)
    	var = numbers(num)
    	If var > avg1 Then
    		lstDisplay.Items.Add(var)
    	End If
    Loop
    And I'm FAIRLY sure I can get the average of two parallel elements.

    Code:
    For cnt = 0 to UBound(numbers)
    	avg2 = (numbers(cnt) + numbers(CInt(UBound(numbers)) - cnt))/2
    	lstDisplay.Items.Add(avg2)
    Next
    However, as far as actually MAKING the arrays, I'm completely lost.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Working with Arrays

    You know that there will be 10 numbers because the assignment says so, so you simply create an array with 10 elements and then use a For loop to read the data from the file into its elements. If you don't know how to create an array then I suggest that you read your notes because I very much doubt that they would have set this assignment without showing you that. Alternatively, do some online reading about arrays, e.g.

    http://startvbdotnet.com/language/arrays.aspx
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    5

    Re: Working with Arrays

    Okay, I got my data loaded into the arrays and have my output. However, the output seems rather...ridiculous. The average for the parallel elements and the elements greater than the average seem wrong.

    Variable declarations:

    Code:
    Dim sr As IO.StreamReader
            Dim filespec As String = "numbers.txt"
            Dim msg As String = "Cannot open file: " & filespec
            Dim var As Double
            Dim cnt As Long
            Dim total, avg1 As Double
            Dim num As Integer = 0
            Dim array1() As String = IO.File.ReadAllLines(filespec)
            Dim array2(9) As String
            Dim arrayavg(9) As String
    My code and the respective output:

    Parallel Elements
    Code:
    For cnt = 0 To 9
                arrayavg(CInt(cnt)) = CStr(CDbl((array1(CInt(cnt)) + array2(CInt(cnt)))) / 2)
            Next
            lstDisplay.Items.Add("Average of Parallel Elements:")
            For cnt = 0 To 9
                lstDisplay.Items.Add(arrayavg(CInt(cnt)))
            Next
            lstDisplay.Items.Add("")
    My output here is a lot of random numbers. For example, VB seems to think the average of 10 and 5 is 255. What? Any ideas?

    Greater than Average
    Code:
    lstDisplay.Items.Add("Elements Greater than the Average:")
            Do While num < 9
                var = CDbl(array1(num))
                If var > avg1 Then
                    lstDisplay.Items.Add(var)
                End If
                num += 1
            Loop
    For this snippet of code, I get the output of 7, 8, 9, and 10. 6 SHOULD be included (the average of the array is 5.5). Any ideas why it isn't?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Working with Arrays

    You should debug your code. Place a breakpoint at the top of a method using the F9 key. When execution stops at that point you can step line by line using the F10 key. You can also step into a method call using F11. You can now examine the values of all your variables at each step simply by mousing over them, or using the Locals and Autos windows.

    At each step you check the values of your variables and decide what you think should happen when you step. You then step and check whether what you expected actually did happen. If it didn't then you've found an issue.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width