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
{
cout <<" USER VALUE NOT FOUND " <<endl<<endl;
}



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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <random>
#include <algorithm>
#include <iterator>

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<unsigned> 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,
If you change your lines
1
2
3
4
5
for ( i = 0; i < array_size; i++)
{
   cout <<" Input an Integer: ";
   cin >> num[i];
}

to
1
2
3
4
5
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.

You will need the headers
1
2
3
#include <iostream>
#include <ctime>
#include <cstdlib> 



Please use code tags.
Topic archived. No new replies allowed.