Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- // Take a list of numbers and return the indices of the even numbers.
- // This function takes an integer and returns
- // true if it is even. Otherwise, it returns false.
- int is_even(int number)
- {
- // If two divides into the number evenly
- if (number % 2 == 0)
- {
- // The number is even.
- return true;
- }
- else // Otherwise,
- {
- // The number is odd.
- return false;
- }
- }
- // This function takes a list of numbers and returns
- // the indices of the even numbers.
- vector<int> get_even_numbers(vector<int>& number_list)
- {
- // These are the indices of the even numbers.
- vector<int> even_number_indices;
- // We are testing this number.
- int current_number;
- // Go from the beginning to the end of the number list.
- for (size_t i = 0; i < number_list.size(); ++i)
- {
- // Test the next number.
- current_number = number_list[i];
- // If the number is even,
- if (is_even(current_number))
- {
- // Add its index to the new list.
- even_number_indices.push_back(i);
- }
- }
- return even_number_indices;
- }
- int main()
- {
- // Create a list of numbers.
- vector<int> numbers = { 1, 2, 3, 12, 24, 14, 19, 30 };
- // Get a list of the even numbers.
- vector<int> even_numbers = get_even_numbers(numbers);
- // Display the even numbers.
- for (size_t i = 0; i < even_numbers.size(); ++i)
- {
- cout << even_numbers[i] << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement