Write a program that gets array elements as input from a user and then asks the user to input any value to search from the array. After that, if the value entered by the user is present in the array, it should display the index where that value is present. If the value is NOT present in the array, then it should print a message saying that the given value is not present in the array.
Sample Program:
cpp programming exercise with solution |
cpp programming exercise with solution |
#include <iostream>
using namespace std;
void ArrayElementSearch()
{
int sizeOfArray, searchingValue;
bool flage = false;
cout<<"Please enter the size of array: ";
cin>>sizeOfArray;
int *arr = new int[sizeOfArray];
for (int i = 0; i < sizeOfArray; i++)
{
cout<<"Enter the value number "<<i+1<<" ";
cin>>arr[i];
}
cout<<"enter the value which you want search from array: ";
cin>>searchingValue;
for (int i = 0; i < sizeOfArray; i++)
{
if (arr[i] == searchingValue)
{
cout<<searchingValue<<" is present at index: "<<i;
flage = true;
}
}
if (!flage)
{
cout<<searchingValue<<" is not present in the array.";
}
}
int main(){
ArrayElementSearch();
return 0;
}