Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip> // 3.2131234 -> 3.21
- #include <cmath> // abs()
- using namespace std;
- int findIndex(int vector[], int vector_size, int searched_value, unsigned int times)
- {
- int counter = 0;
- int index = -1;
- for (int i = 0; i < vector_size; i++)
- {
- if (vector[i] == searched_value)
- {
- counter++;
- if (counter == times)
- {
- index = i;
- }
- }
- }
- return index;
- }
- int main() {
- int vec[] = {10, 10, 30, 40, 10, 60, 90, 80, 90};
- int vec_size = sizeof(vec) / sizeof(vec[0]);
- int searched_value = 0;
- unsigned int times = 0;
- cout << "What value are you looking for: ";
- cin >> searched_value;
- cout << "How many times do you want to find it: ";
- cin >> times;
- int result = findIndex(vec, vec_size, searched_value, times);
- if (result == -1)
- {
- cout << "Searched element is not found or it doesn't appear " << times << " times.";
- exit(1);
- }
- else {
- cout << "Element " << searched_value << " was found at index " << result << ".";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment