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
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
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.
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
Creating a list/tuple 4. Write a program to accept a string from the user, then replace...
Creating a list/tuple 4. Write a program to accept a string from the user, then replace the first letter with the last letter, the last letter with the middle letter and the middle letter with the first letter. E.g. “Billy” -> “yiBll”.
how are mission statements, program goals, program objectives, and a program philosophy related?
how are mission statements, program goals, program objectives, and a program philosophy related?
13-16. As a group, develop your promotional objectives. Whatare the initial objectives of your promotional campaign?Will...
13-16. As a group, develop your promotional objectives. Whatare the initial objectives of your promotional campaign?Will these change over time? 13-17. What role will each element of the promotional mix playin your promotional plan? 13-18. Develop a one- to two-page recommendation that youcould present to the university’s leadership team. 13-19. Do you think that the promotional mix will change overtime? How might it be different inthree years? Be sure toaddress this in your proposal.
Develop a recursive algorithm that multiply two integer numbers x and y, where x is an...
Develop a recursive algorithm that multiply two integer numbers x and y, where x is an m-bit number and y is an n-bit number (10 points), and analyze the time complexity of this algorithm (10 points).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT