Question

In: Computer Science

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 data into a vector. Then, based on the number of items, find the middle item. 276452.1593070

( Must include vector library to use vectors)

Solutions

Expert Solution

#include <iostream>
#include <vector>

using namespace std;

int main() {
    const int NUM_ELEMENTS = 9;
    vector<int> userValues;
    int num = 0, count = 0;
    while (num != -1) {
        cin >> num;
        if (num != -1) {
            if (count == NUM_ELEMENTS) {
                cout << "Too many inputs" << endl;
                return 0;
            }
            userValues.push_back(num);
            ++count;
        }
    }
    cout << "Middle item: " << userValues[count / 2] << endl;
    return 0;
}

#include <iostream>
#include <vector>

using namespace std;

int main() {
    const int NUM_ELEMENTS = 9;
    vector<int> userValues;
    int num = 0, count = 0;
    while (num != -1) {
        cin >> num;
        if (num != -1) {
            if (count == NUM_ELEMENTS) {
                cout << "Too many inputs" << endl;
                return 0;
            }
            userValues.push_back(num);
            ++count;
        }
    }
    cout << userValues[count / 2] << endl;
    return 0;
}


Related Solutions

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.
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,...
Write a C program whose input is two integers, and whose output is the first integer...
Write a C program whose input is two integers, and whose output is the first integer and subsequent increments of 10 as long as the value is less than or equal to the second integer. Ex: If the input is: -15 30 the output is: -15 -5 5 15 25 Ex: If the second integer is less than the first as in: 20 5 the output is: Second integer can't be less than the first. For coding simplicity, output a...
Create a C++ program that will prompt the user to input an integer number and output...
Create a C++ program that will prompt the user to input an integer number and output the corresponding number to its numerical words. (From 0-1000000 only) **Please only use #include <iostream>, switch and if-else statements only and do not use string storing for the conversion in words. Thank you.** **Our class is still discussing on the basics of programming. Please focus only on the basics. Thank you.** Example outputs: Enter a number: 68954 Sixty Eight Thousand Nine Hundred Fifty Four...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find the index of the negative integer, and compute its average complexity.
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...
Write a C++ program to find the number of pairs of integers in a given array...
Write a C++ program to find the number of pairs of integers in a given array of integers whose sum is equal to a specified number.
Develop a C++ function to find the number of even and odd integers in a given...
Develop a C++ function to find the number of even and odd integers in a given array of integers Pass in an array of integers Pass in the size of the array of integers Pass in a reference parameter for the number of even values Pass in a reference parameter for the number of odd values The function doesn't return anything Develop a C++ program to test your function
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT