### need help for this problem

1. Create an array of integers and initialize/assign random values to each element.
2. Allow the user to input an integer.
3. If the user input is present inside the array elements, display/print its index number, if not display/print "VALUE NOT FOUND"

please comment down below what code is needed for this thanks I'm new to programming and our prof already assigned us to program c++
Last edited on
 please comment down below what code is needed for this thanks

Please show what you have done so far, thanks.

You must have learnt something, or did you sleep through the lectures?

Please use code tags, when posting code.

Describe what research you have done.

Show any errors / warnings from the compiler verbatim.
using namespace std;
int main()
{
const int array_size = 10;
int num[array_size];
int searchKey, searchedIndex, i;
bool valueFound;

for ( i = 0; i < array_size; i++)
{
cout <<" Input an Integer: ";
cin >> num[i];

}
cout <<endl;

for ( i = 0; i < array_size; i++)
{
cout << num[i]<<endl;

}
cout <<endl;

cout <<" Enter the Value to be Searched : ";
cin >> searchKey;
cout <<endl;

valueFound = false;

for ( i = 0; ( i < array_size) && !valueFound; i++ )
{
if ( searchKey == num[i] )
{
searchedIndex = i;
valueFound = true;
}

}

if ( valueFound )
{
cout <<" USER VALUE INPUT FOUND ITS INDEX :: "<<searchedIndex <<endl<<endl;

}
else
{
}

return 0;
}
Why don't you follow the instructions?
The instructions said:
 1. Create an array of integers and initialize/assign random values to each element.

It didn't say that the user has to input the numbers.
You need to use std::rand for the random numbers.
https://en.cppreference.com/w/cpp/numeric/random/rand
Perhaps:

 ``12345678910111213141516171819202122232425262728293031323334`` ``````#include #include #include #include int main() { constexpr unsigned array_size {10}; constexpr unsigned nummin {1}; constexpr unsigned nummax {100}; std::mt19937 rng(std::random_device {}()); std::uniform_int_distribution distrib(nummin, nummax); unsigned num[array_size]; for (auto& n : num) n = distrib(rng); unsigned searchKey {}; do { std::cout << "Enter the Value to be Searched (" << nummin << " - " << nummax << " or 0 to exit): "; std::cin >> searchKey; if (searchKey) { const auto fnd {std::find(std::begin(num), std::end(num), searchKey)}; if (fnd != std::end(num)) std::cout << "USER VALUE INPUT FOUND ITS INDEX: " << std::distance(std::begin(num), fnd) << "\n\n"; else std::cout << "USER VALUE NOT FOUND\n\n"; } } while (searchKey); }``````

@mot,
 ``12345`` ``````for ( i = 0; i < array_size; i++) { cout <<" Input an Integer: "; cin >> num[i]; }``````

to
 ``12345`` ``````srand( time( 0 ) ); for ( i = 0; i < array_size; i++) { num[i] = rand() % 10; // say }``````

it will do what you want without too much verbiage.

 ``123`` ``````#include #include #include ``````