Question

In: Computer Science

Objectives To develop a recursive solution that reurns a tuple To develop a program that manages...

Objectives

To develop a recursive solution that reurns a tuple
To develop a program that manages bad input
Description

Write a program (sumAndCount.scala) that uses a recursive function that reads Int values from a user one at a time until the user types in the word "quit" (case insensitive!). Your function should return a tuple containing 2 Int values. The first should be the sum of all the Ints, and the second should be the number of Ints entered. Your main program should call this function, and print out the average (rounded) of the values that were entered. Note: You will also need to manage bad input! If the user types something in that isn't an Int or the word "quit", your program should print an error message and ask again! As always, pay close attention to the specific messages that your program must print.

Remember: You don't want any side effects with recursion, so you should not use any "global" variables in your program!

Sample Run 1

$ scala sumAndCount.scala
Enter an Int (type "quit" to quit):
>3
Enter an Int (type "quit" to quit):
>55
Enter an Int (type "quit" to quit):
>78
Enter an Int (type "quit" to quit):
>2
Enter an Int (type "quit" to quit):
>Quit
The average of your 4 numbers is 35
Sample Run 2

$ scala sumAndCount.scala
Enter an Int (type "quit" to quit):
>2
Enter an Int (type "quit" to quit):
>3
Enter an Int (type "quit" to quit):
>4.0
That is not an Int or "quit"!
Enter an Int (type "quit" to quit):
>4
Enter an Int (type "quit" to quit):
>5
Enter an Int (type "quit" to quit):
>quiT
The average of your 4 numbers is 4
Sample Run 3

$ scala sumAndCount.scala
Enter an Int (type "quit" to quit):
>3
Enter an Int (type "quit" to quit):
>78
Enter an Int (type "quit" to quit):
>2
Enter an Int (type "quit" to quit):
>245
Enter an Int (type "quit" to quit):
>quiit
That is not an Int or "quit"!
Enter an Int (type "quit" to quit):
>quit
The average of your 4 numbers is 82
Hints

Similar to the previous assignment, write 2 recursive functions. In this case, however, your function should check to make sure that the user enters eith an Int or the word "quit"
Consider (carefully!) what type of read you would need to use to accept either an Int or a String (in case the user enters "quit") Both are legal!
use the following prototype for your main recursive function:
def sumAndCount: (Int,Int) = { ... }
ACTIONS

Solutions

Expert Solution

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);
        }
}

Related Solutions

DESCRIPTION Objectives To develop a recursive solution to an open-ended iterative problem To develop a program...
DESCRIPTION Objectives To develop a recursive solution to an open-ended iterative problem To develop a program that manages bad input Description Write a program (printSquares.scala) that read non-negative Int values from a user one at a time. For each positive Int, print out a message with the square of the number. Your program should quit when the use enters a negative Int. Note: You will also need to manage bad input! If the user types something in that isn't an...
develop/ list the financial objectives of a start up bar & grill. also list/develop strategic objectives...
develop/ list the financial objectives of a start up bar & grill. also list/develop strategic objectives of a start up bar and grill
Task #1 Develop a recursive method to reverse a list Develop a method with the prototype...
Task #1 Develop a recursive method to reverse a list Develop a method with the prototype public static void reverse (ArrayList inputList) based on selecting the first list element as the head and the remaining list as its tail. Here is the recursive design. 1) Base case: The problem is trivial when the list size is 0 or 1. 2) Decomposition: For lists with size > 1: a) Extract its head (element) and leave the tail (the input list with...
develop a recursive function to compute LCS (x,y)
develop a recursive function to compute LCS (x,y)
This is a short but tricky program that uses multiple variables and tuple assignment. Fill in...
This is a short but tricky program that uses multiple variables and tuple assignment. Fill in the blanks such that the output is "10". a = ((2,4), (1, 6)) b, (c, d) = a e = ____ + ____ [ ____ ] print(e) # Should print "10" First blank:                            [ Select ]                       ["c", "b", "e", "d", "a"]       Second blank:               ...
Please write in Python code Write a program that stores the following data in a tuple:...
Please write in Python code Write a program that stores the following data in a tuple: 54,76,32,14,29,12,64,97,50,86,43,12 The program needs to display a menu to the user, with the following 4 options: 1 – Display minimum 2 – Display maximum 3 – Display total 4 – Display average 5 – Quit Make your program loop back to this menu until the user chooses option 5. Write code for all 4 other menu choices
Lab #3 – Recursion on Strings Lab Objectives • Be able to write a recursive Java...
Lab #3 – Recursion on Strings Lab Objectives • Be able to write a recursive Java method for Java strings • Be able to make use of exploit natural conversion between strings and integers • Be able to write a driver to call the methods developed for testing Deliverables Submit the report in Word or PDF format with the results of every task listed below. They should show screen capture of your results. The problem String as a sequence of...
What are the differences between outcome objectives and communication objectives? Develop an example of each type...
What are the differences between outcome objectives and communication objectives? Develop an example of each type of objective for a health issue of your choice.
C++ Create a recursive program what will test three recursive functions. It would have a menu...
C++ Create a recursive program what will test three recursive functions. It would have a menu like: 1. Recursive factorial 2. Towers of Hanoi 3. Recursive summation 0. Exit Enter selection: 3 Enter number: 4 The recursive summation will take an integer and sum value from 1 to the integer. Don’t enter a number if the selection is 0 for quit. Please have the functions in an implementation file and the prototypes of the functions in a header file and...
Develop a recursive algorithm to find the smallest and largest element in an array and trace...
Develop a recursive algorithm to find the smallest and largest element in an array and trace the recursive function with appropriate message. using c++ add comment to the code
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT