You are on page 1of 19

Mobile Application Development

(CS1205)

Assignment - 2

Submitted by: -
Vikas Janu
2020btechCSE083
Section – B

Submitted to: -
Prof. Pranab Kumar Roy

Department of Computer Science Engineering


Institute of Engineering and Technology
JK Lakshmipat University, Jaipur

Nov, 2022

1|Page
Table Of Content

Sr. No. Content Page No.


1. Question 1 3
2. Question 2 5
3. Question 3 7
4. Question 4 12
5. Question 5 11

2|Page
1. WAP to take two different arrays as input.
a. Assign numbers to both the arrays
b. Sort each of the arrays in ascending order using insertion sort
c. Merge the two sorted arrays in a third one where the numbers are sorted in
descending order.

Answer: -

Code: -
package com.example.classactivity

3|Page
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
fun main(args: Array<String>) {
val arr1 = intArrayOf(11, 34, 66, 75)
val n1 = arr1.size
val arr2 = intArrayOf(1, 5, 19, 50, 89, 100)
val n2 = arr2.size
val merge = IntArray(n1 + n2)
var i = 0
var j = 0
var k = 0
var x: Int
println("Array 1: ")
x = 0
while (x < n1) {
println(arr1[x].toString() + " ")
x++
}
println("Array 2: ")
x = 0
while (x < n2) {
println(arr2[x].toString() + " ")
x++
}
while (i < n1 && j < n2) {
if (arr1[i] < arr2[j]) merge[k++] = arr1[i++] else merge[k++]
= arr2[j++]
}
while (i < n1) merge[k++] = arr1[i++]
while (j < n2) merge[k++] = arr2[j++]
println("Array after merging: ")
x = 0
while (x < n1 + n2) {
print(merge[x].toString() + " ")
x++
}
}

Output: -

4|Page
2. WAP to take a set of numbers (0-100) in an array and print the frequency of numbers within
given class. For example
Form a class 1-10,11-20,21-30 up to 91-100
And find how many numbers lie within each interval.

Answer: -

Code: -
package com.example.classactivity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import java.util.*

5|Page
fun classFrequency(array:Array<Int>):Array<Int>{
val frequency = Array<Int>(10){0}
for (arr in array){
if(arr in 1..10)
frequency[0] += 1
else if (arr in 11..20)
frequency[1] += 1
else if (arr in 21..30)
frequency[2] += 1
else if (arr in 31..40)
frequency[3] += 1
else if (arr in 41..50)
frequency[4] += 1
else if (arr in 51..60)
frequency[5] += 1
else if (arr in 61..70)
frequency[6] += 1
else if (arr in 71..80)
frequency[7] += 1
else if (arr in 81..90)
frequency[8] += 1
else if (arr in 91..100)
frequency[9] += 1

}
return frequency;
}
fun main(args: Array<String>) {
val read = Scanner(System.`in`);
// array 1 input
println("Enter Size Of Array: ")
val s1 = read.nextInt()
println("Enter Array Elements: ")
var arr1 = Array(s1) {i-> read.nextInt()}

val countResult = classFrequency(arr1);

for(x in 1..10){
val y:Int = ((x-1)*10)+1;
println("Class Frequency in Range " + y +"-"+ x*10 + ": ${countResult[x-1]}");
}
}

Output: -

6|Page
3. WAP to define two 2D arrays of integers as two Matrices. Perform the following
operations
a. Check if the matrices are symmetric and skew symmetric
b. Addition of the two matrices and result to be stored in a third one (Check
compatibility)
Answer: -

Code: -
fun main(args: Array<String>) {
val rows: Int
val cols: Int

val a = arrayOf(intArrayOf(1, 0, 1), intArrayOf(4, 5, 6),


intArrayOf(1, 2, 3))

//Initialize matrix b
val b = arrayOf(intArrayOf(1, 1, 1), intArrayOf(2, 3, 1),
intArrayOf(1, 5, 1))
rows = a.size
cols = a[0].size
val sum = Array(rows) { IntArray(cols) }
for (i in 0 until rows) {
for (j in 0 until cols) {
sum[i][j] = a[i][j] + b[i][j]
}
}
println("Addition of two matrices: ")
for (i in 0 until rows) {
for (j in 0 until cols) {
print(sum[i][j].toString() + " ")
}
println()

7|Page
}
}

Output: -

c. Product of two matrices and result to be stored in a third one (check compatibility)
Answer: -

Code: -
fun main(args: Array<String>) {
val row1: Int
val col1: Int
val row2: Int
val col2: Int

//Initialize matrix a

8|Page
val a = arrayOf(intArrayOf(1, 3, 2), intArrayOf(3, 1, 1),
intArrayOf(1, 2, 2))

//Initialize matrix b
val b = arrayOf(intArrayOf(2, 1, 1), intArrayOf(1, 0, 1),
intArrayOf(1, 3, 1))

//Calculates number of rows and columns present in first matrix


row1 = a.size
col1 = a[0].size

//Calculates the number of rows and columns present in the second


matrix
row2 = b.size
col2 = b[0].size

//For two matrices to be multiplied,


//number of columns in first matrix must be equal to number of rows
in second matrix
if (col1 != row2) {
println("Matrices cannot be multiplied")
} else {
//Array prod will hold the result
val prod = Array(row1) { IntArray(col2) }

//Performs product of matrices a and b. Store the result in


matrix prod
for (i in 0 until row1) {
for (j in 0 until col2) {
for (k in 0 until row2) {
prod[i][j] = prod[i][j] + a[i][k] * b[k][j]
}
}
}
println("Product of two matrices: ")
for (i in 0 until row1) {
for (j in 0 until col2) {
print(prod[i][j].toString() + " ")
}
println()
}
}
}

Output: -

9|Page
d. Transpose of any one matrix and the result to be stored in a separate one.
Answer: -

Code: -
fun main(args: Array<String>) {
val rows: Int
val cols: Int

//Initialize matrix a
val a = arrayOf(intArrayOf(1, 2, 3), intArrayOf(4, 5, 6),
intArrayOf(7, 8, 9))

//Calculates number of rows and columns present in given matrix


rows = a.size
cols = a[0].size

//Declare array t with reverse dimensions


val t = Array(cols) { IntArray(rows) }

//Calculates transpose of given matrix


for (i in 0 until cols) {
for (j in 0 until rows) {
//Converts the row of original matrix into column of
transposed matrix
t[i][j] = a[j][i]
}
}
println("Transpose of given matrix: ")
for (i in 0 until cols) {
for (j in 0 until rows) {
print(t[i][j].toString() + " ")
}
println()
}
}

10 | P a g e
Output: -

e. Obtain a third matrix whose lower part is lower triangular of first matrix and
upper part is the upper triangular of the second matrix (pad the additional parts
with zero values and assign the diagonal to be zero)

11 | P a g e
4. WAP to define two 1 D arrays as two sets of numbers (setA and setB)[keep a check for
the elements to be unique for each set]
Perform the following operations on the two sets
a. Form a new set to get the union of the two given sets
b. Form a new set to get the intersection of the two sets(if no common element –
define the result as disjoint)
Answer: -

12 | P a g e
Code: -
import java.util.HashSet

fun printUnion(
arr1: IntArray, arr2: IntArray, n1: Int,
n2: Int
) {
// Defining set container s
val s: MutableSet<Int> = HashSet()

// Insert the elements of arr1[] to set s


for (i in 0 until n1) {
s.add(arr1[i])
}

// Insert the elements of arr2[] to set s


for (i in 0 until n2) {
s.add(arr2[i])
}
println("Union")
for (itr in s) // s will contain only distinct
// elements from array a and b
print("$itr ")
println()
}

