SlideShare une entreprise Scribd logo
1  sur  31
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Python object oriented programming (lab2) (2)
Python object oriented programming (lab2) (2)Python object oriented programming (lab2) (2)
Python object oriented programming (lab2) (2)
iloveallahsomuch
 

Tendances (19)

Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303Numpy tutorial(final) 20160303
Numpy tutorial(final) 20160303
 
Pandas Series
Pandas SeriesPandas Series
Pandas Series
 
The Essence of the Iterator Pattern
The Essence of the Iterator PatternThe Essence of the Iterator Pattern
The Essence of the Iterator Pattern
 
Python object oriented programming (lab2) (2)
Python object oriented programming (lab2) (2)Python object oriented programming (lab2) (2)
Python object oriented programming (lab2) (2)
 
Python seaborn cheat_sheet
Python seaborn cheat_sheetPython seaborn cheat_sheet
Python seaborn cheat_sheet
 
Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional array
 
Qno 3 (a)
Qno 3 (a)Qno 3 (a)
Qno 3 (a)
 
DataFrame in Python Pandas
DataFrame in Python PandasDataFrame in Python Pandas
DataFrame in Python Pandas
 
Python bokeh cheat_sheet
Python bokeh cheat_sheet Python bokeh cheat_sheet
Python bokeh cheat_sheet
 
Seminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mmeSeminar PSU 10.10.2014 mme
Seminar PSU 10.10.2014 mme
 
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...
N-Queens Combinatorial Problem - Polyglot FP for fun and profit - Haskell and...
 
Matlab plotting
Matlab plottingMatlab plotting
Matlab plotting
 
Tupple ware in scala
Tupple ware in scalaTupple ware in scala
Tupple ware in scala
 
Matlab ploting
Matlab plotingMatlab ploting
Matlab ploting
 
Python3 cheatsheet
Python3 cheatsheetPython3 cheatsheet
Python3 cheatsheet
 
Oh, All the things you'll traverse
Oh, All the things you'll traverseOh, All the things you'll traverse
Oh, All the things you'll traverse
 
The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189The Ring programming language version 1.6 book - Part 183 of 189
The Ring programming language version 1.6 book - Part 183 of 189
 
A quick introduction to R
A quick introduction to RA quick introduction to R
A quick introduction to R
 
List and Dictionary in python
List and Dictionary in pythonList and Dictionary in python
List and Dictionary in python
 

Similaire à Programs in array using SWIFT

1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
rushabhshah600
 

Similaire à Programs in array using SWIFT (20)

Programming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAYProgramming in lua STRING AND ARRAY
Programming in lua STRING AND ARRAY
 
BUilt in Functions and Simple programs in R.pdf
BUilt in Functions and Simple programs in R.pdfBUilt in Functions and Simple programs in R.pdf
BUilt in Functions and Simple programs in R.pdf
 
arrays.pptx
arrays.pptxarrays.pptx
arrays.pptx
 
A brief introduction to apply functions
A brief introduction to apply functionsA brief introduction to apply functions
A brief introduction to apply functions
 
2D array
2D array2D array
2D array
 
CLUSTERGRAM
CLUSTERGRAMCLUSTERGRAM
CLUSTERGRAM
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
 
chapter1.ppt
chapter1.pptchapter1.ppt
chapter1.ppt
 
Introduction to matlab
Introduction to matlabIntroduction to matlab
Introduction to matlab
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
 
Basic_analysis.ppt
Basic_analysis.pptBasic_analysis.ppt
Basic_analysis.ppt
 
Python High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.pptPython High Level Functions_Ch 11.ppt
Python High Level Functions_Ch 11.ppt
 
Basic Analysis using Python
Basic Analysis using PythonBasic Analysis using Python
Basic Analysis using Python
 
lect.no.3.pptx
lect.no.3.pptxlect.no.3.pptx
lect.no.3.pptx
 
Los dskn
Los dsknLos dskn
Los dskn
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
 
Data structures KTU chapter2.PPT
Data structures KTU chapter2.PPTData structures KTU chapter2.PPT
Data structures KTU chapter2.PPT
 
Chapter2
Chapter2Chapter2
Chapter2
 
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm ProblemsLeet Code May Coding Challenge - DataStructure and Algorithm Problems
Leet Code May Coding Challenge - DataStructure and Algorithm Problems
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 

Plus de vikram mahendra

Plus de vikram mahendra (20)

Communication skill
Communication skillCommunication skill
Communication skill
 
Python Project On Cosmetic Shop system
Python Project On Cosmetic Shop systemPython Project On Cosmetic Shop system
Python Project On Cosmetic Shop system
 
Python Project on Computer Shop
Python Project on Computer ShopPython Project on Computer Shop
Python Project on Computer Shop
 
PYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEMPYTHON PROJECT ON CARSHOP SYSTEM
PYTHON PROJECT ON CARSHOP SYSTEM
 
BOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in PythonBOOK SHOP SYSTEM Project in Python
BOOK SHOP SYSTEM Project in Python
 
FLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHONFLOW OF CONTROL-NESTED IFS IN PYTHON
FLOW OF CONTROL-NESTED IFS IN PYTHON
 
FLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHONFLOWOFCONTROL-IF..ELSE PYTHON
FLOWOFCONTROL-IF..ELSE PYTHON
 
FLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHONFLOW OF CONTROL-INTRO PYTHON
FLOW OF CONTROL-INTRO PYTHON
 
OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1OPERATOR IN PYTHON-PART1
OPERATOR IN PYTHON-PART1
 
OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2OPERATOR IN PYTHON-PART2
OPERATOR IN PYTHON-PART2
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2
 
DATA TYPE IN PYTHON
DATA TYPE IN PYTHONDATA TYPE IN PYTHON
DATA TYPE IN PYTHON
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
 
USER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHONUSER DEFINE FUNCTIONS IN PYTHON
USER DEFINE FUNCTIONS IN PYTHON
 
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]FUNCTIONS IN PYTHON[RANDOM FUNCTION]
FUNCTIONS IN PYTHON[RANDOM FUNCTION]
 
INTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHONINTRODUCTION TO FUNCTIONS IN PYTHON
INTRODUCTION TO FUNCTIONS IN PYTHON
 
Python Introduction
Python IntroductionPython Introduction
Python Introduction
 
GREEN SKILL[PART-2]
GREEN SKILL[PART-2]GREEN SKILL[PART-2]
GREEN SKILL[PART-2]
 
GREEN SKILLS[PART-1]
GREEN SKILLS[PART-1]GREEN SKILLS[PART-1]
GREEN SKILLS[PART-1]
 
Dictionary in python
Dictionary in pythonDictionary in python
Dictionary in python
 

Dernier

Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 

Dernier (20)

tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare%in Harare+277-882-255-28 abortion pills for sale in Harare
%in Harare+277-882-255-28 abortion pills for sale in Harare
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 

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