How can i check if the position of an element in a one-dimensional array(vector) is odd or even?
can i get an example :)
Last edited on
What is the actual question?
Is it: Is an integer odd?
Is it: What is the index of element?
At least one of those was discussed in your earlier thread.
i solved the previous question
Last edited on
The position of an element : positions are 0,1,2,3,4..
And i need to know when i introduce an element to know if its on a odd position or even
How are you adding an element to the vector?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <vector>
#include <iostream>
#include <algorithm>
int main()
{
const char* const odev[] = {"even", "odd"};
std::vector<int> vi {2, 4, 6, 8};
int n {};
std::cout << "Enter number to find: ";
std::cin >> n;
if (const auto fnd = std::find(vi.begin(), vi.end(), n); fnd != vi.end())
std::cout << odev[(fnd - vi.begin()) % 2] << " index\n";
else
std::cout << "Not found\n";
std::cout << "Enter new number: ";
std::cin >> n;
vi.push_back(n);
std::cout << odev[(vi.size() - 1) % 2] << " index\n";
}
| |
Enter number to find: 4
odd index
Enter new number: 10
even index
|
Last edited on