Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Swift Program to Calculate the sum of Elements in a Given Array
This tutorial will discuss how to write swift program to calculate the sum of elements in a given array.
An array is an ordered collection which is used to store same type of data. For example, if any array is of integer type then it will only store integers, you are strictly not allowed to store elements of other data types like string, float, etc.
Syntax
Following is the syntax for an array ?
var array1 = [val1, val2, val3, ?] Or var array2 = [Type]()
Suppose we have an array of integer type: [20, 30, 40, 50]. Now we calculate the sum of the elements by adding them all together with the help of + operator. 20+30+40+50 = 140. Hence the sum of all the elements of the array is 140.
Below is a demonstration of the same ?
Input
Suppose our given input is ?
Myarr = [100, 1000, 10000, 100000]
Output
The desired output would be ?
Sum = 111100
Algorithm
Following is the algorithm ?
Step 1 ? Create an array of integer type with values.
Step 2 ? Declare a variable to store the sum of the elements.
Step 3 ? Run a for loop from 0 to less than the size of the array. Or we can say iterate through each element and find their sum.
for x in 0..<arrNums.count{
sum += arrNums[x]
}
Step 4 ? Print the output
Example 1
The following program shows how to calculate the sum of the elements of the array.
<div class="execute"></div><div class="code-mirror language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation
<span class="token keyword">import</span> Glibc
<span class="token comment">// Creating an array of integer type</span>
<span class="token keyword">var</span> arrNums <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">23</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">19</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">]</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Original Array:"</span><span class="token punctuation">,</span> arrNums<span class="token punctuation">)</span>
<span class="token keyword">var</span> sum <span class="token operator">=</span> <span class="token number">0</span>
<span class="token comment">// Finding the sum of all the</span>
<span class="token comment">// elements of the array</span>
<span class="token keyword">for</span> x <span class="token keyword">in</span> <span class="token number">0.</span><span class="token punctuation">.</span><span class="token operator"><</span>arrNums<span class="token punctuation">.</span>count<span class="token punctuation">{</span>
sum <span class="token operator">+=</span> arrNums<span class="token punctuation">[</span>x<span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Sum of all the elements:"</span><span class="token punctuation">,</span> sum<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
Original Array: [23, 10, 19, 20] Sum of all the elements: 72
Here, in the above code, we have an integer type array. Now we iterate through each element of the array to find their sum and store the result to sum variable ?
for x in 0..<arrNums.count{
sum += arrNums[x]
}
So the working of the above code is ?
sum = 0
1st iteration: sum = 0 + arrNums[0] = 0 + 23 = 23
2nd iteration: sum = 23 + arrNums[1] = 23 + 10 = 33
3rd iteration: sum = 33 + arrNums[2] = 33 + 19 = 52
4th iteration: sum = 52 + arrNums[3] = 52 + 20 = 72
Hence the sum of all the elements of the array(that is [23, 10, 19, 20]) is 72.
Example 2
The following program shows how to calculate the sum of the elements of the array.
<div class="execute"></div><div class="code-mirror language-javascript" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">import</span> Foundation
<span class="token keyword">import</span> Glibc
<span class="token comment">// Function to find the sum of</span>
<span class="token comment">// all the elements of the array</span>
func <span class="token function">arraySum</span><span class="token punctuation">(</span>arr<span class="token operator">:</span> <span class="token punctuation">[</span>Int<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> Int<span class="token punctuation">{</span>
<span class="token keyword">let</span> arrNums <span class="token operator">=</span> arr
<span class="token keyword">var</span> sum <span class="token operator">=</span> <span class="token number">0</span>
<span class="token comment">// Finding the sum of all the</span>
<span class="token comment">// elements of the array</span>
<span class="token keyword">for</span> x <span class="token keyword">in</span> <span class="token number">0.</span><span class="token punctuation">.</span><span class="token operator"><</span>arrNums<span class="token punctuation">.</span>count<span class="token punctuation">{</span>
sum <span class="token operator">+=</span> arrNums<span class="token punctuation">[</span>x<span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span class="token keyword">return</span> sum
<span class="token punctuation">}</span>
<span class="token comment">// Creating an array of integer type</span>
<span class="token keyword">var</span> arrVals <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">27</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">400</span><span class="token punctuation">,</span> <span class="token number">600</span><span class="token punctuation">,</span> <span class="token number">700</span><span class="token punctuation">]</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Original Array:"</span><span class="token punctuation">,</span> arrVals<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Sum of all the elements:"</span><span class="token punctuation">,</span> <span class="token function">arraySum</span><span class="token punctuation">(</span>arr<span class="token operator">:</span> arrVals<span class="token punctuation">)</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
Original Array: [27, 10, 200, 400, 600, 700] Sum of all the elements: 1937
Here, in the above code, we have an array named arrVals of integer type. Now to find the sum of elements of arrVals we create a function named arraySum(). This function will return the sum of all the elements present in the given array. So the resultant sum is 1937.
