Edit is Program so that the user can define the size of the array prior to enter
ID: 3671612 • Letter: E
Question
Edit is Program so that the user can define the size of the array prior to entering the values (dynamic array implementation). Once the array has been created and populated during runtime - allow the program to have a 4th menu option ( Choice 3: Pointer Search) - that will use a variation of the linear search function. The function will use an array pointer as the argument and display the index(es), and address(es) of the search value in the array. It should also notify the user if the value is not found.
Note - this program requires you to create a pointer to a dynamically created array. It also requires you to pass that pointer to your new search function and use pointer notation to locate your value in the array and to display the address. Don't just use regular array notation. You are expected to be able to demonstrate your understanding of pointers in this assigment ( see the output )
#include<iostream>
using namespace std;
void linearsearch(int a[], int num, int x);
int binarysearch(int a[], int num, int x);
int main()
{
int a[10], k, x, ch, ans, i;
x = 10;
cout << "Please Enter 10 number " << endl;
for (i = 0; i<x; i++){
cin >> a[i];
}
while (1){
cout << "Enter:" << endl;
cout << "1. For linear search" << endl;
cout << "2. For binary search" << endl;
cout << "0. to Exit" << endl;
cout << "Enter your choice" << endl;
cin >> ch;
switch (ch){
case 1:
cout << "Enter the search value" << endl;
cin >> k;
linearsearch(a, k, x);
break;
case 2:
cout << "Enter the search value" << endl;
cin >> k;
ans = binarysearch(a, k, x);
if (ans != -1)
cout << "the value " << k << " is found at position # " << ans << endl;
else
cout << "Element " << k << " not found " << endl;
break;
case 0:
return 0;
default:
cout << "Invalid entry " << endl;
}
}
}
void linearsearch(int a[10], int num, int x)
{
int i;
for (i = 0; i<x; i++){
if (num == a[i])
cout << "the value " << num<< " is found at position # " << i << endl;
}
cout << "Element " << num << " is not found" << endl;
}
int binarysearch(int a[10], int num, int x)
{
int high, low, mid;
low = 0;
high = x - 1;
while (low <= high){
mid = (low + high) / 2;
if (a[mid] == num)
return mid;
else if (a[mid]<num)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
output
program 4 output.pdf-Adobe Reader File Edit View Document Tools Window Help 125% Please enterthe size of your array 4 Please enter 4 integers Here is your array Choose 1: For linear search Choose For 1nary search Choose 3: For pointer search Choose to EXIT Enter the search value2 The value 2 was found at position 1 and address 00260C44 The value 2 was found at position 2 and address 00260C48 hoose 1: For linear hoose 2: For binary search hoose 3: For pointer search hoose 0 to EXIT search 8.50 x11.00 in 7:34 AM 2/25/2016Explanation / Answer
This below function will work to search an element in pointer search function written below.
See this pointersearch will read the size and elements will read and element which is need to be search also will read
and then find then position of an element in the array.
int pointersearch();
int pointersearch()
{
int size;
cout<<"Enter Array Size :"<<endl;
cin >> size;
int *array = new int[size];
cout << "Please Enter Elements :"<< endl;
for (i = 0; i<size; i++)
{
cin >> array[i];
}
cout<<"Enter Element Search :"<<endl;
int element;
cin >> element;
for (int i = 0; i<size; i++)
{
if (element == *(array+i))
cout << "the value " << element<< " is found at position # " << (array+i) << endl;
else
cout << "Element " << element << " is not found" << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.