In: Computer Science
I have implemented the sumAndCount(Int,Int) as per the given description.
Please find the following Code Screenshot, Output, and Code.
ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT
1.CODE SCREENSHOT :
2.OUTPUT :
3.CODE :
object SumAndCountDemo{
def sumAndCount (sum:Int,count:Int):List[Int]={
//print the menu
print("Enter an Int (type \"quit\" to quit):\n>");
//read the data
var input = scala.io.StdIn.readLine()
//if the input is quit then return
if(input.toLowerCase()=="quit"){
//create the output list to return
var nums: List[Int] = List()
//append the term to the list
nums = nums:+(count);
nums = nums:+(sum);
//return the list
return nums;
}
else if(scala.util.Try(input.toInt).isSuccess){
//if the user enter a valid integer then call the sumAndCount with updated counts
var t=input.toInt;
sumAndCount(sum+t,count+1);
}else{
//otherwise print error message
print("That is not an Int or \"quit\"!\n");
sumAndCount(sum,count);
}
}
def main(args: Array[String]):Unit={
var nums: List[Int] =sumAndCount(0,0);
print("The average of your "+nums(0)+" number is "+(nums(1).toFloat/nums(0)).ceil.toInt);
}
}