fun printIntersection(
arr1: IntArray, arr2: IntArray,
n1: Int, n2: Int
) {
// Defining set container s
val s: MutableSet<Int> = HashSet()

// Insert the elements of arr1[] to set s


for (i in 0 until n1) {
s.add(arr1[i])
}
println("Intersection")
for (i in 0 until n2) {
// If element is present in set then
if (s.contains(arr2[i])) {
print(arr2[i].toString() + " ")
}
}
println()
}

fun main(args: Array<String>) {


val arr1 = intArrayOf(7, 1, 5, 2, 3, 6)
val arr2 = intArrayOf(3, 8, 6, 20, 7)
val n1 = arr1.size
val n2 = arr2.size

// Function call
printUnion(arr1, arr2, n1, n2)
printIntersection(arr1, arr2, n1, n2)
}

13 | P a g e
Output: -

c. Form a new set to get (setA -setB)


Answer: -

Code: -
fun main(args: Array<String>) {

val input_set_1: MutableSet<Int> = HashSet()


input_set_1.add(45)
input_set_1.add(60)
input_set_1.add(75)
input_set_1.add(90)
println("The first set is defined as: $input_set_1")
val input_set_2: MutableSet<Int> = HashSet()
input_set_2.add(60)
input_set_2.add(90)
println("The second set is defined as: $input_set_2")
input_set_1.removeAll(input_set_2)
println("After subtraction of two sets: $input_set_1")
}

Output: -

14 | P a g e
d. For set A show the combination in the power set.
Answer: -

Code: -
fun printPowerSet(
set: CharArray,
set_size: Int
) {

/*set_size of power set of a set


with set_size n is (2**n -1)*/
val pow_set_size = Math.pow(2.0, set_size.toDouble()).toLong()
var counter: Int
var j: Int

/*Run from counter 000..0 to


111..1*/counter = 0
while (counter <
pow_set_size
) {
j = 0
while (j < set_size) {

/* Check if jth bit in the


counter is set If set then
print jth element from set */if (counter and (1 shl j) > 0)
print(set[j])
j++
}
println()
counter++
}
}
fun main(args: Array<String>) {
val set = charArrayOf('a', 'b', 'c')
printPowerSet(set, 3)
}

15 | P a g e
Output: -

16 | P a g e
5. WAP to store a set of numbers in an array and perform the following operations
a. Find all the consecutive monotonically increasing sequence of numbers in the
array
b. Find the first and second maximum number within the array (without sorting)
c. Rearrange the array such that all the odd and even numbers exist alternatively as
long it permits.
Answer: -

Code: -
fun main(args: Array<String>) {
val arr = intArrayOf(1, 2, 3, 5, 6, 4, 8, 5, 7)

17 | P a g e
println("Check Increasing Monotonic order")
checkmonotonic(arr)
println()
println("Finding 2 Max numbers: ")
find2max(arr)
println()
println("Find series of odd even: ")
rearrangeoddeven(arr)
}

private fun rearrangeoddeven(arr: IntArray) {


val result = IntArray(arr.size)
var e = 0
var o = 1
for (i in 0 until result.size - 2) {
if (arr[i] % 2 == 0) {
result[e] = arr[i]
e += 2
} else {
result[o] = arr[i]
o += 2
}
}
for (i in result.indices) {
print(result[i].toString() + " ")
}
}

private fun find2max(arr: IntArray) {


var max1 = 0
var max2 = 0
var index = 0
for (i in arr.indices) {
if (max1 < arr[i]) {
max1 = arr[i]
index = i
}
}
for (i in arr.indices) {
if (i != index && max2 < arr[i]) {
max2 = arr[i]
}
}
println("First max: $max1")
println("Second max: $max2")
}

fun checkmonotonic(arr: IntArray) {


val N = arr.size
var inc = true
for (i in 0 until N - 1) {
inc = arr[i] <= arr[i + 1]
}
if (inc) println("Increasing Monotonic") else println("Not an
increasing monotonic" }

Output: -

18 | P a g e
19 | P a g e

You might also like