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.
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
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})...
In this task you will implement a C++ function with arguments: a sorted integer array, size...
In this task you will implement a C++ function with arguments: a sorted integer array, size of the array, and a target integer value. Find all combinations of two elements in the sorted array which sum up to the target value. When found, add the combinations into an array, and print it. Where there is greater than one combination, you may use the number 200 as a separator for the combinations in the output array. Do not use more than...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT