SlideShare a Scribd company logo
SWIFT
PROGRAMS ON ARRAY
INDEX
S.NO. PROGRAMS SIGNATURE
1 Program to print the numbers in an array horizontally
2 Program to search the number from a 1 D array using linear search
3 Program to search the number from a 1 D array using binary search
4 Program to insert the number in a 1 D array using Insertion sorted array
5 Program to insert the number in a 1 D array using Deletion sorted array
6 Program to sort the number in an array using bubble sort
7 Program to sort the number in an array using selection sort
8 Write a function Alter(), with array and size as a parameter ,which
should change all the multiples of 5 in the array to 5 and rest of the
element as 1
9 Write a function modify() with array and size as a parameter, which
should reposition the content after swapping each adjacent pair of
number in it
10 Write a function Large() with array and size as a parameter, which
should display the largest and second largest by returning both from an
array
11 Write a function addup() with array and size as a parameter,in which all
even position(0,2,4,…..) of the array should be added with the content
of element in the next position and odd position(1,3,5,…..) elements
should be increment by 10
12 Write a function twotoone() with array and size as an parameters,in
which accept two array X[] and Y[] and transfer all X[] numbers in even
places of Z[] and all Y[] numbers in odd places of Z[].
13 Write a function convert() with array and size as a parameter,which
reposition all the elements of the array by shifting each of them one to
one position before and by shifting the first elements to the last
position
14 Write a function SWAP() with array and size as a parameter,which swap
the numbers on the basis of example given:
If array A no : 10,20,30,40,50,60 the output after swapping
20,10,40,30,60,50
15 Write a function SWAP2() with array and size as a parameter,which
swap the numbers on the basis of example given:
If array A no : 10,20,30,40,50,60 the output after swapping
40,50,60,10,20,30
16 Write a function merge() with three array as a parameter array A,B, and
C with their size M , N , and M+N , where A is ascending order , B is
descending order and C accept numbers from A and B in ascending
order.
17 Write a function dispTen() with 2D array no and size as a parameter and
display only those numbers which are divisible by 10
18 Write a function rowsum() with 2D array A and size as a parameter and
display the sum of each row
20 Write a function diagonalsum() with 2D array A and size as a parameter
and display the sum
21 program to find the largest no from a 1D array of 10 numbers
22 Write a function swaparr() with 2D array A and size as a parameter and
swap or interchange the first row with the last row elements.
23 Write a function lefttrg() with 2D array A and size as a parameter and
display the output as given
Example: output
1 2 3 4
6 4 5 6 6
2 1 5 4 2 1
1 2 3 5 1 2 3
24 Write a function summatrix() with 2D array A , B , C and size as a
parameter, and display the sum of two matrix in C
25 Write a function copyarr() with 2D array A and B as 1D array and size as
a parameter,display the output as given below:
Example: if array B values are 1,2,3,4,5
Array A store values as:
1,2,3,4,5
1,2,3,4,0
1,2,3,0,0
1,2,0,0,0
1,0,0,0,0
//Program to print the numbers in an array horizontally
func display(no:[Int], k:Int)
{
var x = 0
while x < k
{
print("(no[x]), ",terminator:"")
x = x + 1
}
print("")
}
var n:[Int] = [10,20,30,40,50,60,70,80,90,100 ]
display(no:n, k:10)
----------------------------output ----------------------------------
10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
// Program to search the number from a 1 D array using linear search
func lsearch(no:[Int], src:Int, k:Int) -> Int
{
var tmp = -1
var x = 0
while x < k
{
if no[x] == src
{
tmp = x
break
}
x = x + 1
}
return tmp
}
var n:[Int] = [40,10,51,86,19,87,70,3,14,100 ]
var rs = lsearch(no:n, src:19, k:10)
if rs < 0
{
print("no not found")
}else
{
rs = rs + 1
print("no found at pos =(rs)")
}
--------------------------output----------------------------
no found at pos =5
// Program to search the number from a 1 D array using binary search
func bsearch(no:[Int], src:Int, k:Int) -> Int
{
var tmp = -1
var x = 0
var beg = 0 , last = k - 1 , mid = 0
while beg < last
{
mid = (beg + last) / 2
if no[mid] == src
{
tmp = x
break
}else
{ if no[mid] > src
{
last = mid - 1
}else
{
beg = mid + 1
}
}
x = x + 1
}
return tmp
}
var n:[Int] = [40,45,58,75,86,89,92,97,99,120]
var rs = bsearch(no:n, src:58, k:10)
if rs < 0
{
print("no not found")
}else
{
rs = rs + 1
print("no found at pos =(rs)")
}
---------------output------------------
no found at pos =3
// Program to insert the number in a 1 D array using Insertion sorted array
func insertionUN(no:[Int],ser:Int,k:Int) -> Int
{
var pos:Int = -1
var flag = -1
var x = 0
let last = k - 1
while x < k
{
if no[0] >= ser
{
flag = 0
pos = 0
break
}else if no[last] <= ser
{
pos = last
flag = 1
break;
}
else if no[x] < ser && no[x + 1] > ser
{
flag = 2
pos = x + 1
break
}
x = x + 1
}
if flag == 0
{
pos = 0
}else if flag == 1
{
pos = pos + 1
}
return pos
}
var search = 67
print("before insertion")
var n:[Int] = [10,20,30,40,50,60,70,80]
var k1 = insertionUN(no:n, ser:search, k:n.count)
print("after insertion")
n.insert(search,at:k1)
print(n)
----------------------------output------------------------------
before insertion
[10, 20, 30, 40, 50, 60, 70, 80]
after insertion
[10, 20, 30, 40, 50, 60, 67, 70, 80]
// Program to insert the number in a 1 D array using Deletion sorted array
func deletionUN(no:[Int],ser:Int,k:Int) -> Int
{
var pos:Int = -1
var x = 0
while x < k
{
if no[x] == ser
{
pos = x
break
}
x = x + 1
}
return pos
}
var search = 40
var y = 0
var n:[Int] = [10,20,30,40,50,60,70,80]
print("before deletion array size = (n.count)")
print(n)
var k1 = deletionUN(no:n, ser:search, k:n.count)
if k1 > 0
{
y = k1
while y < n.count - 1
{
n[y] = n[y + 1]
y = y + 1
}
}
n.remove(at:n.count - 1)
print("after deletion array size =(n.count)")
print(n)
-------output----------------
before deletion array size = 8
[10, 20, 30, 40, 50, 60, 70, 80]
after deletion array size =7
[10, 20, 30, 50, 60, 70, 80]
// Program to sort the number in an array using bubble sort
func sortbubble(no:inout [Int],k:Int)
{
var x = 0 , y = 0
var tmp = 0
while x < k
{
y = 0
while y < k - 1
{
if no[y] > no[y + 1]
{
tmp = no[y]
no[y] = no[y + 1]
no[y + 1] = tmp
}
y = y + 1
}
x = x + 1
}
}
var n:[Int] = [60,2,33,55,5,22,1,8]
print("before sorting array ")
print(n)
sortbubble(no: &n, k:n.count)
print("after sorting array")
print(n)
----------------------------------output------------------------------------
before sorting array
[60, 2, 33, 55, 5, 22, 1, 8]
after sorting array
[1, 2, 5, 8, 22, 33, 55, 60]
// Program to sort the number in an array using selection sort
func sortselection(no:inout [Int],k:Int)
{
var x = 0 , y = 0
var tmp = 0
while x < k
{
y = x + 1
while y < k
{
if no[x] > no[y]
{
tmp = no[x]
no[x] = no[y]
no[y] = tmp
}
y = y + 1
}
x = x + 1
}
}
var n:[Int] = [60,2,33,55,5,22,1,8]
print("before sorting array ")
print(n)
sortselection(no: &n, k:n.count)
print("after sorting array")
print(n)
-----------------------------output------------------------------
before sorting array
[60, 2, 33, 55, 5, 22, 1, 8]
after sorting array
[1, 2, 5, 8, 22, 33, 55, 60]
/* Write a function Alter(), with array and size as a parameter ,which should
change all the multiples of 5 in the array to 5 and rest of the element as 1 */
func alter(no:inout [Int],k:Int)
{
var x = 0
while x < k
{
if no[x] % 5 == 0
{
no [x] = 5
}else
{
no[x] = 1
}
x = x + 1
}
}
var n:[Int] = [55,43,20,16,39,90,83,40,48,25]
print("before ")
print(n)
alter(no: &n, k:n.count)
print("after")
print(n)
------------------------output-----------------------------
before
[55, 43, 20, 16, 39, 90, 83, 40, 48, 25]
after
[5, 1, 5, 1, 1, 5, 1, 5, 1, 5]
/* Write a function modify() with array and size as a parameter, which should
reposition the content after swapping each adjacent pair of number in it */
func modify(no:inout [Int],k:Int)
{
var x = 0 , tmp = 0
while x < k
{
tmp = no[x]
no[x] = no[x + 2]
no[x + 2] = tmp
if x % 4 >= 1
{
x = x + 2
}
x = x + 1
}
}
var n:[Int] = [86,93,40,36,52,21,70,10]
print("before ")
print(n)
modify(no: &n, k:n.count)
print("after")
print(n)
----------------------------output-------------------------------------------------
before
[86, 93, 40, 36, 52, 21, 70, 10]
after
[40, 36, 86, 93, 70, 10, 52
/*Write a function Large() with array and size as a parameter, which should
display the largest and second largest by returning both from an array.*/
func large(no:inout [Int],k:Int) -> ( Int , Int )
{
var x = 0 , l = no[0] , sec1 = no[0]
while x < k
{
if no[x] > l
{
sec1 = l
l = no[x]
}else if no[x] > sec1
{
sec1 = no[x]
}
x = x + 1
}
return (l , sec1)
}
var n:[Int] = [86,93,40,136,52,21,70,10]
var (k , s ) = large(no: &n, k:n.count)
print("Largest no = (k) and second large = (s)")
--------------------------------output------------------------------------
Largest no = 136 and second large = 93
/* Write a function addup() with array and size as a parameter,in which all even
position(0,2,4,…..) of the array should be added with the content of element in
the next position and odd position(1,3,5,…..) elements should be increment by 10
*/
func addup(no:inout [Int] , k:Int)
{
var x:Int = 0
while x < k
{
if no[x] % 2 == 0
{
no[x] = no[x] + no[x + 1]
}else
{
no[x] = no[x] + 10
}
x = x + 1
}
}
var n:[Int] = [23,30,45,10,15,25,45,10,56,78,33]
print("before ")
print(n)
addup(no: &n, k:n.count)
print("after")
print(n)
----------------------------output--------------------------------
before
[23, 30, 45, 10, 15, 25, 45, 10, 56, 78, 33]
after
[33, 75, 55, 25, 25, 35, 55, 66, 134, 111, 43]
/* Write a function twotoone() with array and size as an parameters,in
which accept two array X[] and Y[] and transfer all X[] numbers in even
places of Z[] and all Y[] numbers in odd places of Z[]. */
func twotoone(z:inout [Int] ,x:[Int] ,y:[Int] , k:Int)
{
var a:Int = 0 , b:Int = 0
var c:Int = 0
while c < k
{
if c % 2 == 0
{
z.insert(x[a],at:c)
a = a + 1
}else
{
z.insert(y[b],at:c)
b = b + 1
}
c = c + 1
}}
var Z:[Int] = []
var X:[Int] = [10,20,30,40,50]
var Y:[Int] = [11,12,13,14,15]
print("before array Z")
print(Z)
print("before array X")
print(X)
print("before array Y")
print(Y)
twotoone(z:&Z, x:X, y:Y, k:10)
print("after")
print(Z)
---------------output---------------------
before array Z
[]
before array X
[10, 20, 30, 40, 50]
before array Y
[11, 12, 13, 14, 15]
After array Z
[10, 11, 20, 12, 30, 13, 40, 14, 50,
15]
/* Write a function convert() with array and size as a parameter,which
reposition all the elements of the array by shifting each of them one to
one position before and by shifting the first elements to the last
position*/
func convert(no:inout [Int] ,k:Int ,size:Int)
{
var tmp:Int = 0
var last:Int = size - 1
var x:Int = 1
var y:Int = 0
while x <= k
{
tmp = no[0]
y = 0
while y < size - 1
{
no[y] = no[y + 1]
y = y + 1
}
no[last] = tmp
x = x + 1
}
}
var X:[Int] = [10,20,30,40,50,60,70,80]
print("before")
print(X)
convert(no: &X, k:2, size:X.count)
print("After")
print(X)
--------------------------------------output----------------------------------------
before
[10, 20, 30, 40, 50, 60, 70, 80]
After
[30, 40, 50, 60, 70, 80, 10, 20]
/*Write a function SWAP() with array and size as a parameter,which
swap the numbers on the basis of example given:
If array A no : 10,20,30,40,50,60 the output after swapping
20,10,40,30,60,50 */
func swap(no:inout [Int] ,size:Int)
{
var tmp:Int = 0
var y:Int = 0
while y < size - 1
{
tmp = no[y]
no[y] = no[y + 1]
no[y + 1] = tmp
y = y + 2
}
}
var X:[Int] = [10,20,30,40,50,60]
print("before")
print(X)
swap(no: &X, size:X.count)
print("After")
print(X)
------------------------------output---------------------------
before
[10, 20, 30, 40, 50, 60]
After
[20, 10, 40, 30, 60, 50]
/*Write a function SWAP2() with array and size as a parameter,which
swap the numbers on the basis of example given:
If array A no : 10,20,30,40,50,60 the output after swapping
40,50,60,10,20,30 */
func swap2(no:inout [Int] ,size:Int)
{
var tmp:Int = 0
var y:Int = 0
var x:Int = size / 2
while y < size / 2
{
tmp = no[y]
no[y] = no[x]
no[x] = tmp
y = y + 1
x = x + 1
}
}
var X:[Int] = [10,20,30,40,50,60]
print("before")
print(X)
swap(no: &X, size:X.count)
print("After")
print(X)
---------------------------output---------------------------
before
[10, 20, 30, 40, 50, 60]
After
[40, 50, 60, 10, 20, 30]
/* Write a function merge() with three array as a parameter array A,B,
and C with their size M , N , and M+N , where A is ascending order , B
is descending order and C accept numbers from A and B in ascending
order. */
func merge(A1:[Int],B1:[Int],C1:inout [Int], M:Int, N:Int)
{
var x = 0
var a = 0
var k = 0
// var y = ( M + N ) - 1
var y = N - 1
while x < M && y >= 0
{
if A1[x] <= B1[y]
{
C1.append(A1[x])
k = k + 1
x = x + 1
}else
{
C1.append(B1[y])
k = k + 1
y = y - 1
}
}
if x < M
{
a = x
while a < M
{
C1.append(A1[a])
a = a + 1
}
}
if y >= 0
{
a = y
while a >= 0
{
C1.append(B1[a])
a = a - 1
}
}
}
var A:[Int] = [1,2,3,4,5,6,9,10,12]
var B:[Int] = [9,8,7,5,3,2,1]
var C:[Int] = []
print("before A")
print(A)
print("b numbers")
print(B)
merge(A1: A,B1: B,C1: &C,M:A.count , N:B.count)
print("After")
print(C)
----------------------------------output--------------------------------------
before A
[1, 2, 3, 4, 5, 6, 9, 10, 12]
b numbers
[9, 8, 7, 5, 3, 2, 1]
After
[1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 8, 9, 9, 10, 12
/* Write a function dispTen() with 2D array A and size
as a parameter and display only those numbers which are
divisible by 10 */
func dispTen(n: [[Int]], size:Int)
{
var x = 0 , y = 0
while x < size
{
y = 0
while y < size
{
if n[x][y] % 10 == 0
{
print("(n[x][y]), ",terminator:"")
}
y = y + 1
}
x = x + 1
}
print("")
}
var no:[[Int]] = [[12,20,13],[2,10,30],[40,5,16]]
dispTen(n:no,size:no.count)
----------------------------output-----------------------------------
20, 10, 30, 40,
// Write a function rowsum() with 2D array A and size as
a parameter and display the sum of each row
func rowsum(n: [[Int]], size:Int)
{
var x = 0 , y = 0
var sum = 0
while x < size
{
y = 0
sum = 0
while y < size
{
sum = sum + n[x][y]
print("(n[x][y]) +",terminator:"")
y = y + 1
}
x = x + 1
print("=(sum)n",terminator:"")
}
print("")
}
var no:[[Int]] = [[12,20,13],[2,10,30],[40,5,16]]
rowsum(n:no,size:no.count)
----------------------output------------------------------
12 +20 +13 =45
2 +10 +30 =42
40 +5 +16 =61
// Write a function diagonalsum() with 2D array A and
size as a parameter and display the sum
func diagonalsum(A1:inout [[Int]] , k:Int) -> Int
{
var x:Int = 0 , y:Int = 0
var lsum = 0 , rsum = 0
while x < k
{
y = 0
while y < k
{
if x == y
{
lsum = lsum + A1[x][y]
}else if x + y == k - 1
{
rsum = rsum + A1[x][y]
}
y = y + 1
}
x = x + 1
}
return lsum + rsum
}
var A:[[Int]] = [[4,5,6],[7,8,9],[1,2,3]]
print("before array A")
print(A)
var rs = diagonalsum (A1:&A, k:A.count)
print(“sum of diagonal =(rs)”)
-----------------------------output--------------------------------
before array A
[[4, 5, 6], [7, 8, 9], [1, 2, 3]]
Sum of diagonal = 22
//program to find the largest no from a 1D array of 10 numbers
func largest(no:[Int], k:Int) -> Int
{
var large = no[0]
var x = 0
while x < k
{
if no[x] > large
{
large = no[x]
}
x = x + 1
}
return large
}
var n:[Int] = [51, 21, 30, 45, 75, 15, 2, 56, 120, 11 ]
var rs = largest(no:n, k:10)
print("largest no = (rs)")
------------------------output-------------------------------
Largest no = 120
/*Write a function swaparr() with 2D array A and size as
a parameter and swap or interchange the first row with
the last row elements.*/
func display(tmp:[[Int]], size:Int)
{
var x = 0 , y = 0
while x < size
{
y = 0
while y < size
{
print("(tmp[x][y]) , ",terminator:"")
y = y + 1
}
x = x + 1
print("n")
}
print("")
}
func swaparr (A1:inout [[Int]] , k:Int)
{
var a = 0 , last = k - 1
var t = 0
while a < k
{
t = A1[0][a]
A1[0][a] = A1[last][a]
A1[last][a] = t
a = a + 1
}
}
var A:[[Int]] = [[5,6,2,3],[1,2,4,9],[2,5,8,1],[9,7,5,8]]
print("before")
display(tmp:A,size:A.count)
swaparr(A1:&A,k:A.count)
print("after")
display(tmp:A,size:A.count)
-----------------------output--------------------------------------
before
5 , 6 , 2 , 3
1 , 2 , 4 , 9
2 , 5 , 8 , 1
9 , 7 , 5 , 8
after
9 , 7 , 5 , 8
1 , 2 , 4 , 9
2 , 5 , 8 , 1
5 , 6 , 2 , 3
/* Write a function lefttrg() with 2D array A and size as a parameter and
display lower triangle as the output given
Example: output
1 2 3 4
6 4 5 6 6
2 1 5 4 2 1
1 2 3 5 1 2 3 */
func display(tmp:[[Int]], size:Int)
{
var x = 0 , y = 0
while x < size
{
y = 0
while y < size
{
print("(tmp[x][y]) , ",terminator:"")
y = y + 1
}
x = x + 1
print("n")
}
print("")
}
func lfttrg(z:[[Int]] , k:Int)
{
var a = 0 , b = 0
var t = 0
while a < k
{
b = 0
while b < k
{
if a > b
{
print("(z[a][b]) , ",terminator:"")
}
b = b + 1
}
print("n",terminator:"")
a = a + 1
}
print("")
}
var A:[[Int]] = [[1,2,3,4],[6,4,5,6],[2,1,5,4],[1,2,3,5]]
print("before")
display(tmp:A,size:A.count)
print("lower triangle")
lfttrg(z:A,k:A.count)
-----------------------------output---------------------------
before
1 , 2 , 3 , 4
6 , 4 , 5 , 6
2 , 1 , 5 , 4
1 , 2 , 3 , 5
lower triangle
6
2 , 1
1 , 2 , 3
/* Write a function summatrix() with 2D array A , B , C and size as a parameter,
and display the sum of two matrix in C */
func display(tmp:[[Int]], size:Int)
{
var x = 0 , y = 0
while x < size
{
y = 0
while y < size
{
print("(tmp[x][y]) , ",terminator:"")
y = y + 1
}
x = x + 1
print("n")
}
print("")
}
func summat(A1:[[Int]], B1:[[Int]], C1:inout [[Int]], size:Int)
{
var x = 0 , y = 0
var sum = 0
while x < size
{
y = 0
while y < size
{
sum = A1[x][y] + B1[x][y]
C1[x][y] = sum
y = y + 1
}
x = x + 1
}
}
var A:[[Int]] = [[1,2,3],[1,2,3],[1,2,3]]
var B:[[Int]] = [[4,5,6],[4,5,6],[4,5,6]]
var C:[[Int]] = [[Int]](repeating:[Int] (repeating: 0, count: 3), count: 3)
print("Array A")
display(tmp:A,size:A.count)
print("Array B")
display(tmp:B,size:B.count)
summat(A1:A,B1:B,C1:&C,size:3)
print("Array C")
display(tmp:C,size:C.count)
--------------------output-----------------------
Array C
5 , 7 , 9 ,
5 , 7 , 9 ,
5 , 7 , 9 ,
---------------------output--------------------------
Array A
1 , 2 , 3 ,
1 , 2 , 3 ,
1 , 2 , 3 ,
Array B
4 , 5 , 6 ,
4 , 5 , 6 ,
4 , 5 , 6 ,
/* Write a function copyarr() with 2D array A and B as 1D array and size as a
parameter,display the output as given below:
Example: if array B values are 1,2,3,4,5
Array A store values as:
1,2,3,4,5
1,2,3,4,0
1,2,3,0,0
1,2,0,0,0
1,0,0,0,0
*/
func display(tmp:[[Int]], size:Int)
{
var x = 0 , y = 0
while x < size
{
y = 0
while y < size
{
print("(tmp[x][y]) , ",terminator:"")
y = y + 1
}
x = x + 1
print("n")
}
print("")
}
func copyarr(A1:inout [[Int]] , B1:[Int] , k:Int)
{
var x = 0 , y = 0
while x < k
{
y = 0
while y < k
{
if x + y < k
{
A1[x][y] = B1[y]
}else
{
A1[x][y] = 0
}
y = y + 1
}
x = x + 1
}
}
var B:[Int] = [10,20,30,40,50]
var A:[[Int]] = [[Int]](repeating:[Int] (repeating: 0, count: B.count), count: B.count)
print("before array B")
print(B)
copyarr(A1:&A, B1:B, k:B.count)
print("after array A")
display(tmp:A,size:A.count)
--------------------output--------------------------
before array B
[10, 20, 30, 40, 50]
After array A
10 20 30 40 50
10 20 30 40 0
10 20 30 0 0
10 20 0 0 0
10 0 0 0 0

More Related Content

PDF
Pandas pythonfordatascience
PPTX
Introduction to Monads in Scala (2)
PDF
Python matplotlib cheat_sheet
PDF
Pandas,scipy,numpy cheatsheet
DOCX
Introduction to r
PDF
Matlab plotting
PPT
Chapter2
PDF
The Essence of the Iterator Pattern (pdf)
Pandas pythonfordatascience
Introduction to Monads in Scala (2)
Python matplotlib cheat_sheet
Pandas,scipy,numpy cheatsheet
Introduction to r
Matlab plotting
Chapter2
The Essence of the Iterator Pattern (pdf)

What's hot (19)

PDF
Numpy tutorial(final) 20160303
PPTX
Pandas Series
PPTX
The Essence of the Iterator Pattern
PPTX
Python object oriented programming (lab2) (2)
PDF
Python seaborn cheat_sheet
PDF
Multi dimensional array
PPT
PPTX
DataFrame in Python Pandas
PDF
Python bokeh cheat_sheet
PPTX
Seminar PSU 10.10.2014 mme
PDF
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...
PPTX
Matlab plotting
PPTX
Tupple ware in scala
PPTX
Matlab ploting
PDF
Python3 cheatsheet
PDF
Oh, All the things you'll traverse
PDF
The Ring programming language version 1.6 book - Part 183 of 189
PPTX
A quick introduction to R
PPTX
List and Dictionary in python
Numpy tutorial(final) 20160303
Pandas Series
The Essence of the Iterator Pattern
Python object oriented programming (lab2) (2)
Python seaborn cheat_sheet
Multi dimensional array
DataFrame in Python Pandas
Python bokeh cheat_sheet
Seminar PSU 10.10.2014 mme
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...
Matlab plotting
Tupple ware in scala
Matlab ploting
Python3 cheatsheet
Oh, All the things you'll traverse
The Ring programming language version 1.6 book - Part 183 of 189
A quick introduction to R
List and Dictionary in python
Ad

Similar to Programs in array using SWIFT (20)

PPT
Chapter 10.ppt
PPT
358 33 powerpoint-slides_5-arrays_chapter-5
PPTX
Program Practical to operations on Array
PPT
array.ppt
PPTX
Csci101 lect07 algorithms_ii
PDF
Arrays and its properties IN SWIFT
DOC
PDF
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 5.pdf
PPT
Array ajjh jhjhj jhjhj jhjhjh jhj jhjhjh
PPT
PPT
PPTX
Ch-11-Arrays.ppt-1.pptx eurhrbdhdbdhrhdhdh
PPTX
Module_3_Arrays - Updated.pptx............
PDF
1D Array
PDF
Arrays in python
PDF
SPL 10 | One Dimensional Array in C
PPTX
Arrays matrix 2020 ab
PDF
programs on arrays.pdf
PPTX
Array programs
Chapter 10.ppt
358 33 powerpoint-slides_5-arrays_chapter-5
Program Practical to operations on Array
array.ppt
Csci101 lect07 algorithms_ii
Arrays and its properties IN SWIFT
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 5.pdf
Array ajjh jhjhj jhjhj jhjhjh jhj jhjhjh
Ch-11-Arrays.ppt-1.pptx eurhrbdhdbdhrhdhdh
Module_3_Arrays - Updated.pptx............
1D Array
Arrays in python
SPL 10 | One Dimensional Array in C
Arrays matrix 2020 ab
programs on arrays.pdf
Array programs
Ad

More from vikram mahendra (20)

PPTX
Communication skill
PDF
Python Project On Cosmetic Shop system
PDF
Python Project on Computer Shop
PDF
PYTHON PROJECT ON CARSHOP SYSTEM
PDF
BOOK SHOP SYSTEM Project in Python
PPTX
FLOW OF CONTROL-NESTED IFS IN PYTHON
PPTX
FLOWOFCONTROL-IF..ELSE PYTHON
PPTX
FLOW OF CONTROL-INTRO PYTHON
PPTX
OPERATOR IN PYTHON-PART1
PPTX
OPERATOR IN PYTHON-PART2
PPTX
USE OF PRINT IN PYTHON PART 2
PPTX
DATA TYPE IN PYTHON
PPTX
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
PPTX
USER DEFINE FUNCTIONS IN PYTHON
PPTX
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
PPTX
INTRODUCTION TO FUNCTIONS IN PYTHON
PPTX
Python Introduction
PPTX
GREEN SKILL[PART-2]
PPTX
GREEN SKILLS[PART-1]
PPTX
Dictionary in python
Communication skill
Python Project On Cosmetic Shop system
Python Project on Computer Shop
PYTHON PROJECT ON CARSHOP SYSTEM
BOOK SHOP SYSTEM Project in Python
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOWOFCONTROL-IF..ELSE PYTHON
FLOW OF CONTROL-INTRO PYTHON
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART2
USE OF PRINT IN PYTHON PART 2
DATA TYPE IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
INTRODUCTION TO FUNCTIONS IN PYTHON
Python Introduction
GREEN SKILL[PART-2]
GREEN SKILLS[PART-1]
Dictionary in python

Recently uploaded (20)

PPTX
history of c programming in notes for students .pptx
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
top salesforce developer skills in 2025.pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Essential Infomation Tech presentation.pptx
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
medical staffing services at VALiNTRY
PPTX
Transform Your Business with a Software ERP System
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PPTX
ai tools demonstartion for schools and inter college
PPTX
L1 - Introduction to python Backend.pptx
history of c programming in notes for students .pptx
Design an Analysis of Algorithms II-SECS-1021-03
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
How to Migrate SBCGlobal Email to Yahoo Easily
top salesforce developer skills in 2025.pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PTS Company Brochure 2025 (1).pdf.......
Upgrade and Innovation Strategies for SAP ERP Customers
Essential Infomation Tech presentation.pptx
2025 Textile ERP Trends: SAP, Odoo & Oracle
Design an Analysis of Algorithms I-SECS-1021-03
Which alternative to Crystal Reports is best for small or large businesses.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
medical staffing services at VALiNTRY
Transform Your Business with a Software ERP System
Understanding Forklifts - TECH EHS Solution
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Reimagine Home Health with the Power of Agentic AI​
ai tools demonstartion for schools and inter college
L1 - Introduction to python Backend.pptx

Programs in array using SWIFT

  • 2. INDEX S.NO. PROGRAMS SIGNATURE 1 Program to print the numbers in an array horizontally 2 Program to search the number from a 1 D array using linear search 3 Program to search the number from a 1 D array using binary search 4 Program to insert the number in a 1 D array using Insertion sorted array 5 Program to insert the number in a 1 D array using Deletion sorted array 6 Program to sort the number in an array using bubble sort 7 Program to sort the number in an array using selection sort 8 Write a function Alter(), with array and size as a parameter ,which should change all the multiples of 5 in the array to 5 and rest of the element as 1 9 Write a function modify() with array and size as a parameter, which should reposition the content after swapping each adjacent pair of number in it 10 Write a function Large() with array and size as a parameter, which should display the largest and second largest by returning both from an array 11 Write a function addup() with array and size as a parameter,in which all even position(0,2,4,…..) of the array should be added with the content of element in the next position and odd position(1,3,5,…..) elements should be increment by 10 12 Write a function twotoone() with array and size as an parameters,in which accept two array X[] and Y[] and transfer all X[] numbers in even places of Z[] and all Y[] numbers in odd places of Z[]. 13 Write a function convert() with array and size as a parameter,which reposition all the elements of the array by shifting each of them one to one position before and by shifting the first elements to the last position 14 Write a function SWAP() with array and size as a parameter,which swap the numbers on the basis of example given: If array A no : 10,20,30,40,50,60 the output after swapping 20,10,40,30,60,50 15 Write a function SWAP2() with array and size as a parameter,which swap the numbers on the basis of example given: If array A no : 10,20,30,40,50,60 the output after swapping 40,50,60,10,20,30 16 Write a function merge() with three array as a parameter array A,B, and C with their size M , N , and M+N , where A is ascending order , B is descending order and C accept numbers from A and B in ascending order. 17 Write a function dispTen() with 2D array no and size as a parameter and display only those numbers which are divisible by 10 18 Write a function rowsum() with 2D array A and size as a parameter and display the sum of each row
  • 3. 20 Write a function diagonalsum() with 2D array A and size as a parameter and display the sum 21 program to find the largest no from a 1D array of 10 numbers 22 Write a function swaparr() with 2D array A and size as a parameter and swap or interchange the first row with the last row elements. 23 Write a function lefttrg() with 2D array A and size as a parameter and display the output as given Example: output 1 2 3 4 6 4 5 6 6 2 1 5 4 2 1 1 2 3 5 1 2 3 24 Write a function summatrix() with 2D array A , B , C and size as a parameter, and display the sum of two matrix in C 25 Write a function copyarr() with 2D array A and B as 1D array and size as a parameter,display the output as given below: Example: if array B values are 1,2,3,4,5 Array A store values as: 1,2,3,4,5 1,2,3,4,0 1,2,3,0,0 1,2,0,0,0 1,0,0,0,0
  • 4. //Program to print the numbers in an array horizontally func display(no:[Int], k:Int) { var x = 0 while x < k { print("(no[x]), ",terminator:"") x = x + 1 } print("") } var n:[Int] = [10,20,30,40,50,60,70,80,90,100 ] display(no:n, k:10) ----------------------------output ---------------------------------- 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
  • 5. // Program to search the number from a 1 D array using linear search func lsearch(no:[Int], src:Int, k:Int) -> Int { var tmp = -1 var x = 0 while x < k { if no[x] == src { tmp = x break } x = x + 1 } return tmp } var n:[Int] = [40,10,51,86,19,87,70,3,14,100 ] var rs = lsearch(no:n, src:19, k:10) if rs < 0 { print("no not found") }else { rs = rs + 1 print("no found at pos =(rs)") } --------------------------output---------------------------- no found at pos =5
  • 6. // Program to search the number from a 1 D array using binary search func bsearch(no:[Int], src:Int, k:Int) -> Int { var tmp = -1 var x = 0 var beg = 0 , last = k - 1 , mid = 0 while beg < last { mid = (beg + last) / 2 if no[mid] == src { tmp = x break }else { if no[mid] > src { last = mid - 1 }else { beg = mid + 1 } } x = x + 1 } return tmp } var n:[Int] = [40,45,58,75,86,89,92,97,99,120] var rs = bsearch(no:n, src:58, k:10) if rs < 0 { print("no not found") }else { rs = rs + 1 print("no found at pos =(rs)") } ---------------output------------------ no found at pos =3
  • 7. // Program to insert the number in a 1 D array using Insertion sorted array func insertionUN(no:[Int],ser:Int,k:Int) -> Int { var pos:Int = -1 var flag = -1 var x = 0 let last = k - 1 while x < k { if no[0] >= ser { flag = 0 pos = 0 break }else if no[last] <= ser { pos = last flag = 1 break; } else if no[x] < ser && no[x + 1] > ser { flag = 2 pos = x + 1 break } x = x + 1 } if flag == 0 { pos = 0 }else if flag == 1 { pos = pos + 1 } return pos }
  • 8. var search = 67 print("before insertion") var n:[Int] = [10,20,30,40,50,60,70,80] var k1 = insertionUN(no:n, ser:search, k:n.count) print("after insertion") n.insert(search,at:k1) print(n) ----------------------------output------------------------------ before insertion [10, 20, 30, 40, 50, 60, 70, 80] after insertion [10, 20, 30, 40, 50, 60, 67, 70, 80]
  • 9. // Program to insert the number in a 1 D array using Deletion sorted array func deletionUN(no:[Int],ser:Int,k:Int) -> Int { var pos:Int = -1 var x = 0 while x < k { if no[x] == ser { pos = x break } x = x + 1 } return pos } var search = 40 var y = 0 var n:[Int] = [10,20,30,40,50,60,70,80] print("before deletion array size = (n.count)") print(n) var k1 = deletionUN(no:n, ser:search, k:n.count) if k1 > 0 { y = k1 while y < n.count - 1 { n[y] = n[y + 1] y = y + 1 } } n.remove(at:n.count - 1) print("after deletion array size =(n.count)") print(n) -------output---------------- before deletion array size = 8 [10, 20, 30, 40, 50, 60, 70, 80] after deletion array size =7 [10, 20, 30, 50, 60, 70, 80]
  • 10. // Program to sort the number in an array using bubble sort func sortbubble(no:inout [Int],k:Int) { var x = 0 , y = 0 var tmp = 0 while x < k { y = 0 while y < k - 1 { if no[y] > no[y + 1] { tmp = no[y] no[y] = no[y + 1] no[y + 1] = tmp } y = y + 1 } x = x + 1 } } var n:[Int] = [60,2,33,55,5,22,1,8] print("before sorting array ") print(n) sortbubble(no: &n, k:n.count) print("after sorting array") print(n) ----------------------------------output------------------------------------ before sorting array [60, 2, 33, 55, 5, 22, 1, 8] after sorting array [1, 2, 5, 8, 22, 33, 55, 60]
  • 11. // Program to sort the number in an array using selection sort func sortselection(no:inout [Int],k:Int) { var x = 0 , y = 0 var tmp = 0 while x < k { y = x + 1 while y < k { if no[x] > no[y] { tmp = no[x] no[x] = no[y] no[y] = tmp } y = y + 1 } x = x + 1 } } var n:[Int] = [60,2,33,55,5,22,1,8] print("before sorting array ") print(n) sortselection(no: &n, k:n.count) print("after sorting array") print(n) -----------------------------output------------------------------ before sorting array [60, 2, 33, 55, 5, 22, 1, 8] after sorting array [1, 2, 5, 8, 22, 33, 55, 60]
  • 12. /* Write a function Alter(), with array and size as a parameter ,which should change all the multiples of 5 in the array to 5 and rest of the element as 1 */ func alter(no:inout [Int],k:Int) { var x = 0 while x < k { if no[x] % 5 == 0 { no [x] = 5 }else { no[x] = 1 } x = x + 1 } } var n:[Int] = [55,43,20,16,39,90,83,40,48,25] print("before ") print(n) alter(no: &n, k:n.count) print("after") print(n) ------------------------output----------------------------- before [55, 43, 20, 16, 39, 90, 83, 40, 48, 25] after [5, 1, 5, 1, 1, 5, 1, 5, 1, 5]
  • 13. /* Write a function modify() with array and size as a parameter, which should reposition the content after swapping each adjacent pair of number in it */ func modify(no:inout [Int],k:Int) { var x = 0 , tmp = 0 while x < k { tmp = no[x] no[x] = no[x + 2] no[x + 2] = tmp if x % 4 >= 1 { x = x + 2 } x = x + 1 } } var n:[Int] = [86,93,40,36,52,21,70,10] print("before ") print(n) modify(no: &n, k:n.count) print("after") print(n) ----------------------------output------------------------------------------------- before [86, 93, 40, 36, 52, 21, 70, 10] after [40, 36, 86, 93, 70, 10, 52
  • 14. /*Write a function Large() with array and size as a parameter, which should display the largest and second largest by returning both from an array.*/ func large(no:inout [Int],k:Int) -> ( Int , Int ) { var x = 0 , l = no[0] , sec1 = no[0] while x < k { if no[x] > l { sec1 = l l = no[x] }else if no[x] > sec1 { sec1 = no[x] } x = x + 1 } return (l , sec1) } var n:[Int] = [86,93,40,136,52,21,70,10] var (k , s ) = large(no: &n, k:n.count) print("Largest no = (k) and second large = (s)") --------------------------------output------------------------------------ Largest no = 136 and second large = 93
  • 15. /* Write a function addup() with array and size as a parameter,in which all even position(0,2,4,…..) of the array should be added with the content of element in the next position and odd position(1,3,5,…..) elements should be increment by 10 */ func addup(no:inout [Int] , k:Int) { var x:Int = 0 while x < k { if no[x] % 2 == 0 { no[x] = no[x] + no[x + 1] }else { no[x] = no[x] + 10 } x = x + 1 } } var n:[Int] = [23,30,45,10,15,25,45,10,56,78,33] print("before ") print(n) addup(no: &n, k:n.count) print("after") print(n) ----------------------------output-------------------------------- before [23, 30, 45, 10, 15, 25, 45, 10, 56, 78, 33] after [33, 75, 55, 25, 25, 35, 55, 66, 134, 111, 43]
  • 16. /* Write a function twotoone() with array and size as an parameters,in which accept two array X[] and Y[] and transfer all X[] numbers in even places of Z[] and all Y[] numbers in odd places of Z[]. */ func twotoone(z:inout [Int] ,x:[Int] ,y:[Int] , k:Int) { var a:Int = 0 , b:Int = 0 var c:Int = 0 while c < k { if c % 2 == 0 { z.insert(x[a],at:c) a = a + 1 }else { z.insert(y[b],at:c) b = b + 1 } c = c + 1 }} var Z:[Int] = [] var X:[Int] = [10,20,30,40,50] var Y:[Int] = [11,12,13,14,15] print("before array Z") print(Z) print("before array X") print(X) print("before array Y") print(Y) twotoone(z:&Z, x:X, y:Y, k:10) print("after") print(Z) ---------------output--------------------- before array Z [] before array X [10, 20, 30, 40, 50] before array Y [11, 12, 13, 14, 15] After array Z [10, 11, 20, 12, 30, 13, 40, 14, 50, 15]
  • 17. /* Write a function convert() with array and size as a parameter,which reposition all the elements of the array by shifting each of them one to one position before and by shifting the first elements to the last position*/ func convert(no:inout [Int] ,k:Int ,size:Int) { var tmp:Int = 0 var last:Int = size - 1 var x:Int = 1 var y:Int = 0 while x <= k { tmp = no[0] y = 0 while y < size - 1 { no[y] = no[y + 1] y = y + 1 } no[last] = tmp x = x + 1 } } var X:[Int] = [10,20,30,40,50,60,70,80] print("before") print(X) convert(no: &X, k:2, size:X.count) print("After") print(X) --------------------------------------output---------------------------------------- before [10, 20, 30, 40, 50, 60, 70, 80] After [30, 40, 50, 60, 70, 80, 10, 20]
  • 18. /*Write a function SWAP() with array and size as a parameter,which swap the numbers on the basis of example given: If array A no : 10,20,30,40,50,60 the output after swapping 20,10,40,30,60,50 */ func swap(no:inout [Int] ,size:Int) { var tmp:Int = 0 var y:Int = 0 while y < size - 1 { tmp = no[y] no[y] = no[y + 1] no[y + 1] = tmp y = y + 2 } } var X:[Int] = [10,20,30,40,50,60] print("before") print(X) swap(no: &X, size:X.count) print("After") print(X) ------------------------------output--------------------------- before [10, 20, 30, 40, 50, 60] After [20, 10, 40, 30, 60, 50]
  • 19. /*Write a function SWAP2() with array and size as a parameter,which swap the numbers on the basis of example given: If array A no : 10,20,30,40,50,60 the output after swapping 40,50,60,10,20,30 */ func swap2(no:inout [Int] ,size:Int) { var tmp:Int = 0 var y:Int = 0 var x:Int = size / 2 while y < size / 2 { tmp = no[y] no[y] = no[x] no[x] = tmp y = y + 1 x = x + 1 } } var X:[Int] = [10,20,30,40,50,60] print("before") print(X) swap(no: &X, size:X.count) print("After") print(X) ---------------------------output--------------------------- before [10, 20, 30, 40, 50, 60] After [40, 50, 60, 10, 20, 30]
  • 20. /* Write a function merge() with three array as a parameter array A,B, and C with their size M , N , and M+N , where A is ascending order , B is descending order and C accept numbers from A and B in ascending order. */ func merge(A1:[Int],B1:[Int],C1:inout [Int], M:Int, N:Int) { var x = 0 var a = 0 var k = 0 // var y = ( M + N ) - 1 var y = N - 1 while x < M && y >= 0 { if A1[x] <= B1[y] { C1.append(A1[x]) k = k + 1 x = x + 1 }else { C1.append(B1[y]) k = k + 1 y = y - 1 } } if x < M { a = x while a < M { C1.append(A1[a]) a = a + 1 } } if y >= 0 {
  • 21. a = y while a >= 0 { C1.append(B1[a]) a = a - 1 } } } var A:[Int] = [1,2,3,4,5,6,9,10,12] var B:[Int] = [9,8,7,5,3,2,1] var C:[Int] = [] print("before A") print(A) print("b numbers") print(B) merge(A1: A,B1: B,C1: &C,M:A.count , N:B.count) print("After") print(C) ----------------------------------output-------------------------------------- before A [1, 2, 3, 4, 5, 6, 9, 10, 12] b numbers [9, 8, 7, 5, 3, 2, 1] After [1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 7, 8, 9, 9, 10, 12
  • 22. /* Write a function dispTen() with 2D array A and size as a parameter and display only those numbers which are divisible by 10 */ func dispTen(n: [[Int]], size:Int) { var x = 0 , y = 0 while x < size { y = 0 while y < size { if n[x][y] % 10 == 0 { print("(n[x][y]), ",terminator:"") } y = y + 1 } x = x + 1 } print("") } var no:[[Int]] = [[12,20,13],[2,10,30],[40,5,16]] dispTen(n:no,size:no.count) ----------------------------output----------------------------------- 20, 10, 30, 40,
  • 23. // Write a function rowsum() with 2D array A and size as a parameter and display the sum of each row func rowsum(n: [[Int]], size:Int) { var x = 0 , y = 0 var sum = 0 while x < size { y = 0 sum = 0 while y < size { sum = sum + n[x][y] print("(n[x][y]) +",terminator:"") y = y + 1 } x = x + 1 print("=(sum)n",terminator:"") } print("") } var no:[[Int]] = [[12,20,13],[2,10,30],[40,5,16]] rowsum(n:no,size:no.count) ----------------------output------------------------------ 12 +20 +13 =45 2 +10 +30 =42 40 +5 +16 =61
  • 24. // Write a function diagonalsum() with 2D array A and size as a parameter and display the sum func diagonalsum(A1:inout [[Int]] , k:Int) -> Int { var x:Int = 0 , y:Int = 0 var lsum = 0 , rsum = 0 while x < k { y = 0 while y < k { if x == y { lsum = lsum + A1[x][y] }else if x + y == k - 1 { rsum = rsum + A1[x][y] } y = y + 1 } x = x + 1 } return lsum + rsum } var A:[[Int]] = [[4,5,6],[7,8,9],[1,2,3]] print("before array A") print(A) var rs = diagonalsum (A1:&A, k:A.count) print(“sum of diagonal =(rs)”) -----------------------------output-------------------------------- before array A [[4, 5, 6], [7, 8, 9], [1, 2, 3]] Sum of diagonal = 22
  • 25. //program to find the largest no from a 1D array of 10 numbers func largest(no:[Int], k:Int) -> Int { var large = no[0] var x = 0 while x < k { if no[x] > large { large = no[x] } x = x + 1 } return large } var n:[Int] = [51, 21, 30, 45, 75, 15, 2, 56, 120, 11 ] var rs = largest(no:n, k:10) print("largest no = (rs)") ------------------------output------------------------------- Largest no = 120
  • 26. /*Write a function swaparr() with 2D array A and size as a parameter and swap or interchange the first row with the last row elements.*/ func display(tmp:[[Int]], size:Int) { var x = 0 , y = 0 while x < size { y = 0 while y < size { print("(tmp[x][y]) , ",terminator:"") y = y + 1 } x = x + 1 print("n") } print("") } func swaparr (A1:inout [[Int]] , k:Int) { var a = 0 , last = k - 1 var t = 0 while a < k { t = A1[0][a] A1[0][a] = A1[last][a] A1[last][a] = t a = a + 1 } } var A:[[Int]] = [[5,6,2,3],[1,2,4,9],[2,5,8,1],[9,7,5,8]] print("before") display(tmp:A,size:A.count) swaparr(A1:&A,k:A.count) print("after") display(tmp:A,size:A.count) -----------------------output-------------------------------------- before 5 , 6 , 2 , 3 1 , 2 , 4 , 9 2 , 5 , 8 , 1 9 , 7 , 5 , 8 after 9 , 7 , 5 , 8 1 , 2 , 4 , 9 2 , 5 , 8 , 1 5 , 6 , 2 , 3
  • 27. /* Write a function lefttrg() with 2D array A and size as a parameter and display lower triangle as the output given Example: output 1 2 3 4 6 4 5 6 6 2 1 5 4 2 1 1 2 3 5 1 2 3 */ func display(tmp:[[Int]], size:Int) { var x = 0 , y = 0 while x < size { y = 0 while y < size { print("(tmp[x][y]) , ",terminator:"") y = y + 1 } x = x + 1 print("n") } print("") } func lfttrg(z:[[Int]] , k:Int) { var a = 0 , b = 0 var t = 0 while a < k { b = 0 while b < k { if a > b { print("(z[a][b]) , ",terminator:"") } b = b + 1 }
  • 28. print("n",terminator:"") a = a + 1 } print("") } var A:[[Int]] = [[1,2,3,4],[6,4,5,6],[2,1,5,4],[1,2,3,5]] print("before") display(tmp:A,size:A.count) print("lower triangle") lfttrg(z:A,k:A.count) -----------------------------output--------------------------- before 1 , 2 , 3 , 4 6 , 4 , 5 , 6 2 , 1 , 5 , 4 1 , 2 , 3 , 5 lower triangle 6 2 , 1 1 , 2 , 3
  • 29. /* Write a function summatrix() with 2D array A , B , C and size as a parameter, and display the sum of two matrix in C */ func display(tmp:[[Int]], size:Int) { var x = 0 , y = 0 while x < size { y = 0 while y < size { print("(tmp[x][y]) , ",terminator:"") y = y + 1 } x = x + 1 print("n") } print("") } func summat(A1:[[Int]], B1:[[Int]], C1:inout [[Int]], size:Int) { var x = 0 , y = 0 var sum = 0 while x < size { y = 0 while y < size { sum = A1[x][y] + B1[x][y] C1[x][y] = sum y = y + 1 } x = x + 1 } } var A:[[Int]] = [[1,2,3],[1,2,3],[1,2,3]] var B:[[Int]] = [[4,5,6],[4,5,6],[4,5,6]] var C:[[Int]] = [[Int]](repeating:[Int] (repeating: 0, count: 3), count: 3) print("Array A") display(tmp:A,size:A.count) print("Array B") display(tmp:B,size:B.count) summat(A1:A,B1:B,C1:&C,size:3) print("Array C") display(tmp:C,size:C.count) --------------------output----------------------- Array C 5 , 7 , 9 , 5 , 7 , 9 , 5 , 7 , 9 , ---------------------output-------------------------- Array A 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , Array B 4 , 5 , 6 , 4 , 5 , 6 , 4 , 5 , 6 ,
  • 30. /* Write a function copyarr() with 2D array A and B as 1D array and size as a parameter,display the output as given below: Example: if array B values are 1,2,3,4,5 Array A store values as: 1,2,3,4,5 1,2,3,4,0 1,2,3,0,0 1,2,0,0,0 1,0,0,0,0 */ func display(tmp:[[Int]], size:Int) { var x = 0 , y = 0 while x < size { y = 0 while y < size { print("(tmp[x][y]) , ",terminator:"") y = y + 1 } x = x + 1 print("n") } print("") } func copyarr(A1:inout [[Int]] , B1:[Int] , k:Int) { var x = 0 , y = 0 while x < k { y = 0 while y < k { if x + y < k { A1[x][y] = B1[y]
  • 31. }else { A1[x][y] = 0 } y = y + 1 } x = x + 1 } } var B:[Int] = [10,20,30,40,50] var A:[[Int]] = [[Int]](repeating:[Int] (repeating: 0, count: B.count), count: B.count) print("before array B") print(B) copyarr(A1:&A, B1:B, k:B.count) print("after array A") display(tmp:A,size:A.count) --------------------output-------------------------- before array B [10, 20, 30, 40, 50] After array A 10 20 30 40 50 10 20 30 40 0 10 20 30 0 0 10 20 0 0 0 10 0 0 0 0