Re: Array Sorting but return the index number as well
- From: "Dmitriy Antonov" <antonovdima@xxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 27 Oct 2006 21:38:11 -0400
<atmkoh@xxxxxxxxxxx> wrote in message
news:1161996939.530324.171760@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Thanks Larry worked a treat!
I will study the code and try to learn as much as I can.
On a related matter, how do you actually pass an array to a function?
Why do i get the error message?
Private Sub Command1_Click()
ReDim A(10, 5)
ReDim B(10, 5)
Dim i
Dim j
For i = 1 To 10
For j = 1 To 5
A(i, j) = Rnd
Next j
Next i
'What I am having trouble is:
B = square(A)
End Sub
Public Function square(A As Variant) As Variant
square = A ^ 2
End Function
'How do I write the function square such that for every element of A
that is passed in is squared and the number of elements that is passed
in is fixed.? What I am doing at present is passing it in as element
one by one!! Obviously there must be a method to square it as a matrix
right? I always get a type mismatch error. I dont understand why?
I was thinking of using a UBOUND function
Public Function square(A As Variant) As Variant
for i = 1 to UBound(A) ......
square(i,j) = A(i,j) ^ 2
End Function
But what do I give for the dimensions of j or can anyone advise a
better method?
Grateful for any help!
It's not clear for me what you are trying to accomplish. If you want to
return an array of the same size with elements equal to squares of
respective elements of the source then it is something like this (air code -
means typed right here without testing):
private sub Test()
'if you work with integer values - replace it with Long of whatever will
fit your needs
dim arSrc() as Double'dynamic array
dim arSquare() as Double
redim A(10,5)
arSquare=GetSquare(arSrc)
end sub
private function GetSquare(arSrc() as Double) as Double()
dim arRes() as Double
dim i as long, j as long
dim iMin as long, iMax as long
dim jMin as long, jMax as long
iMin=lbound(arSrc,1) : iMax=ubound(arSrc,1)
jMin=lbound(arSrc,2) : jMax=ubound(arSrc,2)
redim arRes(iMin to iMax, jMin to jMax)
for i=iMin to iMax
for j=jMin to jMax
arRes(i,j)=arSrc(i,j) * arSrc(i,j)
next j
next i
GetSquare=arRes
end function
Some [kind of OT] notes. I intentionally don't use Variants. You can still
use it - just replace Double() with Variant. But it is highly recommended to
use primitive data types, especially for extensive math operations
involving loops. Using Variant everywhere is very convenient way to degrade
you application's performance and memory usage, unless you know exactly when
and how to use them efficiently.
In your code it seems, that you use either global or module level variables,
where local variables would suffice, or you just don't use Option Explicit
and rely on autoinstancing of variables. Both are extremely dangerous
techniques, which you should drop without even starting.
Place Option Explicit statement at the beginning of each of your modules and
use explicit variables declarations (you can "ask" VB to do it automatically
for all new modules - Tools->Options-> check "Require Variable
Declaration"). Redim has unfortunate property to work as an implicit dim,
but there is nothing good about it - you still should use dim explicitly.
Dmitriy.
.
- Follow-Ups:
- Re: Array Sorting but return the index number as well
- From: atmkoh@xxxxxxxxxxx
- Re: Array Sorting but return the index number as well
- References:
- Array Sorting but return the index number as well
- From: atmkoh@xxxxxxxxxxx
- Re: Array Sorting but return the index number as well
- From: Larry Serflaten
- Re: Array Sorting but return the index number as well
- From: atmkoh@xxxxxxxxxxx
- Array Sorting but return the index number as well
- Prev by Date: Re: Log Phone Calls
- Next by Date: Obtain Mcse,Mcsa,Mcdba,Mcsd .Net,Ccna,Ccnp Without Exams(Pay After Check Resutls)100% Passing Gaurantee
- Previous by thread: Re: Array Sorting but return the index number as well
- Next by thread: Re: Array Sorting but return the index number as well
- Index(es):
Relevant Pages
|