Swift Program to Find Geometric Mean of the Numbers

This tutorial will discuss how to write swift program to find the geometric mean of the numbers.

Geometric mean also known as GM. Geometric mean is defined as the xth root of the product of x numbers. Suppose we have x elements(that are, a1, a2, a3, ....ax) in the given array, then the geometric mean is ?

GM = (a1 * a2 * a3 * a4 * ....*ax)1/x

Below is a demonstration of the same ?

Input

Suppose our given input is ?

MyVal = [34, 67, 2, 45, 8, 12]

Output

The desired output would be ?

Geometric Mean = 16.413138687438

Explanation

(34 * 67 * 2 * 45 * 8 * 12)1/6 = (19681920)1/6 = 16.413138687438

Formula

Following is the formula for geometric mean ?

GM = (a1 * a2 * a3 * a4 * ....*ax)1/x

Algorithm

Following is the algorithm ?

  • Step 1 ? Create an array.

  • Step 2 ? Declare a variable to store the product 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 product.

for x in 0..<arrNums.count{
   sum += arrNums[x]
}
  • Step 4? Find the geometric mean by calculating the xth root of the product of x numbers.

var GeometricMean = pow(arrProduct, 1/size)
  • Step 5 ? Print the output

Example 1

The following program shows how to find the geometric mean of the numbers.

<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 array</span>
<span class="token keyword">var</span> MyNumber <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">3.4</span><span class="token punctuation">,</span> <span class="token number">5.6</span><span class="token punctuation">,</span> <span class="token number">1.2</span><span class="token punctuation">,</span> <span class="token number">4.0</span><span class="token punctuation">]</span>

<span class="token comment">// To store the product</span>
<span class="token keyword">var</span> arrProduct <span class="token operator">=</span> <span class="token number">1.0</span>

<span class="token comment">// Finding the product of all the numbers</span>
<span class="token keyword">for</span> y <span class="token keyword">in</span> <span class="token number">0.</span><span class="token punctuation">.</span><span class="token operator"><</span>MyNumber<span class="token punctuation">.</span>count<span class="token punctuation">{</span>
   arrProduct <span class="token operator">=</span> arrProduct <span class="token operator">*</span> MyNumber<span class="token punctuation">[</span>y<span class="token punctuation">]</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> size <span class="token operator">=</span> <span class="token function">Double</span><span class="token punctuation">(</span>MyNumber<span class="token punctuation">.</span>count<span class="token punctuation">)</span>

<span class="token comment">// Finding the geometric mean</span>
<span class="token keyword">var</span> GeometricMean <span class="token operator">=</span> <span class="token function">pow</span><span class="token punctuation">(</span>arrProduct<span class="token punctuation">,</span> <span class="token number">1</span><span class="token operator">/</span>size<span class="token punctuation">)</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Array:"</span><span class="token punctuation">,</span> MyNumber<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Geometric Mean:"</span><span class="token punctuation">,</span> GeometricMean<span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

Output

Array: [3.4, 5.6, 1.2, 4.0]
Geometric Mean: 3.091911434311368

Example 2

The following program shows how to find the geometric mean of the numbers.

<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 geometric mean</span>
func <span class="token function">geoMean</span><span class="token punctuation">(</span>arr<span class="token operator">:</span> <span class="token punctuation">[</span>Double<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span>Double<span class="token punctuation">{</span>

   <span class="token keyword">let</span> MyNumber <span class="token operator">=</span> arr
   
   <span class="token comment">// To store the product</span>
   <span class="token keyword">var</span> arrProduct <span class="token operator">=</span> <span class="token number">1.0</span>
   
   <span class="token comment">// Finding the product of all the numbers</span>
   <span class="token keyword">for</span> y <span class="token keyword">in</span> <span class="token number">0.</span><span class="token punctuation">.</span><span class="token operator"><</span>MyNumber<span class="token punctuation">.</span>count<span class="token punctuation">{</span>
      arrProduct <span class="token operator">=</span> arrProduct <span class="token operator">*</span> MyNumber<span class="token punctuation">[</span>y<span class="token punctuation">]</span>
   <span class="token punctuation">}</span>
   
   <span class="token keyword">let</span> size <span class="token operator">=</span> <span class="token function">Double</span><span class="token punctuation">(</span>MyNumber<span class="token punctuation">.</span>count<span class="token punctuation">)</span>
   
   <span class="token comment">// Finding the geometric mean</span>
   <span class="token keyword">let</span> GeometricMean <span class="token operator">=</span> <span class="token function">pow</span><span class="token punctuation">(</span>arrProduct<span class="token punctuation">,</span> <span class="token number">1</span><span class="token operator">/</span>size<span class="token punctuation">)</span>
   <span class="token keyword">return</span> GeometricMean
<span class="token punctuation">}</span>

<span class="token comment">// Creating an array</span>
<span class="token keyword">var</span> Myval <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">5.6</span><span class="token punctuation">,</span> <span class="token number">8.9</span><span class="token punctuation">,</span> <span class="token number">12.3</span><span class="token punctuation">,</span> <span class="token number">5.6</span><span class="token punctuation">,</span> <span class="token number">34.5</span><span class="token punctuation">]</span>

<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Array:"</span><span class="token punctuation">,</span> Myval<span class="token punctuation">)</span>
<span class="token function">print</span><span class="token punctuation">(</span><span class="token string">"Geometric Mean:"</span><span class="token punctuation">,</span> <span class="token function">geoMean</span><span class="token punctuation">(</span>arr<span class="token operator">:</span> Myval<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

Array: [5.6, 8.9, 12.3, 5.6, 34.5]
Geometric Mean: 10.344227262841542

Here, in the above code, we have an array named Myval of double type. Now to find the geometric mean of Myval, so we create a function named geoMean(). This function will return the geometric mean. So the resultant geometric mean is 10.344227262841542.

Updated on: 2022-10-20T08:14:29+05:30

441 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements