Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping logic???? 1

Status
Not open for further replies.

Cheech

Technical User
Nov 6, 2000
2,933
EU
Hi I need to determine the maximum value in 2 arrays and thought the code below would do it just nice. Butttt____ it doesnt seem to roll through and find the highest value.

Anyone got any clues?

===================================================
For i = LBound(myArray1) to UBound(myArray1) Step 1
If (max_val < myArray2(i)) Then
max_val = myArray2(i)
End If
If (max_val < myArray3(i)) Then
max_val = myArray3(i)
End If
Next
===================================================

Cheers Live long and make your kids suffer..
 
what is myArray1? do all 3 arrays have the same number of elements? to be safe, i would loop thru each of the 2 arrays individually, which you're doing anyway....

dim max_val
max_val = 0

For i = 0 to UBound(myArray2)
If (max_val < myArray2(i)) Then
max_val = myArray2(i)
End If
Next

For i = 0 to UBound(myArray3)
If (max_val < myArray3(i)) Then
max_val = myArray3(i)
End If
Next
 
I still dont get it. gunna pull my hair out soon.. can anyone see what is wrong with this code PLEASE :( the output is below

arr.asp =
======================================
<%@LANGUAGE=&quot;VBSCRIPT&quot;%>

<html>
<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<%
myArray2 = Array(&quot;3&quot;, &quot;4&quot;, &quot;5&quot;, &quot;6&quot;, &quot;7&quot;, &quot;8&quot;, &quot;10&quot;)
myArray3 = Array(&quot;1&quot;, &quot;10&quot;, &quot;1&quot;, &quot;10&quot;, &quot;20&quot;, &quot;0&quot;, &quot;0&quot;)
dim max_val
max_val = 0

For i = 0 to UBound(myArray2)
If (max_val < myArray2(i)) Then
max_val = myArray2(i)
End If
Response.Write(&quot;max_val = &quot; & max_val & &quot; -- arr 2 = &quot; & myArray2(i) & &quot;<br>&quot;)
Next

For i = 0 to UBound(myArray3)
If (max_val < myArray3(i)) Then
max_val = myArray3(i)
End If
Response.Write(&quot;max_val = &quot; & max_val & &quot; -- arr 3 = &quot; & myArray3(i) & &quot;<br>&quot;)
Next
%>
</body>
</html>
=================================
output
max_val = 3 -- arr 2 = 3
max_val = 4 -- arr 2 = 4
max_val = 5 -- arr 2 = 5
max_val = 6 -- arr 2 = 6
max_val = 7 -- arr 2 = 7
max_val = 8 -- arr 2 = 8
max_val = 8 -- arr 2 = 10
max_val = 8 -- arr 3 = 1
max_val = 8 -- arr 3 = 10
max_val = 8 -- arr 3 = 1
max_val = 8 -- arr 3 = 10
max_val = 8 -- arr 3 = 20
max_val = 8 -- arr 3 = 0
max_val = 8 -- arr 3 = 0

why is max_val not increasing after it reaches &quot;8&quot; HELP!!!!!!!! Live long and make your kids suffer..
 
Hi i'm not sure but there is something about &quot;number&quot; since you're comparing numbers you need to put: FormatNumber

i think it should be here:

For i = 0 to UBound(myArray2)
If (max_val < FormatNumber(myArray2(i))) Then
max_val = myArray2(i)
End If

if not sorry but i know it should work used it before but can't find the files Have Fun...

Sharky99 >:):O>
 
Cheers sharky you put me on the right road...

dumbass that I am I was comparing strings not integers so what I have added is a line in each loop reading something like

this_var = Cdbl(MyArray2(i))

I then compare max_val to this_val and hey presto it all works.

Once again cheers for ya help. Live long and make your kids suffer..
 
Thanx for the *...

I need to code more, i'm getting rusty hahaha

See ya around ;-)
Have Fun...

Sharky99 >:):O>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top