Answer to Question #188566 in C++ for Asad

Question #188566

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.



1
Expert's answer
2021-05-03T17:51:56-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS