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.

import Foundation import Glibc // Creating an array of integer type var arrNums = [23, 10, 19, 20] 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] } print("Sum of all the elements:", sum)

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.

import Foundation import Glibc // Function to find the sum of // all the elements of the array func arraySum(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] } return sum } // Creating an array of integer type var arrVals = [27, 10, 200, 400, 600, 700] print("Original Array:", arrVals) print("Sum of all the elements:", arraySum(arr: arrVals))

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.

Updated on: 20-Oct-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements