Question #285335

Write a program to search the array element. Enter a value from the keyboard and find out the location of the entered value in the array. If the entered number is not found in the array display the message


Expert's answer

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> v { 1, 5, 8, 3, 2, 10, -10, -5, 4, -4};
    int value;
    std::cin >> value;
    if (auto it = std::find(v.begin(), v.end(), value); 
        it != v.end()) 
    {
        std::cout << "location: " << it - v.begin() << '\n';
    }
    else {
        std::cout << "couldn't found the value\n";
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS