Академический Документы
Профессиональный Документы
Культура Документы
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("")
}
----------------------------output ----------------------------------
10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
// Program to search the number from a 1 D array using linear search
--------------------------output----------------------------
no found at pos =5
// Program to search the number from a 1 D array using binary search
----------------------------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
}
}
----------------------------------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
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 x = 0
while x < k
{
if no[x] % 5 == 0
{
no [x] = 5
}else
{
no[x] = 1
}
x=x+1
}
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
}
}
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.*/
--------------------------------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
}
}
----------------------------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
}
}
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
}
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 */
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. */
}
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
}
}
}
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
}
print("")
}
----------------------------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
----------------------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
{
y=y+1
}
x=x+1
}
return lsum + rsum
}
-----------------------------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 */
-----------------------------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("")
}
--------------------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