Question

In: Computer Science

In Coral. Given a sorted list of integers, output the middle integer .Assume the number of...

In Coral. Given a sorted list of integers, output the middle integer .Assume the number of integers ia odd. Ex: if the input 2 3 4 8 11 -1(a negative indicates end), the output is 4.
the maximum number of inputs for any test case should not exceed 9 positive values. If exceeded , output Too many inputs". Hint: Use an array of size 9. First read the data into array.Then,based in the number of items, find the middle item.

Solutions

Expert Solution

The logic for this is simple. Keep taking input until -1 is given and for every input, increment the count value (0 indexed) so that when the count becomes 8 (actually 9), it means 9 elements are given so any more input causes "Too many inputs" as output. The code is shown below:

// Create an array of size 9
integer array(9) nums
integer x
integer count

// To check if broke while because of overflow count
integer flag

count = 0

// Initialize it to 0
flag = 0

while x != -1
   x = Get next input
   // If count is 8, required elements are got (9)
   // So no need of any more so to break, put x = -1
   if count == 9
      if x != -1
         x = -1
         flag = 1
      
   // If x is not -1, put in array and increment count
   if x != -1
      nums[count] = x
      count = count + 1
      
// If broke while because of count
if flag == 1
   Put "Too many inputs" to output
else
   Put nums[count/2] to output

A screenshot for indentation purposes which will help you to code clearly is given below:

A screenshot of the output for two different inputs:

and

Hope this is helpful. In case of any queries, please comment below. All the best :)


Related Solutions

In C++ Given a sorted list of integers, output the middle integer. A negative number indicates...
In C++ Given a sorted list of integers, output the middle integer. A negative number indicates the end of the input (the negative number is not a part of the sorted list). Assume the number of integers is always odd. Ex: If the input is: 2 3 4 8 11 -1 the output is: Middle item: 4 The maximum number of inputs for any test case should not exceed 9. If exceeded, output "Too many numbers". Hint: First read the...
In Coral Code Please!!!! The assignment is to get an integer from input, and output that...
In Coral Code Please!!!! The assignment is to get an integer from input, and output that integer squared, ending with newline. (Note: This assignment is configured to have students programming directly in the zyBook. Instructors may instead require students to upload a file). Below is a program that's been nearly completed for you. Click "Run program". The output is wrong. Sometimes a program lacking input will produce wrong output (as in this case), or no output. Remember to always pre-enter...
in coral write a program whose inputs are three integers, and whose output is the largest...
in coral write a program whose inputs are three integers, and whose output is the largest of the three values. Ex: If the input is 7 15 3, the output is: 15
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Input: arr = [1,2,2,6,6,6,6,7,10] Output:
Design a function called middle_value, that takes 3 integers, and returns the integer with the middle...
Design a function called middle_value, that takes 3 integers, and returns the integer with the middle value. If there is a tie, any of the possible middle values can be returned. Example: middle_value(1, 2, 8) -> 2 middle_value(9, 7, 7) -> 7 middle_value(3, 3, 3) -> 3 Design a function called combine_strings that takes two phrases and appends the longer string onto the back of the shorter one with no space between the two phrases joined. Example: If the phrases...
Task 1: Remove Number Complete the function remove number such that given a list of integers...
Task 1: Remove Number Complete the function remove number such that given a list of integers and an integer n, the function removes every instance of n from the list. Remember that this function needs to modify the list, not return a new list. Task 2: Logged List The log2() function is one of an algorithm designer’s favourite functions. You’ll learn more about this later, but briefly – if your input size is 1048576 elements, but you only look at...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether...
Given a list of positive integers c[0...n − 1], and a positive integer v, decides whether we can use numbers from c[0...n − 1] to make a sum of v, allowing any particular number in the list to be used multiple times. Or, mathematically, this means that there exists non-negative integer coefficients, x0, x1, ..., xn−1, such that v = x0c[0] + x1c[1] + ...xn−1c[n − 1]. For example, given c[0...3] = {2, 4, 6, 10}, and v = 17,...
Java, no imports: Given a list of integers, round each number to the nearest multiple of...
Java, no imports: Given a list of integers, round each number to the nearest multiple of 5.        2.5-7.49 round to 5. 7.5-12.49 round to 10. 12.5-17.49 round to 15. etc.        return the sum of all the rounded elements.        Implement this way for credit:        Fill in the method directly below this one called helperRound() to round each number. Call that method and use the sum of the returned values.        helperSum({4.3, 16.7})...
CORAL Language Only: Write a program that first gets a list of six integers from input....
CORAL Language Only: Write a program that first gets a list of six integers from input. The first five values are the integer list. The last value is the upper threshold. Then output all integers less than or equal to the threshold value. Ex: If the input is 50 60 140 200 75 100, the output is: 50 60 75 For coding simplicity, follow every output value by a space, including the last one. Such functionality is common on sites...
Suppose I have a list of 17 sorted elements (A0 ---- A16) and an integer x.....
Suppose I have a list of 17 sorted elements (A0 ---- A16) and an integer x.. Assuming that we compare x with the first element A0, if x is equal to A0 , we found x and we return 1; otherwise we jump 4 elements (to A4). If x is equal to A4, we return 5 (index + 1), otherwise if it is less we back up 1 space to A3; if x is equal to A3 we return 4,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT