COMP2247 - Algorithms and Data Structures

Syllabus Software Installation Assignments Tests
Lectures and Labs
**Back to Full Menu**

Recursion Factorial Program Fibonacci Program Integer Power Program Tower of Hanoi Program Max Element Program Palindrome Program Reverse String Program Array Sum Program



ArraySum Program


Description

The program uses a recursive method to calculate the total values of an integer array's elements.

Associated Concepts:

Recursion

Algorithm:

main method:

Create an array with a number of integer elements.

Call the recursive method addElements() to calculate the total value of array elements, and pass the array and the size of the array as the arguments.

addElements method definition:

If the array has only one element, return it.

Otherwise

Call addElements() method recursively to get the subtotal of the array elements from 0 up to the second last element

Add the last element to the subtotal

Source Code



Sample Run



Video