Вы находитесь на странице: 1из 31

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
1234
6456 6
2154 21
1235 123
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]


---------------output------------------
var rs = bsearch(no:n, src:58, k:10)
if rs < 0 no found at pos =3
{
print("no not found")
}else
{
rs = rs + 1
print("no found at pos =\(rs)")
}
// 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
{
-------output----------------
n[y] = n[y + 1] before deletion array size = 8
y=y+1 [10, 20, 30, 40, 50, 60, 70, 80]
after deletion array size =7
} [10, 20, 30, 50, 60, 70, 80]
}
n.remove(at:n.count - 1)
print("after deletion array size =\(n.count)")
print(n)
// 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) ---------------output---------------------
before array Z
print("before array X") []
print(X) before array X
[10, 20, 30, 40, 50]
print("before array Y") before array Y
print(Y) [11, 12, 13, 14, 15]
After array Z
twotoone(z:&Z, x:X, y:Y, k:10) [10, 11, 20, 12, 30, 13, 40, 14, 50,
print("after") 15]
print(Z)
/* 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")
} -----------------------output--------------------------------------
print("")
before
} 5 , 6 , 2 , 3
func swaparr (A1:inout [[Int]] , k:Int)
1 , 2 , 4 , 9
{
var a = 0 , last = k - 1 2 , 5 , 8 , 1
var t = 0 9 , 7 , 5 , 8
while a < k
{ after
t = A1[0][a] 9 , 7 , 5 , 8
A1[0][a] = A1[last][a] 1 , 2 , 4 , 9
A1[last][a] = t
2 , 5 , 8 , 1
a=a+1
} 5 , 6 , 2 , 3
}
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)
/* Write a function lefttrg() with 2D array A and size as a parameter and
display lower triangle as the output given
Example: output
1234
6456 6
2154 21
1235 123 */

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)
---------------------output--------------------------
{
var x = 0 , y = 0 Array A
while x < size 1 , 2 , 3 ,
{ 1 , 2 , 3 ,
y=0
while y < size 1 , 2 , 3 ,
{
print("\(tmp[x][y]) , ",terminator:"") Array B
y=y+1 4 , 5 , 6 ,
}
4 , 5 , 6 ,
x=x+1
print("\n") 4 , 5 , 6 ,
}
print("")
}

func summat(A1:[[Int]], B1:[[Int]], C1:inout [[Int]], size:Int)


{
var x = 0 , y = 0 --------------------output-----------------------
var sum = 0 Array C
while x < size 5 , 7 , 9 ,
{
y=0 5 , 7 , 9 ,
while y < size 5 , 7 , 9 ,
{
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)
/* 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

Вам также может понравиться