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...
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
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).
Develop, write and certify a properly recursive procedure reverseDigits to input a positive integer n and...
Develop, write and certify a properly recursive procedure reverseDigits to input a positive integer n and to output the integer formed by reversing the digits of n. Thus (reverseDigits 1234) returns the integer 4321. Please answer using Dr Racket(R5RS language)
There is a Java program that is missing one recursive function: public class Ackermann { /*...
There is a Java program that is missing one recursive function: public class Ackermann { /* / n+1 when m = 0 * ack(m,n) = | ack(m-1,1) when m > 0 and n = 0 * \ ack(m-1, ack(m, n-1)) otherwise */ public static int ack(int m, int n) { return 0; } /* Ackermann's Function Test Framework * * Be extremely careful with these test cases. Ackermann's grows very fast. * For example, ack(4, 0) = 13, but ack(5,0)...
There is a Java program that is missing one recursive function: public class BinarySearch { /*...
There is a Java program that is missing one recursive function: public class BinarySearch { /* / -1 when min > max * | mid when A[mid] = v * search(A, v, min, max) = | search(A,v,mid+1,max) when A[mid] < v * \ search(A,v,min,mid-1) otherwise * where mid = (min+max)/2 */ public static int search_rec(int[] A, int v, int min, int max) { return 0; } public static int search(int[] A, int v) { return search_rec(A, v, 0, A.length-1); }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT