Write down the code to traverse the arrayuntill X located ?
#include <iostream>
int main()
{
const int ARRAY_SIZE = 10;
int data[ARRAY_SIZE] = {1, 11, 43, 32, 63, 3, 19, 54, 17, 21};
std::cout << "Please enter the number to locate: ";
int number;
std::cin >> number;
if(!std::cin)
{
std::cout << "Bad input\n";
return 1;
}
int position = -1;
for(int i = 0; i < ARRAY_SIZE; ++i)
{
if(data[i] == number)
{
position = i;
break;
}
}
if(position < 0)
{
std::cout << "The number not found in the array\n";
}
else
{
std::cout << "The number is in the array at position " << position << "\n";
}
return 0;
}
Comments
Leave a comment