Solved

function

Posted on 2006-11-13
14
124 Views
Last Modified: 2010-04-23
Hi,

How can I create an function to return the following result

orginal data     result
10                   10    <---- first value
5                     15   <---- first value + second value
0                     15   <--- first + second + third value
25                   40   <--- first + second + third + fouth and so on

ayha
0
Comment
Question by:ayha1999
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 35

Expert Comment

by:YZlat
ID: 17932648
how does your input data come in? as a string, as an array? in a file?
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17932681
this solution is for an array:

Function Summation(ByVal arr as Integer) As Integer
        Dim result As Integer
        For i As Integer = 0 To arr.length - 1
            result = result + arr(i)
        Next
return result
    End Function
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17932685
sorry, should be

Function Summation(ByVal arr() as Integer) As Integer
        Dim result As Integer
        For i As Integer = 0 To arr.length - 1
            result = result + arr(i)
        Next
return result
    End Function
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17933362
Maybe this is what you had in mind:


Function RunningSum(ByVal x As Integer) As Integer
    Static total As Integer = 0
    total +=x
    Return total;
End Function


... though I suspect there is a more direct way of achieving that for any particular situatoin.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17933367
Use a static variable:

Public Class FormC

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Debug.Print(AddValue(10))
        Debug.Print(AddValue(5))
        Debug.Print(AddValue(0))
        Debug.Print(AddValue(25))
    End Sub

    Public Function AddValue(ByVal value As Integer) As Integer
        Static total As Integer = 0
        total = total + value
        Return total
    End Function

End Class



Though it would probably be better to encapsulate this in a class so you can store the invdivual variables and optionally reset the total...
0
 
LVL 7

Author Comment

by:ayha1999
ID: 17938843
Hi,

YZlat's solution seems to be what I am looking but the the result should also return in an array becasue the result will not be a single value. which should look like ;

orginal data     result
10                   10    <---- first value
5                     15   <---- first value + second value
0                     15   <--- first + second + third value
25                   40   <--- first + second + third + fouth and so on

ayha
0
 
LVL 19

Assisted Solution

by:VoteyDisciple
VoteyDisciple earned 75 total points
ID: 17938940
What threw us off is that kind of notation is often used to express what the result of sending particular single inputs to the function should be.  "Send in 10, get out 10.  Send in 5, get out 15.  Et cetera."

Interpreting it as an array means that a simple modification to YZlat's solution is all that's needed:


    Protected Function Summation(ByVal arr As Integer) As Integer()
        Dim result As Integer(arr.Length - 1)
        Dim sum As Integer = 0

        For i As Integer = 0 To arr.length - 1
            sum += arr(i)
            result(i) = sum
        Next
        Return result
    End Function
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 35

Expert Comment

by:YZlat
ID: 17938958
try something like that:

Function Summation(ByVal arr() as Integer) As Integer()
        Dim result() As Integer
Dim subtotal as integer
        For i As Integer = 0 To arr.length - 1
            result(i)=subtotal
            subtotal = subtotal + arr(i)
        Next
return result
    End Function

0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 75 total points
ID: 17938964
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim values() As Integer = {10, 5, 0, 25}
        Dim totals() As Integer = AddValues(values)
        For Each total As Integer In totals
            Debug.WriteLine(total)
        Next
    End Sub

    Public Function AddValues(ByVal arr() As Integer) As Integer()
        Dim total As Integer
        Dim retArr(arr.Length - 1) As Integer
        For i As Integer = 0 To arr.GetUpperBound(0)
            total = total + arr(i)
            retArr(i) = total
        Next
        Return retArr
    End Function
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17938968
Hehe...guess I shoulda refreshed eh?  =)
0
 
LVL 35

Accepted Solution

by:
YZlat earned 100 total points
ID: 17939012
actually no, use this instead:

Function Summation(ByVal arr() As Integer) As Integer()
        Dim result(arr.Length - 1) As Integer
        Dim subtotal As Integer
        For i As Integer = 0 To arr.length - 1

            subtotal = subtotal + arr(i)
            result(i) = subtotal
        Next
        Return result
    End Function
0
 
LVL 7

Author Comment

by:ayha1999
ID: 17946834
Hi,

thanks. all of your solution works. how can I provide data to the array from datareader or dataset?

ayha
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17946977
for datareader

i=0
while datreader.Read
   myarray(i)=datareader("myfield")
   i+=1

end while

or for dataset

For i=0 to DataSet1.Tables(0).Rows.COunt-1
   myarray(i)=DataSet1.Tables(0).Rows(i).Item("myfield")
Next
0
 
LVL 7

Author Comment

by:ayha1999
ID: 17988336
Thanks for all of your help.

ayha
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now