Question #170454

Create a program using a one-dimensional array that accepts five input values from the keyboard. Then it should also accept a number to search. This number is to be searched if it is among the five input values. If it is found, display the message “Searched number is found!”, otherwise display “Search number is lost!”.


Expert's answer

#include <iostream>

int main()
{
    const int ARRAY_SIZE = 5;
    
    std::cout << "Please enter " << ARRAY_SIZE <<" numbers: ";
    int numbers[ARRAY_SIZE];
    for(int i=0; i<ARRAY_SIZE; ++i)
    {
        std::cin >> numbers[i];

            if(!std::cin)
            {
                std::cout << "Bad input\n";
                return 1;
            }        
    }

    std::cout << "Please the number to search: ";
    int value;
    std::cin >> value;

        if(!std::cin)
        {
            std::cout << "Bad input\n";
            return 1;
        }  

    for(int i=0; i<ARRAY_SIZE; ++i)
    {
        if(numbers[i] == value)
        {
            std::cout << "Searched number is found!\n";
            return 0;
        }
    }

    std::cout << "Search number is lost!\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