Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Count array elements and show in message box

XsoulXXsoulX Posts: 18Member
Hi,

how to determine the the count of odd and even numbers in an array ?

and

how to show the output in a message box ?

Thanks.

Comments

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭

    [code]
    Dim iNum as Integer = 21

    If iNum Mod 2 = 1 Then
    MsgBox("The number " & iNum.toString & " contained in the iNum variable is clearly Odd")
    Else
    MsgBox("The number " & iNum.toString & " contained in the iNum variable is clearly Even")
    End If
    [/code]

    Mod (short for Modulous) is a math operator that returns the Remainder of one number divided by another. In this case, 21 / 2 = 10 Remainder 1

    [code]
    'It's pretty easy to loop through an array
    'Assume arrNumbers is an array with values declared in it
    For i as integer = 0 to arrNumbers.Length - 1
    'Code here to figure out it arrNumbers(i) is Odd and count it
    Next i
    [/code]

    Good luck
  • XsoulXXsoulX Posts: 18Member
    But how am i going to count the number of occurrence of odd number as well as even number in an array ? Can you show me the code for doing that ?

    Thanks very much.
  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    I could have written the code for you in the last post, but I have a sneaking suspicion that your request is for a school assignment, and I am not about to write code for you to cheat at school (I did that when I was in school, and cheating got me no where).

    Of course, even if this is not for school, writing it for yourself will clearly teach you better then me writing it and you copying it, so I will provide you with a suggestion and let you adapt this and the last post into your own...

    If this is a school assignment, keep in mind:
    "Zero is an even number. An integer n is called *even* if there exists
    an integer m such that n = 2m, and *odd* if n+1 is even. From this,
    it is clear that 0 = (2)(0) is even. The reason for this definition
    is so that we have the property that every integer is either even or
    odd."
    So don't let your teacher/professor try and cheat you out of 100%, I had a teacher argue that with me once (for a strikingly similar assignment)


    so... here's a suggestion, and you can write it yourself:

    Create 2 variables to count the occurrences of both Even and Odd numbers...
    Dim iEven as Integer
    Dim iOdd as Integer

    In my last post I showed you an If statement that can determine if a number is either Odd or Even. Below that I showed you a For loop that could iterate through an array.

    Put the pieces together, add code to increment the values of iEven and iOdd so at the end iEven and iOdd will contain the count of Odd and Even occurrences.

    Good Luck, happy coding
    Sean Campbell
    -programmer, musician, forum moderator
    firesickle.com
  • XsoulXXsoulX Posts: 18Member
    Actually this is not for my assignment but for the exercise that my lecturer gave me since i cant figure out the correct way of doing it.

    Could you please explain further on this lines :

    For i As Interger = 0 to arrNumber.Length - 1
    ....
    Next i

    Thanks.
  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    I remember the good old days when I was still learning and every puzzle pozed was new grounds to tackle (insert nostalgia here)

    Sure! not a problem:

    I don't know how much you know about For loops. They are a very fundamental looping method in programming, and you will find them in most languages. Assuming you know how they technically work here is an explanation of that loop (if you want more information on how FOR loops work, hit google, there is some great explanations on them out there):

    In the problem you are trying to solve (Count Odd and Even occurences in an Array of numbers) you will need to itterate through an array.

    Assume arrNumber has been initialized with 10 values (as follows)

    Dim arrNumber() as Integer = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}

    From this line, we know that arrNumber(0) = 10, arrNumber(9) = 1, and calling arrNumber(10) will throw an exception (which crashes the program if you don't use exception handling). Arrays have a built in Property that return the "Count" of cells in the array, we call it like this:

    arrNumber.Length

    In our example, arrNumber.Length will return 10, the array bounds are 0-9

    The loop that you have asked about, has i being declared on the fly (hence the For i as integer part). When the loop is done, i gets garbage collected and I cannot use it outside of that loop. I do this out of habit, and as far as I know there isn't any advantages to doing that over using a declared variable.

    Now, the purpose of this For loop is to be able to iterate through EACH cell in the arrNumber array. We know that the first cell is 0 and the last cell is 9. We know that arrNumber.Length is 10. So, to go from 0 to 9, we have to call it from 0 to arrNumber.Length - 1

    So:

    [code]
    'Declare array
    Dim arrNumber As Integer = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}
    Dim iEven as Integer = 0
    Dim iOdd as Integer = 0

    'In most cases Arrays go from 0 to .Length - 1, so this For Loop
    'structure will become second nature to you

    'As far as I know there is no advantage to declaring i on the Fly
    'I simply do it from habit, and you could declare i before had like this:
    Dim i As Integer = 1241 'It doesn't matter what i is before the loop
    'On the first pass of the loop, it will be set to the Start value.
    For i = 0 to arrNumber.Length - 1
    If arrNumber(i) Mod 2 = 1 Then
    iOdd = iOdd + 1
    Else
    iEven = iEven + 1
    End If
    Next i
    [/code]

    Hope this helps,
    Sean C
    firesickle.com

  • XsoulXXsoulX Posts: 18Member
    I can solve the problem now.

    Thanks for your explanation and thanks for your help. ^^

  • seancampbellseancampbell Pennsylvania, USAPosts: 684Member ✭✭✭
    No problem,

    Hope you stick with it, there is a lack of good programmers out there,
    Sean Campbell
    firesickle.com
  • XsoulXXsoulX Posts: 18Member
    Still a long way to go ... because i just started learning programming.
    And I do not have a good problem-solving skill sometimes it's a bit tough for me.
  • SaadBinSaulatSaadBinSaulat Posts: 1Member
    Full source code for counting even and odd elements of an array is available at:
    http://bitsbyta.blogspot.com/2011/02/counting-even-odd-elements-in-array.html
Sign In or Register to comment.