Write a program that initializes an array of size 10 with 10 integer values between 1 and 100. The program then asks the user to enter an integer number between 1 and 100. The program will search the entered number in the array, if it is present the program displays its index in the array otherwise program displays “element not found” message.
#include <iostream>
#include <random>
#include <ctime>
using namespace std;
int main()
{
srand(time(0));
int A[10];
int search = 0;
for (int i = 0; i < 10; i++)
{
A[i] = rand() % 100 + 1;
}
cout << "Enter search number: ";
cin >> search;
for (int i = 0; i < 10; i++)
{
if (A[i] == search)
{
cout << "Elements in array are: ";
for (int j = 0; j < 10; j++)
{
cout << A[j] << " ";
}
cout << endl;
cout << "Index in array is: " << i << endl;
system("pause");
return 0;
}
}
cout << "Elements in array are: ";
for (int j = 0; j < 10; j++)
{
cout << A[j] << " ";
}
cout << endl;
cout << "Element not found." << endl;
system("pause");
return 0;
}
Comments
Leave a comment