Algorithm to sum all elements in array.

Iterative version:

IN: Array of nubmers, size of array.
OUT: Sum of numbers in given array.
1. Initialise variables: i = 0, sum = 0.
2. Add array[i] to sum, increase i by 1.
3. Check if i is less than size, if true go to step 2.
4. Return sum.

Recursion version:

IN: Array of nubmers, size of array.
OUT: Sum of numbers in given array.
1. Check if size of array is equal to 0, if true return 0.
2. Return this algortihm for ((array, size - 1) + array[size - 1])

 

Sample Output:

IN:
1, 2, 5, 7
1, -1, 5, -3
3.14, 2.56, 9.81, 6.5, 7.312
OUT:
15
2
29,322
C++ Implementation
Sum Elements Of Array Algorithm
Tagged on:

Leave a Reply

Your email address will not be published. Required fields are marked *

This website stores some user agent data. These data are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to opt-out of any future tracking, a cookie will be set up in your browser to remember this choice for one year. I Agree, Deny
504

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.

Close