35.3k views
9 votes
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 the 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: first read the data into a vector. then, based on the number of items, find the middle item.
#include
#include // must include vector library to use vectors
using namespace std;
int main() {
/* type your code here. */
return 0;
}

1 Answer

13 votes

integer userInput

integer i

integer mid

integer array(20) number

userInput = 1

for i = 0; userInput >= 0; i = i + 1

if number[i] > -1

userInput = Get next input

number[i] = userInput

i = i - 1

mid = i / 2

if i > 9

Put "Too many inputs" to output

elseif i % 2 == 0

Put number[mid - 1] to output

else

Put number[mid] to output

User Jrcalabrese
by
3.8k points