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.

import Foundation import Glibc // Creating an array of integer type var arrNums = [2, 45, 12, 49, 92] print("Original Array:", arrNums) var sum = 0 // Finding the sum of all the // elements of the array for x in 0..<arrNums.count{ sum += arrNums[x] } // Finding the mean var mean = sum/arrNums.count print("Mean:", mean)

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.

import Foundation import Glibc // Function to find the mean of the given array func arrayMean(arr: [Int]) -> Int{ let arrNums = arr var sum = 0 // Finding the sum of all the // elements of the array for x in 0..<arrNums.count{ sum += arrNums[x] } // Returning the mean return sum/arrNums.count } // Creating an array of integer type var arrVals = [40, 20, 60, 80, 30] print("Original Array:", arrVals) print("Mean:", arrayMean(arr: arrVals))

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.

Updated on: 20-Oct-2022

159 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements