Swift Program to Calculate Average Using Arrays


In this article, we will learn how to write a swift program to calculate averages using an array. Average is defined as the ratio of the sum of the elements present in the given sequence to the total number of elements available in the given sequence. The general formula of average is −

Average = (p1+p2+p3+..+pn)/n

Here we use the following methods to calculate the average using array −

  • Using pre−define functions

  • Without using predefine functions

Method 1: Using Pre-Define Functions

To find the average of the given array, we use reduce() method to find the sum of all the elements present dnd for a total number of elements present in the given array we use the count property. The reduce(_:_:) method is used to combine the given sequence according to the given closure.

Syntax

func reduce<result>(_initial result: result, 
_nextResult:(result, Self.Element) throws->result)rethrows->result

Here, the initial parameter is used for the initial accumulating value and passed in the nextResult the first time the closure is executed. Whereas nextResult is a closure that adds the accumulating value and the item of the sequence into a new accumulating value, it is further used in the next call of the nextResult closure.

Algorithm

  • Step 1 − Create an array of integer type.

  • Step 2 − Find the sum of all the elements of the array using reduce function.

var arraySum = myArray.reduce(0, +)
  • Step 3 − Calculate the array’s total number of elements using count property

var length = myArray.count
  • Step 4 − Find the average of the given array by dividing the sum and the total number of array.

var average = Double(arraySum)/Double(length)
  • Step 5 − Print the output.

Example

Following Swift program to calculate the average of the array using pre-defined functions.

import Foundation
import Glibc
 
// Creating an array of integer type
var myArray : [Int] = [3, 5, 6, 74, 32, 7, 31]

// Finding the sum of the given array elements
var arraySum = myArray.reduce(0, +)

// Calculating the total number of 
// elements present in the array
var length = myArray.count

// Finding the average
var average = Double(arraySum)/Double(length)

print("Array:", myArray)
print("Average:", average)

Output

Array: [3, 5, 6, 74, 32, 7, 31]
Average: 22.571428571428573

Here in the above code, we create an array of integer types. First, we find the sum of all the array elements using reduce(0,+) where 0 is the initial value which will add with the first element of the given array, and then the result will add to the next value and this process will continue till the last element. Now we calculate the total number of elements using the count property, then find the average by dividing the sum by the total number of elements and print the output.

Method 2: Without using Pre-Define Functions

We can also find the average of the given array using loops. Here we add all the elements of the given array using for loop and then find the average.

Algorithm

  • Step 1 − Create an array of integer type.

  • Step 2 − Find the sum of all the elements of the array using for loop.

for x in 0..<myArray.count{
   arraySum += myArray[x]
}
  • Step 3 − Calculate total number of elements of the array using count property.

var length = myArray.count
  • Step 4 − Find average of the given array by dividing sum and total number of array.

var average = Double(arraySum)/Double(length)
  • Step 5 − Print the output.

Example

Following Swift program to calculate the average of the array using loops.

import Foundation
import Glibc
 
// Creating an array of integer type
var myArray : [Int] = [4, 56, 78, 21, 5, 6, 76, 3, 21, 1]

// Finding the sum of the given array elements
var arraySum = 0

for x in 0..<myArray.count{
   arraySum += myArray[x]
}

// Calculating the total number of 
// elements present in the array
var length = myArray.count

// Finding the average
var average = Double(arraySum)/Double(length)

print("Array:", myArray)
print("Average:", average)

In the following example, we check whether the given array is empty or not using conditional statement.

Output

Array: [4, 56, 78, 21, 5, 6, 76, 3, 21, 1]
Average: 27.1

Here in the above code, we create an array of integer type. First we find the sum of the given array elements using for loop. Here for loop iterate through each element and add them together and store result into a variable. Now we find the total number of arrays present in the given array using count property, then find average by dividing sum by the total number of elements, and print the output.

Conclusion

So this is how we can find the average using array. Here we can either use pre-define function or using loops to find the average of the given array.

Updated on: 29-Dec-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements