Help! havign issues with arrays.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Help! havign issues with arrays.

  1. #1
    Join Date
    Sep 2014
    Posts
    4

    Help! havign issues with arrays.

    I have a home work assignment due at the end of the week and im stuck on the last two problems and I am not sure how to make them happen using an array.

    Assume the array nums contains a list of positive integers. In the following exercises, write a Function procedure that calculates the stated value with a For Each loop.

    1. the sum of the numbers in the array
    2. the average of the numbers in the array
    3. the largest even number in the array (If there are no even numbers in the array, the Function procedure should return 0.)
    4. the smallest number in the array
    5. the number of two-digit numbers in the array
    6. the number of even numbers in the array

    This is what i have so far

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim b() As Integer = {3, 9, 30, 24, 5}
            Dim sum As Integer
            Dim ave As Integer
            Dim larg As Integer = b(0)
            Dim small As Integer = b(0)
            Dim Two As Integer
            Dim even As Integer
    
            For Each item As Integer In b
                sum += item
            Next
    
            ListBox1.Items.Add("The sum of the array is  " & Format(vbCrLf & sum))
            For Each item As Integer In b
                ave = CInt(sum / item)
            Next
            ListBox1.Items.Add("The average of the array is  " & Format(vbCrLf & ave))
    
    
            For i As Integer = 1 To b.Count - 1
                If b(i) > larg Then
                    larg = b(i)
                End If
            Next
    
            ListBox1.Items.Add("The largest number of the array is  " & Format(vbCrLf & larg))
    
            For i As Integer = 1 To b.Count - 1
                If b(i) < small Then
                    small = b(i)
                End If
            Next
    
            ListBox1.Items.Add("The smallest number in the array is  " & Format(vbCrLf & small))
        End Sub
    
    End Class

  2. #2
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,835

    Re: Help! havign issues with arrays.

    Well you would use a loop as you have done with the other items above

    If you are dealing with only positive integers then any value >9 will be 2 digits
    Alternately you could convert the value to a string using Cstr$() and then test the Len() of the string
    The first method would be the better and faster option

    For determining the Even/Odd part you could use the Mod operator
    Code:
    If Value Mod 2 = 0 Then
          'Value is an Even Number
    Else
          'Value is an odd number
    End If
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Sep 2014
    Posts
    4

    Re: Help! havign issues with arrays.

    Im actually pretty knew to programing. I am not exactly sure what you mean.

  4. #4
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,835

    Re: Help! havign issues with arrays.

    And I'm not sure what part you do not understand. Its very simple all positive integer values that are > 9 will be two digits those that are =<9 will be 1 digit. Simple right?

    As for the other part Mod will return the remainder of a division so all even numbers will return 0 if you use Mod 2 where all odd numbers will return 1 when using Mod 2

    3 Mod 2 = 1
    4 Mod 2= 0

    111 Mod 2= 1
    112 Mod 2=0

    Look up the Mod keyword

    All in all it is just basic math
    Always use [code][/code] tags when posting code.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center