cyndy_northrup Level: Trainee
 Registered: 15-08-2007 Posts: 2
|
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
|