Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. Approximate zero values in an array

Approximate zero values in an array

Scheduled Pinned Locked Moved Visual Basic
helpquestiondata-structurestutorial
12 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Johnkokk
    wrote on last edited by
    #1

    Ok, here is the question I have an array with let's say, the following values : 17 18 0 0 24 23 0 20 0 0 0 16 I need to approximate(average) the ZERO values to the non zero values.For example i would expect the 0 between the 23 and the 20, to become 21.5. Or the first 2 zeros to be 20 and 22. Can you help ? It should be easy but i am completely stuck in here.

    L 1 Reply Last reply
    0
    • J Johnkokk

      Ok, here is the question I have an array with let's say, the following values : 17 18 0 0 24 23 0 20 0 0 0 16 I need to approximate(average) the ZERO values to the non zero values.For example i would expect the 0 between the 23 and the 20, to become 21.5. Or the first 2 zeros to be 20 and 22. Can you help ? It should be easy but i am completely stuck in here.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Johnkokk wrote:

      It should be easy but i am completely stuck in here.

      This is simple mathematics. Add up the numbers surrounding the zeros and calculate the average (by division). Replace the zeros with the average values. Try doing it with pencil and paper to see how it can be achieved.

      D 1 Reply Last reply
      0
      • L Lost User

        Johnkokk wrote:

        It should be easy but i am completely stuck in here.

        This is simple mathematics. Add up the numbers surrounding the zeros and calculate the average (by division). Replace the zeros with the average values. Try doing it with pencil and paper to see how it can be achieved.

        D Offline
        D Offline
        David Skelly
        wrote on last edited by
        #3

        It's not really the average, though, if I've understood the question. The sequence 23, 0, 20 becomes 23, 21.5, 20. 18, 0, 0, 24 becomes 18, 20, 22, 24 20, 0, 0, 0, 16 becomes 20, 19, 18, 17, 16. Not sure what the technical term for that is. Interpolation? Even so, it is still simple maths. The only tricky bit is handling the end conditions if a 0 occurs at the first or last position in the array.

        L 2 Replies Last reply
        0
        • D David Skelly

          It's not really the average, though, if I've understood the question. The sequence 23, 0, 20 becomes 23, 21.5, 20. 18, 0, 0, 24 becomes 18, 20, 22, 24 20, 0, 0, 0, 16 becomes 20, 19, 18, 17, 16. Not sure what the technical term for that is. Interpolation? Even so, it is still simple maths. The only tricky bit is handling the end conditions if a 0 occurs at the first or last position in the array.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          David Skelly wrote:

          It's not really the average, though, if I've understood the question.

          You're right of course, I was confused (quite easy thing to do) by the questioner using the expression "approximate(average)".

          1 Reply Last reply
          0
          • D David Skelly

            It's not really the average, though, if I've understood the question. The sequence 23, 0, 20 becomes 23, 21.5, 20. 18, 0, 0, 24 becomes 18, 20, 22, 24 20, 0, 0, 0, 16 becomes 20, 19, 18, 17, 16. Not sure what the technical term for that is. Interpolation? Even so, it is still simple maths. The only tricky bit is handling the end conditions if a 0 occurs at the first or last position in the array.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            David Skelly wrote:

            Interpolation

            Ding ding ding! Winner winner chicken dinner!

            Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

            J P 2 Replies Last reply
            0
            • L Lost User

              David Skelly wrote:

              Interpolation

              Ding ding ding! Winner winner chicken dinner!

              Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

              J Offline
              J Offline
              Johnkokk
              wrote on last edited by
              #6

              Ok, we established that what i need, is some sort of linear interpolation. Any ideas ? :)

              N 1 Reply Last reply
              0
              • J Johnkokk

                Ok, we established that what i need, is some sort of linear interpolation. Any ideas ? :)

                N Offline
                N Offline
                nlarson11
                wrote on last edited by
                #7
                    Dim arr() As Single = {17, 18, 0, 0, 24, 23, 0, 20, 0, 0, 0, 16}
                    Dim idx1 As Int16 = -1, idx2 As Int16 = -1
                    Dim FoundAZero As Boolean = False
                
                    For i As Int16 = 0 To arr.GetUpperBound(0)
                
                        If Not FoundAZero Then
                            If arr(i) = 0 Then
                                FoundAZero = True 'once we find an zero the else will be used below until we find the next non zero 
                            Else
                                idx1 = i 'hold on to the index every non zero
                            End If
                        Else
                            If arr(i) <> 0 Then '
                                idx2 = i 'hold on to the index of the non zero 
                                '--------------------------------------
                                'get the value that's held in both indexes
                                Dim val1 As Int16 = arr(idx1), val2 As Int16 = arr(idx2)
                                Dim IncVal As Single
                
                                'increment value is non zero 1 minus non zero 2 / the number of zeros between
                                'the \* -1 is whether or not we need to increment up or down depending on values of the array
                
                                If val1 < val2 Then
                                    IncVal = (val2 - val1) / (idx2 - idx1)
                                Else
                                    IncVal = ((val1 - val2) / (idx2 - idx1)) \* -1
                                End If
                
                                For x As Int16 = idx1 + 1 To idx2 - 1
                                    arr(x) = arr(x - 1) + IncVal 'replace the 0 inbetween both indexes with the incremented value 
                                Next
                                '--------------------------------------
                                idx1 = idx2        'take the last none zero and set it to the first hold as we continue down the array
                                FoundAZero = False 'reset finding a zero flag
                            End If
                        End If
                    Next
                

                'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

                J G 2 Replies Last reply
                0
                • N nlarson11
                      Dim arr() As Single = {17, 18, 0, 0, 24, 23, 0, 20, 0, 0, 0, 16}
                      Dim idx1 As Int16 = -1, idx2 As Int16 = -1
                      Dim FoundAZero As Boolean = False
                  
                      For i As Int16 = 0 To arr.GetUpperBound(0)
                  
                          If Not FoundAZero Then
                              If arr(i) = 0 Then
                                  FoundAZero = True 'once we find an zero the else will be used below until we find the next non zero 
                              Else
                                  idx1 = i 'hold on to the index every non zero
                              End If
                          Else
                              If arr(i) <> 0 Then '
                                  idx2 = i 'hold on to the index of the non zero 
                                  '--------------------------------------
                                  'get the value that's held in both indexes
                                  Dim val1 As Int16 = arr(idx1), val2 As Int16 = arr(idx2)
                                  Dim IncVal As Single
                  
                                  'increment value is non zero 1 minus non zero 2 / the number of zeros between
                                  'the \* -1 is whether or not we need to increment up or down depending on values of the array
                  
                                  If val1 < val2 Then
                                      IncVal = (val2 - val1) / (idx2 - idx1)
                                  Else
                                      IncVal = ((val1 - val2) / (idx2 - idx1)) \* -1
                                  End If
                  
                                  For x As Int16 = idx1 + 1 To idx2 - 1
                                      arr(x) = arr(x - 1) + IncVal 'replace the 0 inbetween both indexes with the incremented value 
                                  Next
                                  '--------------------------------------
                                  idx1 = idx2        'take the last none zero and set it to the first hold as we continue down the array
                                  FoundAZero = False 'reset finding a zero flag
                              End If
                          End If
                      Next
                  

                  'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

                  J Offline
                  J Offline
                  Johnkokk
                  wrote on last edited by
                  #8

                  Ok, the code above works almost perfectly. THe only problem is that if the first position of the array is populated with 0, it crashes at " Dim val1 As Int16 = arr(idx1)", because idx1 remains -1 Thanks.

                  N G 2 Replies Last reply
                  0
                  • J Johnkokk

                    Ok, the code above works almost perfectly. THe only problem is that if the first position of the array is populated with 0, it crashes at " Dim val1 As Int16 = arr(idx1)", because idx1 remains -1 Thanks.

                    N Offline
                    N Offline
                    nlarson11
                    wrote on last edited by
                    #9

                    I understood that when I wrote but I figured it wouldn't be possible to have the array start with a zero or end with one because how can you figure out what those should be. I guess you could take the number to the right of the 0 and divide by 2 but wasn't sure if that's what should happen.

                    'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

                    1 Reply Last reply
                    0
                    • L Lost User

                      David Skelly wrote:

                      Interpolation

                      Ding ding ding! Winner winner chicken dinner!

                      Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]

                      P Offline
                      P Offline
                      peterchen
                      wrote on last edited by
                      #10

                      Hmmmmm... chicken!

                      Personally, I love the idea that Raymond spends his nights posting bad regexs to mailing lists under the pseudonym of Jane Smith. He'd be like a super hero, only more nerdy and less useful. [Trevel]
                      | FoldWithUs! | sighist | µLaunch - program launcher for server core and hyper-v server

                      1 Reply Last reply
                      0
                      • N nlarson11
                            Dim arr() As Single = {17, 18, 0, 0, 24, 23, 0, 20, 0, 0, 0, 16}
                            Dim idx1 As Int16 = -1, idx2 As Int16 = -1
                            Dim FoundAZero As Boolean = False
                        
                            For i As Int16 = 0 To arr.GetUpperBound(0)
                        
                                If Not FoundAZero Then
                                    If arr(i) = 0 Then
                                        FoundAZero = True 'once we find an zero the else will be used below until we find the next non zero 
                                    Else
                                        idx1 = i 'hold on to the index every non zero
                                    End If
                                Else
                                    If arr(i) <> 0 Then '
                                        idx2 = i 'hold on to the index of the non zero 
                                        '--------------------------------------
                                        'get the value that's held in both indexes
                                        Dim val1 As Int16 = arr(idx1), val2 As Int16 = arr(idx2)
                                        Dim IncVal As Single
                        
                                        'increment value is non zero 1 minus non zero 2 / the number of zeros between
                                        'the \* -1 is whether or not we need to increment up or down depending on values of the array
                        
                                        If val1 < val2 Then
                                            IncVal = (val2 - val1) / (idx2 - idx1)
                                        Else
                                            IncVal = ((val1 - val2) / (idx2 - idx1)) \* -1
                                        End If
                        
                                        For x As Int16 = idx1 + 1 To idx2 - 1
                                            arr(x) = arr(x - 1) + IncVal 'replace the 0 inbetween both indexes with the incremented value 
                                        Next
                                        '--------------------------------------
                                        idx1 = idx2        'take the last none zero and set it to the first hold as we continue down the array
                                        FoundAZero = False 'reset finding a zero flag
                                    End If
                                End If
                            Next
                        

                        'Never argue with an idiot; they'll drag you down to their level and beat you with experience.' ~ anonymous 'Life's real failure is when you do not realize how close you were to success when you gave up.' ~ anonymous

                        G Offline
                        G Offline
                        Gideon Engelberth
                        wrote on last edited by
                        #11

                        You may want to turn on Option Strict. When you use val1 and val2 as integers, you will produce incorrect results if one of the non-zero values has a decimal component. Also, I would recommend using Integer instead of Int16 for the index variables as turning on Option Strict will cause errors for both of your loop declarations.

                        1 Reply Last reply
                        0
                        • J Johnkokk

                          Ok, the code above works almost perfectly. THe only problem is that if the first position of the array is populated with 0, it crashes at " Dim val1 As Int16 = arr(idx1)", because idx1 remains -1 Thanks.

                          G Offline
                          G Offline
                          Gideon Engelberth
                          wrote on last edited by
                          #12

                          Then add some error checking. Assuming you put this code in a method, you can do the check at the right at the top of the method to make sure that the parameter a) is not nothing, b) does not start with a zero, and c) does not end with a zero and throw the appropriate exception. Like so:

                          Private Sub Interpolate(ByVal values() As Single)
                          If values Is Nothing Then Throw New ArgumentNullException("values")
                          'nothing to do in this routine and the other error checks would cause errors
                          If values.Length = 0 Then Exit Sub
                          If values(0) = 0 Then
                          Throw New ArgumentException("Value array cannot start with a 0.", "values")
                          End If
                          If values(values.Length - 1) = 0 Then
                          Throw New ArgumentException("Value array cannot end with a 0.", "values")
                          End If

                          'other code to do interpolation here
                          

                          End Sub

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups