Passing same data to two or more methods/functions


#1

I am trying to call two mwthods in main method and passing same data to both methods. When execution of second method strats, value of parameter sets to zero. How to solve this. Code is given below.
object NestedLoops {
def main(args: Array[String]): Unit = {
println(“Enter number of process:”);
val n = scala.io.StdIn.readInt();
var array: Array[Array[Double]] = Array.ofDim(n, 2)
var bt: Double = 0
var at: Double = 0
for (i <- 0 to n - 1) {
println("Enter BT for process: " + i);
bt = scala.io.StdIn.readDouble();
println("Enter AT for process: " + i);
at = scala.io.StdIn.readDouble();
array(i)(0) = at
array(i)(1) = bt
}
One(n, array)
Two(n, array)
}
def One(n: Int, data: Array[Array[Double]]): Unit = {
var q = 0.0
var arr = data
arr = arr.sortBy(x => x(1))
var arr_copy = arr
var wt = new ArrayDouble
var a = new ArrayDouble
var tat = new ArrayDouble
var new_tat = new ArrayDouble
var new_wt = new ArrayDouble
var D_Sum = 0.0
var sum = 0.0
for (i <- 0 to n - 1) {
a(i) = arr(i)(1)
}
for (i <- 0 to n - 1) {
wt(i) = 0
}
var tracker = 0.0
var li = 0
var updated_TAT = 0.0
do {
D_Sum = 0.0
arr = arr.filterNot(x => x(1) <= 0)
for (i <- 0 to arr.size - 1) {
D_Sum = D_Sum + arr(i)(1)
}
q = math.round(math.sqrt(arr.length D_Sum 1.49))
for (i <- 0 to arr.size - 1) {
if (tracker >= arr(i)(0)) {
if (arr(i)(1) > q) { //if BT > quantum time
arr(i)(1) -= q
tracker = tracker + q
for (j <- 0 to arr.size - 1) {
if ((j != i) && (arr(j)(1) != 0))
wt(j) += q
}
}
else {
for (j <- 0 to arr.size - 1) {
if ((j != i) && (arr(j)(1) != 0)) {
wt(j) += arr(i)(1)
}
}
tracker = tracker + arr(i)(1)
updated_TAT = tracker
new_tat(li) = updated_TAT - arr_copy(i)(0)
new_wt(li) = tracker - arr_copy(i)(1) - arr_copy(i)(0)
li = li + 1
arr(i)(1) = 0
}
}
}
sum = 0.0
for (i <- 0 to arr.length - 1)
sum = sum + arr(i)(1)
} while (sum != 0)
var avg_wt = 0.0
var avg_tat = 0.0
for (j <- 0 to n - 1)
avg_wt += wt(j)
for (j <- 0 to n - 1)
avg_tat += new_tat(j)
println("average waiting time= " + (avg_wt / n) + " Average turn around time= " + (avg_tat / n))
}
def Two(n: Int, data: Array[Array[Double]]): Unit = {
var arr = data
arr = arr.sortBy(x => x(1))
var arr_copy = arr
var q = 0.0
var wt = new ArrayDouble
var a = new ArrayDouble
var tat = new ArrayDouble
var new_tat = new ArrayDouble
var new_wt = new ArrayDouble
var sum = 0.0
for (i <- 0 to n - 1) {
a(i) = arr(i)(1)
}
for (i <- 0 to n - 1) {
wt(i) = 0
}
var tracker = 0.0
var li = 0
var updated_TAT = 0.0
do {
arr = arr.filterNot(x => x(1) <= 0)
q = arr(0)(1)
for (i <- 0 to arr.size - 1) {
if (tracker >= arr(i)(0)) {
if (arr(i)(1) > q) { //if BT > quantum time
arr(i)(1) -= q
tracker = tracker + q
for (j <- 0 to arr.size - 1) {
if ((j != i) && (arr(j)(1) != 0))
wt(j) += q
}
}
else {
for (j <- 0 to arr.size - 1) {
if ((j != i) && (arr(j)(1) != 0)) {
wt(j) += arr(i)(1)
}
}
tracker = tracker + arr(i)(1)
updated_TAT = tracker
new_tat(li) = updated_TAT - arr_copy(i)(0)
new_wt(li) = tracker - arr_copy(i)(1) - arr_copy(i)(0)
li = li + 1
arr(i)(1) = 0
}
}
}
sum = 0.0
for (i <- 0 to arr.length - 1)
sum = sum + arr(i)(1)
} while (sum != 0)
var avg_wt = 0.0
var avg_tat = 0.0
for (j <- 0 to n - 1)
avg_wt += wt(j)
for (j <- 0 to n - 1)
avg_tat += new_tat(j)
println("average waiting time= " + (avg_wt / n) + " Average turn around time= " + (avg_tat / n))
}
}


#2

I must admit , I’m having some trouble following this code - what are the methods you are calling? From what you posted, I see no user-defined methods or functions being called.


#3

Please check it now,


#4

Both of your methods change the array they are given. Neither var arr = data nor var arr_copy = arr makes a copy of your array. Assigning to a val only gives it a new (additional) name. So when you make changes to arr_copy, they are done to the array you passed in directly.

If you want to keep the imperative style with mutating the array, you have to manually copy it. You can do this with the clone method, but because your array is 2D (represented as array of arrays in Scala), you have to apply it to each element of the outer array: data.map(_clone) will make a deep copy (only for 2D, not more)