// // simple demo of using recursive functions to operate on arrays // println("how many values?") val n = readInt val a = new Array[Int](n) fillArray(a, 0) println("you entered:") printArray(a, 0) println("sum is " + sumArray(a, 0)) println("product is " + productArray(a, 0)) def fillArray(aa : Array[Int], startIndex : Int) { if (startIndex < aa.length) { println("next value?") val n = readInt aa(startIndex) = n fillArray(aa, startIndex + 1) } } def printArray(aa : Array[Int], startIndex : Int) { if (startIndex < aa.length) { println(aa(startIndex)) printArray(aa, startIndex + 1) } } def sumArray(aa : Array[Int], startIndex : Int) : Int = { if (startIndex < aa.length) { aa(startIndex) + sumArray(aa, startIndex + 1) } else { 0 } } def productArray(aa : Array[Int], startIndex : Int) : Int = { if (startIndex < aa.length) { aa(startIndex) * productArray(aa, startIndex + 1) } else { 1 } }