Answer to Question #285335 in C++ for Zain

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


1
Expert's answer
2022-01-07T05:01:39-0500
#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!

Comments

No comments. Be the first!

Leave a comment