Write a program create a dynamic int array with size mentioned by user.
Ask user to enter a number to search.
apply linear search to find that element.
and in the end deallocate the memory allotted to that array.
#include <iostream>
#include <string>
using namespace std;
int main()
{
int* numbers;
int size;
int searcNumber;
//create a dynamic int array with size mentioned by user.
cout<<"Enter an array size: ";
cin>>size;
numbers=new int[size];
for(int i=0;i<size;i++){
cout<<"Enter a number "<<(i+1)<<": ";
cin>>numbers[i];
}
bool isFound=false;
//Ask user to enter a number to search.
cout<<"Enter a number to search: ";
cin>>searcNumber;
//apply linear search to find that element.
for(int i=0;i<size;i++){
if(numbers[i]==searcNumber){
isFound=true;
}
}
if(isFound){
cout<<"\nThe value "<<searcNumber<<" exists in the array.\n";
}else{
cout<<"\nThe value "<<searcNumber<<" DOES NOT exist in the array.\n";
}
//in the end deallocate the memory allotted to that array.
delete[] numbers;
system("pause");
return 0;
}
Comments
Leave a comment