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!”.
#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;
}
Comments
Leave a comment