Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

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

ARRAY FUNCTION 1

Status
Not open for further replies.

shakildor

Civil/Environmental
Jun 19, 2003
35
Dear Friends,
Can any one tell me about a function which receives a dynamic array(multi dimensional) and returns also an array(multi dimensional) . I tries to use VARIANT but could not be successful.
Dim Ans() as Double, InputArray() as Double

Dim Ans()=ArrayFunct(InputArray())

Function ArrayFunct (ReceiveingArray()) as double ()
Dim IntermediateArray() as double
....
....
...
ArrayFunct = IntermediateArray
End Function

The above is the example of my problem.
Can anyone give solution
Shakildor
 
Replies continue below

Recommended for you

Dim Ans()=ArrayFunct(InputArray())
This is not syntactically correct. You cannot call a function inside of a dim statement. Nor will the Redim statment work in this construct either. However, you may be able to do the following, as you've assigned some dimentions to InputArray
Ans = ArrayFunct(InputArray)

Inside your function, it will be necessary to assign dimensions to IntermediateArray before you can use it as a return value.

To provide examples, and/or suggestion to help you get the job done, it would helpful to know what the job is.
 
While you are using dynamic arrays be carefull with using ReDim statment. This statment always clear all your array. To keep your array use following. The following code show how to increase your array with protecting all array data.

'ReDim Preserve MyArray(UBound(MyArray)+1)'


Vlado
 
Friends,
Let me put my problem once again. I am programming a problem requiring matric operation. the size of matrix is not known and size of output matrix is also not known. So it will be like

Dim InputMatrix() as double
Dim Output() as double

Sub Main()
Dim NOE as integer
'....
'......
'Determination of a variable NOE
'.....
ReDim InputMatrix(NOE, NOE) as double
'Now matrix operation function is called as
OutputMatrix=MatrixFunction(InputMatrix)
'......
'....
End Sub

Function MatrixFunction(ABC as variant) as Double()
'Now the result of this function will depend upon
'the size of input matrix or ABC variant
' I tried this
Dim NOS as integer
NOS=2*ubound(ABC,1)
Redim IntermediateMat(NOS,NOS,NOS)
'Calculation of intermediateMat
'Now the problem is how to assign this intermediateMat
'as the result of this function



End Function



 
Not sure about using a Function but you can certainly have a sub with an input and an output array, eg this works


Private Sub Command1_Click()
Dim ABC(), DEF() As Double
ReDim ABC(3, 4)
MatrixFunction ABC(), DEF()
Debug.Print ABC(UBound(DEF, 1), UBound(DEF, 2))
End Sub

Sub MatrixFunction(ABC() As Variant, DEF() As Double)
ReDim DEF(UBound(ABC, 1), UBound(ABC, 2))
End Sub
 
A function should be able to handle it. Try something like this:

Code:
Private Function MatrixFunction(ByVal incoming As Variant) As Double()

  Dim temp() As Double
  Dim j As Integer
  Dim k As Integer
  Dim size As Integer
  
  size = UBound(incoming, 1)
  
  ReDim temp(size, size)
  For j = 0 To size
    For k = 0 To size
      'example calculation: add 2 to each element
      temp(j, k) = incoming(j, k) + 2
    Next k
  Next j
  
  MatrixFunction = temp

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor