473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

summing the elements of the an array

Hi

Can any body give me the syntax for summing the elements of the an
array , without looping

thanks

Jan 23 '07 #1
12 4022
I'm not sure what you are looking for. You could update an already computed
sum when you change the value of an array cell. Is this what you meant ? Or
do you meant using the ForEach method (that is your code calls the method
that does the looping for you).

Please be more specific in what you are trying to do (the general answer is
that you have to loop either explicitely or implicitely) or what you are
trying to solve (your current computation is too slow ?)

"neeraj" <ne********@gmail.coma écrit dans le message de news:
11**********************@a75g2000cwd.googlegroups. com...
Hi

Can any body give me the syntax for summing the elements of the an
array , without looping

thanks

Jan 23 '07 #2
I don't thing you can. If you don't want to do any looping, then
perhaps you could use a DataTable instead of an array and use it's
..Compute() method. Is there a reason you don't want to do a loop?

Thanks,

Seth Rowe
neeraj wrote:
Hi

Can any body give me the syntax for summing the elements of the an
array , without looping

thanks
Jan 23 '07 #3
Yes, of course :)

Dim values() As Integer = {1, 2, 3}
Dim sum As Integer = values(0) + values(1) + values(2)

:))

Serge
http://www.sergejusz.com

neeraj wrote:
Hi

Can any body give me the syntax for summing the elements of the an
array , without looping

thanks
Jan 23 '07 #4
Hi Patrice
Thanks for Reply

Actually I am receiving the data from stored procedure in an output
parameter

Like this "23,34,56,322,4445,3234,33344,3334,34344"

and these value also used in whole application as array like this

int [] rupees = {uotParm};

here we can not change the "sql" stored procedure to return the sum
of these value and also can not change the existing "vb.net" code
on whole application

now I want to sum of these value for an purpose, I have already got
the sum of these values by using for each loop but I want to know is
these any method available to get the sum of array aliment by without
use explicit looping.
Patrice wrote:
I'm not sure what you are looking for. You could update an already computed
sum when you change the value of an array cell. Is this what you meant ? Or
do you meant using the ForEach method (that is your code calls the method
that does the looping for you).

Please be more specific in what you are trying to do (the general answer is
that you have to loop either explicitely or implicitely) or what you are
trying to solve (your current computation is too slow ?)

"neeraj" <ne********@gmail.coma écrit dans le message de news:
11**********************@a75g2000cwd.googlegroups. com...
Hi

Can any body give me the syntax for summing the elements of the an
array , without looping

thanks
Jan 23 '07 #5

neeraj wrote:
Hi

Can any body give me the syntax for summing the elements of the an
array , without looping

thanks

WITHOUT looping? I'm not sure that's possible in VB. Maybe in LISP, but
I doubt it in VB...
if you had to you could write an array_sum procedure to HIDE the
looping...

Jan 23 '07 #6
Though this would be theorically possible (for example Array.ForEach loops
on an array but is likely more suitable for processing each cell separately
than performing an aggregate operation) it would be more an academic
challenge and I don't really see for now the benefit you expect from this
(you could always create a function that sums an arbitrary array and even
cache the result if the string is the same etc...). Also you'll have likely
anyway to update the code where you want to use this sum while the previous
application was using something else.

Finally LINQ will allow to perform SQL like operations on several sources
including arrays.

For now I would keep the current code unless you can explain what doesn't
work well with it...

----
Patrice

"neeraj" <ne********@gmail.coma écrit dans le message de news:
11**********************@a75g2000cwd.googlegroups. com...
Hi Patrice
Thanks for Reply

Actually I am receiving the data from stored procedure in an output
parameter

Like this "23,34,56,322,4445,3234,33344,3334,34344"

and these value also used in whole application as array like this

int [] rupees = {uotParm};

here we can not change the "sql" stored procedure to return the sum
of these value and also can not change the existing "vb.net" code
on whole application

now I want to sum of these value for an purpose, I have already got
the sum of these values by using for each loop but I want to know is
these any method available to get the sum of array aliment by without
use explicit looping.
Patrice wrote:
I'm not sure what you are looking for. You could update an already
computed
sum when you change the value of an array cell. Is this what you meant ?
Or
do you meant using the ForEach method (that is your code calls the method
that does the looping for you).

Please be more specific in what you are trying to do (the general answer
is
that you have to loop either explicitely or implicitely) or what you are
trying to solve (your current computation is too slow ?)

"neeraj" <ne********@gmail.coma écrit dans le message de news:
11**********************@a75g2000cwd.googlegroups. com...
Hi

Can any body give me the syntax for summing the elements of the an
array , without looping

thanks

Jan 23 '07 #7
This would work and it's not an explicit loop :-)

Private Sub Test()

Dim a(8) As Integer

a(0) = 23
a(1) = 34
a(2) = 56
a(3) = 322
a(4) = 4445
a(5) = 3234
a(6) = 33344
a(7) = 3334
a(8) = 34344

Debug.WriteLine(SumArray(a, 0).ToString)

End Sub
Private Function SumArray(ByVal a() As Integer, ByVal i As Integer) As
Integer
Dim Result As Integer = a(i)
i += 1
If (i < a.Length) Then
Result += SumArray(a, i)
End If
Return Result
End Function
Tom
"neeraj" <ne********@gmail.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
Hi

Can any body give me the syntax for summing the elements of the an
array , without looping

thanks

Jan 23 '07 #8


"Tom Leylan" <tl*****@nospam.netwrote in message
news:eE**************@TK2MSFTNGP05.phx.gbl...
This would work and it's not an explicit loop :-)

Private Sub Test()

Dim a(8) As Integer

a(0) = 23
a(1) = 34
a(2) = 56
a(3) = 322
a(4) = 4445
a(5) = 3234
a(6) = 33344
a(7) = 3334
a(8) = 34344

Debug.WriteLine(SumArray(a, 0).ToString)

End Sub
Private Function SumArray(ByVal a() As Integer, ByVal i As Integer) As
Integer
Dim Result As Integer = a(i)
i += 1
If (i < a.Length) Then
Result += SumArray(a, i)
End If
Return Result
End Function
Tom
"neeraj" <ne********@gmail.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
>Hi

Can any body give me the syntax for summing the elements of the an
array , without looping

thanks

LOL, nice one...recursion in place of looping....

Mythran
Jan 23 '07 #9
lmao

or...incase you have more than 3 values...

Dim values() as Integer = {1,2,3,...,n}
Dim Sum as Integer = values(0) + values(1) + values(2) + ... + values(n)

"sergejusz" <se*******@gmail.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
Yes, of course :)

Dim values() As Integer = {1, 2, 3}
Dim sum As Integer = values(0) + values(1) + values(2)

:))

Serge
http://www.sergejusz.com

neeraj wrote:
>Hi

Can any body give me the syntax for summing the elements of the an
array , without looping

thanks

Jan 23 '07 #10

"neeraj" <ne********@gmail.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
:
: Hi
:
: Can any body give me the syntax for summing the elements of the an
: array , without looping
:
: thanks
How do you feel about recursion?

================================================== ===
Option Strict On

Imports System

Public Module [module]
public sub main
Dim arr() As Integer = {1, 3, 5, 7, 9}
Console.WriteLine(Sum(arr))
End sub

Function Sum(arr() As Integer) As Integer
Return Sum(arr, LBound(arr))
End Function

Function Sum(arr() As Integer, ndx As Integer) As Integer
If ndx <= UBound(arr) Then
Return arr(ndx) + Sum(arr, ndx + 1)
Else
Return 0
End If
End Function
End Module
================================================== ===
Ralf
--
--
----------------------------------------------------------
* ^~^ ^~^ *
* _ {~ ~} {~ ~} _ *
* /_``>*< >*<''_\ *
* (\--_)++) (++(_--/) *
----------------------------------------------------------
There are no advanced students in Aikido - there are only
competent beginners. There are no advanced techniques -
only the correct application of basic principles.
Jan 23 '07 #11
Seth,

I have the same curiousity and in my idea all, seen the given answers.

Cor

"rowe_newsgroups" <ro********@yahoo.comschreef in bericht
news:11**********************@q2g2000cwa.googlegro ups.com...
>I don't thing you can. If you don't want to do any looping, then
perhaps you could use a DataTable instead of an array and use it's
.Compute() method. Is there a reason you don't want to do a loop?

Thanks,

Seth Rowe
neeraj wrote:
>Hi

Can any body give me the syntax for summing the elements of the an
array , without looping

thanks

Jan 23 '07 #12
very funny :-))

On Jan 23, 7:54 pm, "sergejusz" <sergej...@gmail.comwrote:
Yes, of course :)

Dim values() As Integer = {1, 2, 3}
Dim sum As Integer = values(0) + values(1) + values(2)

:))

Sergehttp://www.sergejusz.com

neeraj wrote:
Hi
Can any body give me the syntax for summing the elements of the an
array , without looping
thanks- Hide quoted text -- Show quoted text -
Jan 24 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Yaroslav Bulatov | last post by:
I made an array of 10 million floats timed how long it takes to sum the elements, here's what I got (millis): gcc -O2: 21 Python with numarray: 104 Python with Numeric: 302...
2
by: SunMan | last post by:
Hello! I am trying to create a program that will ask for a user to enter a series of letters (codes) and then print out a table that shows the codes in decending frequency. Only letters will be...
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
3
by: Newcomsas | last post by:
Hello, I'm trying to solve a problem with JS textbox array without success. I have two buttons in my page: PLUS and MINUS; at every click on PLUS a new textbox named 'dear' is generated. So, if...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
2
by: MrL8Knight | last post by:
I am building a simple shopping cart and I am having problems trying to add the costs of the items to generate a total cost. I am new at this so forgive me if my technical verbiage isn’t the...
8
by: highroller152 | last post by:
Not to step on anyone, but in reference to this thread on summing odds and evens, why not just use the 'continue' statement? So adding up the odds would look something like this: -on some event-...
7
by: lethek39 | last post by:
Hey I have been trying to figure out how to sum rows and columns in a matrix square. I also have been trying to get the program to list the numbers of the diagonal in the matrix. So far this is the...
19
by: arnuld | last post by:
/* C++ Primer - 4/e * chapter 4- Arrays & Pointers, exercise 4.28 * STATEMENT * write a programme to read the standard input and build a vector of integers from values that are read....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.