borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2010 Andrea Tincaniborder

AndreaVB | Forum | News | Downloads | Register | Help | Member List | Statistics | Search | PM | Profile

Print This Topic
Previous Topic (Please let me open this code with VB.Net)Next Topic (Application Idel or not?) New Topic New Poll Post Reply
AndreaVB Forum : VB General : SIMPLE MATH PROBLEM - HELP
Poster Message
cyndy_northrup
Level: Protégé

Registered: 15-08-2007
Posts: 5

Ads by Lake Quincy Media
icon SIMPLE MATH PROBLEM - HELP

HELP - SIMPLE MATH PROBLEM

Hey people!


Have what should be a simple math problem, but its driving me absolutely buggy!

Something ridiculously obvious is escaping me and I can't for the life of me figure out
what the heck it is.

So, I describe what Im trying to do in this email and have also included what I think are the most
important code snips below, where I think the error is occuring.

So here is the description of the problem.

A series of numbers are provided by the user. The series is a sequence that is variable in length and variable in size per number. For example

23 1 899 72 11 02

or

1 7 8 19 2

or

233 455 23 24 29 12 14 16 17 91 92 94 98 57 34


so any number can be any size, and there can be any number within the sequence

The numbers are entered by the user into a text box

The user clicks a command button

A routine enumerates the total number of individual numbers in the sequence and creates an array having the same number of elements that will correspond to the math we will perform, and that follows the following parameter of;
(Code shown that creates the proper number of array elements)

Where "prefix_number" = the total number of numbers in the sequence, i.e. for the second
example above ( 1 7 8 19 2), prefix_number = 5

'this line determines the total number of array elements required
array_total_elements = (prefix_number + 1) * ((prefix_number + 1) + 1) / 2

'--------------------
'april 19 - 06 - ReDim adding_array(numchars) 'array with the number of elements of the same amount as the number of chars
'this array forms the needed structure of the conic, creating all positions for number placement within the cone

ReDim adding_array(array_total_elements)
'--------------------

So now we have an array with a total number of elements that will accomodate the math we want to perform
on the sequence, and that math operation is this;

Take the given sequence, i.e.

1 7 8 19 2

where each number in the sequence is stored individually in an array element, and subtract it from its neighbor to its immediate
left and starting from the left hand side, take the resultant of that value and place it in the appropriate array element not as yet occupied
and continue the process.

So here we have the following


1 7 8 19 2 is processed as;

where

    1 is stored in array element 0
    7 is stored in array element 1
    8 is stored in array element 2
    19 is stored in array element 3
    2 is stored in array element 4


        2-19 = -17

        19-8 = 11

        8-7 = 1
        
        7-1 = 6



so now we have ;

1 7 8 19 2
6 1 11 -17

and 6, 1, 11, -17 are stored in array elements

    6 is stored in array element 5
    1 is stored in array element 6
    11 is stored in array element 7
    -17 is stored in array element 8

we continue as;


6 1 11 -17


        -17-11 = -28

        11-1 = 10

        1-6 = -5
        


and -28,10,-5 are stored in array elements

    -5 is stored in array element 9
    10 is stored in array element 10
    -28 is stored in array element 11


And so on, the process continuing until we cant do it any more and we arrive at the last
array element result.

Then, we simply dump all the array values in proper sequence to a text box, perhaps a rich text box
and we place a hard return CHR(13) after each sub sequence so we can view the proper order of
computation.


So the box shows something like;

1 7 8 19 2
6 1 11 -17
-28,10,-5
....and so on.


Seems simple enough to me, but for some reason the whole backwards processing thing has stumped me,
working from left to right, subtractively, especially with properly populating and working with the arrays
that way, sounds silly I know, but I can't get this to work.

IMPORTANT - ALL CALCS MUST HAPPEN FROM RIGHT TO LEFT, SUBTRACTIVELY,
NOT LEFT TO RIGHT.

My code snippets follow below, can anyone whip this together simply, quickly?

Thanks for any feedback!

Cyndy


p.s.

