Professional Documents
Culture Documents
(CS1205)
Assignment - 2
Submitted by: -
Vikas Janu
2020btechCSE083
Section – B
Submitted to: -
Prof. Pranab Kumar Roy
Nov, 2022
1|Page
Table Of Content
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()}
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
//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))
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))
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()
fun printIntersection(
arr1: IntArray, arr2: IntArray,
n1: Int, n2: Int
) {
// Defining set container s
val s: MutableSet<Int> = HashSet()
// Function call
printUnion(arr1, arr2, n1, n2)
printIntersection(arr1, arr2, n1, n2)
}
13 | P a g e
Output: -
Code: -
fun main(args: Array<String>) {
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
) {
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)
}
Output: -
18 | P a g e
19 | P a g e