Question

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

****Coral Please****

Given a sorted list of integers, output the middle integer. Assume the number of integers is always odd.

Ex: If the input is 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. If exceeded, output "Too many inputs".

Hint: Use an array of size 9. First read the data into an array. Then, based on the number of items, find the middle item.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.Scanner;

public class Demo

{

    public static void main(String[] args){

    

        Scanner sc = new Scanner(System.in);

        int arr[];

        arr = new int[10];

        int count =0;

        int i;

        for (i = 0; i < arr.length; i++){

        

            arr[i] = sc.nextInt();

            count++;

            if(arr[i]<0){

            

                count =count -1;

                break;

}

}

            

        

        if(i>9){

        

            System.out.println("To Many inputs.");

}

       else if(count%2==0){

        

            System.out.println("No of Integers Must Be Odd");

}

       else{

          

                int mid = arr[count/2];

                System.out.println(mid);

}

    

}

}

Add a comment
Know the answer?
Add Answer to:
****Coral Please**** Given a sorted list of integers, output the middle integer. Assume the number of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Arrays: Middle item Given a set of data, output the middle item (if even number of items, output the two middle items). Ex: If the input is 5 7 9 11 13 -1 (a negative indicates end), the output is: 9...

    Arrays: Middle item Given a set of data, output the middle item (if even number of items, output the two middle items). Ex: If the input is 5 7 9 11 13 -1 (a negative indicates end), the output is: 9 Ex: If the input is 5 7 9 11 -1, the output is: 7 9 The maximum number of inputs for any test case will not exceed 5. If the input exceeds this maximum, output "Too many inputs". Hint:...

  • 8.10 LAB: Middle item Given a set of data, output the middle item (if even number...

    8.10 LAB: Middle item Given a set of data, output the middle item (if even number of items, output the two middle items). Assume that the data set will always contain less than 20 items. Ex: If the input is 579 11 13-1 (a negative indicates end), the output is: Ex: If the input is 57 911-1, the output is: 7 9 Hint: First read the data into an array. Then, based on the array's size, find the middle item(s)...

  • Write a program that first gets a list of integers from the input and adds them...

    Write a program that first gets a list of integers from the input and adds them to an array. The input begins with an integer indicating the number of integers that follow (Your program should work for any size of the array). Then, get the last value from the input, and output all integers less than or equal to that value by iterating through the array. Ex: If the input is: Enter the number of integers: 5 Enter integer 1:...

  • *Coral Language* Write a program that first gets a list of six integers from input. The first five values are the intege...

    *Coral Language* 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 like...

  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • Write a mips program that defines two integer array that are pre-sorted and the same size...

    Write a mips program that defines two integer array that are pre-sorted and the same size (e.g., [3, 7, 9, 11, 15, 21] and [1, 4, 6, 14, 18, 19]) and a function merge that takes the two arrays (and their size) as inputs and populates a single array of twice the size of either input array that contains the elements of both arrays in ascending order. In the example arrays given, then output would be [1, 3, 4, 6,...

  • IN JAVA please Given a sorted array and a target value, return the index if the...

    IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...

  • ==> Please In C Language <== Insert a value in a sorted array: Given an integer...

    ==> Please In C Language <== Insert a value in a sorted array: Given an integer array a[9] = { 2, 3, 5, 7, 11, 13, 17, 19 }, read in an integer k from the screen and insert k into the array so the new array remains sorted. Note that array a has size 9, but there are only 8 initial values. This ensures that no value will be lost after the insertion. For example, when k=12 , the...

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • C++ please 27.5 Loops (nested)**: Sum a given number of integers A user will enter an...

    C++ please 27.5 Loops (nested)**: Sum a given number of integers A user will enter an initial number, followed by that number of integers. Output those integers' sum. Repeat until the initial number is O or negative. Ex: If the user enters 3 96 1 0, the output is 16 Ex: If the user enters 396125 3 0, the output is 16 Hints: Use a while loop as an outer loop. Get the user's initial number of ints before the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT