Remove even numbers
Make a program that takes an input that determines how many inputs the user wants to submit input_count.
Then collect input_count inputs and place them into a std::vector<int> array.
After collecting the numbers, print the array using a function print_vector (you have to create this function).
Then remove all even numbers from the array, and print it again.
Hint
You can use vec.erase(vec.begin() + index) to removePa the element with index index from a std::vector (vec in this case).
Answer
#include <iostream>
#include <vector>
void print_vector(std::vector<int> vector)
{
std::cout << "Current Array:\n";
for (int i = 0; i < vector.size(); i++)
{
std::cout << vector[i] << " ";
}
std::cout << "\n";
}
int main()
{
int input_count;
std::cout << "How many inputs do you want to submit? ";
std::cin >> input_count;
std::vector<int> inputs;
for (int i = 0; i < input_count; i++)
{
std::cout << "Input " << i + 1 << ": ";
int input;
std::cin >> input;
inputs.push_back(input);
}
print_vector(inputs);
// Remove all even numbers
for (int i = 0; i < inputs.size(); i++)
{
if (inputs[i] % 2 == 0)
{
inputs.erase(inputs.begin() + i);
i--; // Adjust index to account for the removed element
}
}
print_vector(inputs);
}