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 Find Mean of an Unsorted Array
This tutorial will discuss how to write swift program to find mean of an unsorted 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]()
Mean represent the average of the given numbers. It is calculated by dividing the sum of the given numbers by the total number. For example, we have the following numbers: 10, 20, 30, 80, 40. So the mean of the given numbers is ?
Mean = 10+20+30+80+40/5 = 180/5 = 36
Below is a demonstration of the same ?
Input
Suppose our given input is ?
MyNums = [23, 45, 67, 78, 12]
Output
The desired output would be ?
Mean of the given array = 45
Formula
Following is the formula of mean ?
Mean = sum of all the numbers/total count of the number
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 ? Find the mean by dividing the sum of all the elements with the total number of elements.
Step 5 ? Print the output
Example 1
The following program shows how to calculate the mean of an unsorted 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">2</span><span class="token punctuation">,</span> <span class="token number">45</span><span class="token punctuation">,</span> <span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">49</span><span class="token punctuation">,</span> <span class="token number">92</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 comment">// Finding the mean</span>
<span class="token keyword">var</span> mean <span class="token operator">=</span> sum<span class="token operator">/</span>arrNums<span class="token punctuation">.</span>count
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Mean:"</span><span class="token punctuation">,</span> mean<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
Original Array: [2, 45, 12, 49, 92] Mean: 40
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 after that we divide the sum with total number of elements present in the array to find the mean ?
for x in 0..<arrNums.count{
sum += arrNums[x]
}
var mean = sum/arrNums.count
So the working of the above code is ?
sum = 0
1st iteration: sum = 0 + arrNums[0] = 0 + 2 = 2
2nd iteration: sum = 2 + arrNums[1] = 2 + 45 = 47
3rd iteration: sum = 47 + arrNums[2] = 47 + 12 = 59
4th iteration: sum = 59 + arrNums[3] = 59 + 49 = 108
5th iteration: sum = 108 + arrNums[4] = 108 + 92 = 200
Sum = 200
Mean = sum/arrNums.count = 200/5 = 40
Hence, the mean of the array(that is [2, 45, 12, 49, 92]) is 40.
Example 2
The following program shows how to calculate the mean of an unsorted 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 mean of the given array</span>
func <span class="token function">arrayMean</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 comment">// Returning the mean</span>
<span class="token keyword">return</span> sum<span class="token operator">/</span>arrNums<span class="token punctuation">.</span>count
<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">40</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token number">60</span><span class="token punctuation">,</span> <span class="token number">80</span><span class="token punctuation">,</span> <span class="token number">30</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">"Mean:"</span><span class="token punctuation">,</span> <span class="token function">arrayMean</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: [40, 20, 60, 80, 30] Mean: 46
Here, in the above code, we have an array named arrVals of integer type. Now to find the mean of arrVals we create a function named arrayMean(). This function will return the mean by dividing the sum of all the elements present in the array by total number of elements. So the resultant mean is 46.
