Question: You are defining functions in main, your header file should only have function declarations, not code I need to separate this code C++ . #include
"You are defining functions in main, your header file should only have function declarations, not code"
I need to separate this code C++
.
#include
using namespace std;
//this menthod will take a input from the user //and validate whether the input is in range or not //if not in range enter again prompt int getInteger(int min,int max){ int value; while(true){ cin>>value; if(value>min && value //This will insert the element the element in sorted order in array void insert(int *array,int size,int value,int cap){ int i=0; for(i=cap-1;(i>=0 && *(array+i) > value);i--){ array[i+1]= array[i]; } array[i+1] = value; } //this will create a array of size given by user using //dynamic memory allocation int* createArray(int size){ //set to current time srand(time(0)); int min = -50; int max = 50; //range represent how many number we wants int range = (max - min); //this will track how many numbers are entered in the array int cap=0; //here we do dynamic memory allocation int *array = new int(size); //here we set the values of the array with max+1 for(int i=0;i //here we randomly insert value to the array for(int i=0;i //here we return the array return array; } //here we display the array //with only 5 value in one row void displayArray(int* array,int size){ int count=0; for(int i=0;i } //here we do binary search inside the array bool binSearch(int* array,int size,int value){ int s=0; int e=size-1; int mid = (s+e)/2; while(e>=s){ mid = (s+e)/2; if(*(array+mid)==value){ return true; }else if(*(array+mid)>value){ e = mid-1; }else{ s = mid+1; } } return false; } int main(){ //take entry of size of array cout<<"Enter size of the array: "; int size = getInteger(10,20); //create an array and fill with random values int *array = createArray(size); //here we display the array displayArray(array,size); //here we use for loop to get 3 values from the user to search in the array for(int i=0;i<3;i++){ cout<<"Enter value to Search: "; int value= getInteger(-50,50); string result = binSearch(array,size,value)==0?"False":"True"; cout< //here we take a new value from the user to search inside the array cout< delete(array); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
