Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- // Take a list of numbers.
- // Display a string indicating more even or
- // more odd 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
- // a string indicating more even or more odd numbers, or
- // an equal amount of even and odd numbers.
- string get_even_odd_balance(vector<int>& number_list)
- {
- // The text stating more even or odd numbers.
- string balance;
- // These store the number of odd and even numbers.
- int number_of_even_numbers = 0;
- int number_of_odd_numbers = 0;
- // 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 one to the number of even numbers.
- number_of_even_numbers++;
- }
- else
- {
- // Add one to the number of odd numbers
- number_of_odd_numbers++;
- }
- }
- // If there are more odd numbers than odd numbers,
- if (number_of_even_numbers < number_of_odd_numbers)
- {
- // DIsplay there are more odd numbers.
- balance = "There are more odd numbers.";
- }
- // Otherwise, if there are more odd numbers than odd numbers,
- else if (number_of_even_numbers > number_of_odd_numbers)
- {
- // Display there are more even numbers.
- balance = "There are more even numbers.";
- }
- // Otherwise, if there is an equal amount,
- else
- {
- // Display there is an equal amount.
- balance = "There is an equal number of even and odd numbers.";
- }
- return balance;
- }
- int main()
- {
- // Create a list of numbers.
- vector<int> numbers = { 1, 2, 3, 12, 24, 14, 19, 30 };
- // Get a string stating more or less even numbers.
- string even_odd_balance = get_even_odd_balance(numbers);
- // Display the result.
- cout << even_odd_balance << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement