Answer to Question #183056 in C++ for ayush singh

Question #183056

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.


1
Expert's answer
2021-04-19T22:57:09-0400


#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;
}

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