I've attached a text file including feedback I've gotten from other coders,
none of them work as yet, but they have expressed elegant ideas in the approach of this, I'm working with the constraint of using VB version
4.0, so using the "Strip" function for VB 6.0 will NOT work here to stack
values into the array, unless the STRIP function is modified to work in VB 4.0, one programmer attempted this and it seems its almost there, but it doesn't work, including the code here too.

Please see attached.












'====================================='May 12 - 07
'this line modified to cause backwards subtractive process in line
    'LAST CODED
check_what_it_is = Val(adding_array(adding_element))
check_what_it_is2 = Val(adding_array(adding_element - 1))
                        adding_var = Val(adding_array(adding_element)) - Val(adding_array(adding_element - 1))
'====================================='May 12 - 07
DoEvents

                
'=======================
' may 12 07
'===================
'value placement
adding_array(adding_element + array_pos_update) = adding_var
adding_element = adding_element - 1
'--------------
'may 29-07
    
   'bug occurs here, adding element gets reduce to zero and it needs to
   'go back up to the rung down

Debug.Print "adding_array(6) ;"; adding_array(6)
'here is where it breaks down, at the row switcher
Debug.Print "adding_array(12) ;"; adding_array(12)
'=======================
    
'======for display to text boxes only, captures data stream for display

string_result = text_trans & " " & string_result
    text_trans = CStr(adding_var) & " " & text_trans
'======for display to text boxes only, captures data stream for display
                
'bug occuring here, may 29-07, its not switching to the next row properly

                    If row_switch_counter = switch_to_new_row Then
                        
        array_pos_update = array_pos_update + times_through_the_loop 'APRIL 18-06, does this var need to be reset to zero
        adding_element = times_through_the_loop '- 1 'array_pos_update 'moves array position pointer ahead two, jumping to next line
Debug.Print "adding_element ;"; adding_element
'may 28 - 7 ===============

'may 28 - 7 switch_to_new_row = switch_to_new_row - 1
                         row_switch_counter = times_through_the_loop
                    text_trans = text_trans & Chr(13)
                
        times_through_the_loop = 0
                End If
        End If

Next q 'steps through for loop
'===============================
'===============================
'=== ADDING ARRAY ENDS
'===============================
'==============================
'-----------


ctr_transfer = 0

RichTextBox3.Text = display_first_Line & Chr(13) & text_trans

'-------- send array info to text box

set_carriage_returns = prefix_number + 1
check_for_carriage_returns = 0

    feedtextbox_ctr = 0
    adding_element = 0
    display_result = "" 'var to hold looped array numbers
    
Do Until feedtextbox_ctr = numchars

DoEvents

    display_result = display_result & adding_array(adding_element)
        If check_for_carriage_returns = set_carriage_returns Then
            display_result = display_result & Chr(13)
            set_carriage_returns = set_carriage_returns - 1
            check_for_carriage_returns = 0
        End If

check_for_carriage_returns = check_for_carriage_returns + 1
    feedtextbox_ctr = feedtextbox_ctr + 1
    adding_element = adding_element + 1

Loop
'-------- send array info to text box
    
Text2.Text = display_result
'Text2.Text = text_trans
    
    

15-08-2007 at 03:45 AM
View Profile Send Email to User Show All Posts | Quote Reply
stickleprojects
Level: Moderator


Registered: 09-09-2002
Posts: 1060
icon Re: SIMPLE MATH PROBLEM - HELP

Hi,
Just noticed this and it's not marked as solved. I came up with this, it produces a few outputs in the debug window so you can take the bits you want.
One comment. From an array, it's going to be difficult to work out where to insert carriage-returns between the iterations, but i'll leave it up to you. Let me know if you need any more.
Kieron

New VB project
Paste this into a module and launch the immediate window and type "test"

Option Explicit

Sub test()
    ProcessUserNumbers "1 7 8 19 2"
    
End Sub
Sub main()
    test
    
End Sub
Private Function GetNumbersArray(strNumbers As String) As Integer()
    Dim arRet() As Integer
    Dim arIDX As Long
    Dim arTmp As Variant
    
    arTmp = Split(strNumbers, " ")
    ReDim arRet(UBound(arTmp))
    
    For arIDX = LBound(arTmp) To UBound(arTmp)
        arRet(arIDX) = arTmp(arIDX)
    Next
    
    GetNumbersArray = arRet
    
End Function
Sub ProcessUserNumbers(strNumbers As String)
' strNumbers is a space-delimited list of numbers
    Dim arNumbersIn() As Integer
    Dim arRowResults() As Integer
    Dim lngResults As Long
    Dim i As Integer
    Dim prefix_number As Long
    Dim array_total_elements As Long
    Dim lngArrayTotalIDX As Long
    Dim arTotalResults() As Integer
    
    ' Get the starting array
    arRowResults = GetNumbersArray(strNumbers)
  
    
    ' calc how much space required for the results
    prefix_number = UBound(arRowResults) - LBound(arRowResults)
    array_total_elements = (prefix_number + 1) * ((prefix_number + 1) + 1) / 2
    ReDim arTotalResults(array_total_elements)
    lngArrayTotalIDX = 0
    
     For i = LBound(arRowResults) To UBound(arRowResults)
        Debug.Print arRowResults(i) & " ";
        
        ' Add the current row into storage
        arTotalResults(lngArrayTotalIDX) = arRowResults(i)
        lngArrayTotalIDX = lngArrayTotalIDX + 1
    Next
    Debug.Print
    
    Do
    
        ' Generate new array with the results
        arRowResults = SubtractNumbers(arRowResults, lngResults)
    
        ' Output the current row
        If lngResults >= 0 Then
            For i = LBound(arRowResults) To UBound(arRowResults)
                Debug.Print arRowResults(i) & " ";
                
                ' Add the current row into storage
                arTotalResults(lngArrayTotalIDX) = arRowResults(i)
                lngArrayTotalIDX = lngArrayTotalIDX + 1
            Next
            Debug.Print
        End If
        
        
    Loop While lngResults > 0
    
    ' Output all totals
    For i = LBound(arTotalResults) To UBound(arTotalResults)
        Debug.Print arTotalResults(i) & " ";
    Next
    
End Sub

Function SubtractNumbers(arNumbersIn() As Integer, ByRef ResultsFound As Long) As Integer()
    ' Create an output array of the numbers in the input array
    
    Dim InIDX As Long
    Dim arNumbersOut() As Integer
    Dim NextOutIDX As Long
    Dim n1 As Integer
    Dim n2 As Integer
    Dim result As Integer
    Dim lngResultElementsRequired As Long
    Dim lngNumberCount As Integer
    
    
    ' calc the number of elements needed in the return array
    lngNumberCount = UBound(arNumbersIn) - LBound(arNumbersIn)
    lngResultElementsRequired = lngNumberCount - 1
    
    ' dimension the return array
    ReDim arNumbersOut(lngResultElementsRequired)
    
    ' results supposed to be stored in reverse order
    NextOutIDX = lngResultElementsRequired
    
    ' Loop through the numbers in from right to left
    For InIDX = UBound(arNumbersIn) To (LBound(arNumbersIn) + 1) Step -1
    
        ' Get numbers to subtract
        n1 = arNumbersIn(InIDX)
        n2 = arNumbersIn(InIDX - 1)
        
        ' subtract numbers
        result = n1 - n2
        
        ' Store the result
        
        arNumbersOut(NextOutIDX) = result
        
        NextOutIDX = NextOutIDX - 1
    Next
    
    ResultsFound = lngResultElementsRequired
    
    ' Return result to calling routine
    SubtractNumbers = arNumbersOut
    
End Function






____________________________
Build it better, faster, quicker, easier.. then fix it (non-offical MS mission statement)

10-09-2007 at 12:13 PM
View Profile Send Email to User Show All Posts | Quote Reply
AndreaVB Forum : VB General : SIMPLE MATH PROBLEM - HELP
Previous Topic (Please let me open this code with VB.Net)Next Topic (Application Idel or not?) New Topic New Poll Post Reply
Surf To:


Not Logged In? Username: Password: Lost your password?
Partners: Download Actual Software | Free Software Download
borderAndreaVB free resources for Visual Basic developersborder

borderAndreaVB Visual Basic and VB.NET source code resources - Copyright © 1999-2010 Andrea Tincaniborder