Hi. Here is code and output. However, I want to find specific number location without user input. That means, the specif

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

Hi. Here is code and output. However, I want to find specific number location without user input. That means, the specif

Post by answerhappygod »

Hi. Here is code and output.
However, I want to find specific number location without user
input.
That means, the specific number what I want to find for the
location has to be hardcoded.
can you change this code please?
thank you.
//bubbleSortFixed.cpp
#include <iostream>
#include <string>
using namespace std;
const int SIZE = 5;
int binarySearch(int a[], int first, int last, int
searchN)
{
{
int middle;
if (last >= first)
{
middle = (first + last) / 2;
if (a[middle] == searchN)
{
return middle + 1;
}
else if (a[middle] < searchN)
{
return binarySearch(a, middle + 1, last,
searchN);
}
else
{
return binarySearch (a, first, middle - 1,
searchN);
}
}
return -1;
}
}
int main()
{
int c, d, swap;
int array[SIZE] = {39, 19, -9, 22, 11};

cout<<"\nInput list is \n" ;
for(int i=0; i<SIZE; i++)
{
cout << array << '\t'
<<endl;
}

int searchNum, location =-1;
int numElements = 5;
for (c = 0; c < numElements - 1; c++)
{ for (d = 0; d < numElements - c - 1;
d++)
{ if (array[d] >
array[d + 1])
{ swap =
array[d];
array[d] =
array[d + 1];
array[d +
1] = swap;
}
}
}
cout << "Sorted list :" << endl;
for (c = 0; c < numElements; c++)
{
cout << array[c]
<< "\t";
}
cout<<" "<<endl;
cout<<"Enter the number that you would like to search:
";
cin>>searchNum;
location = binarySearch(array, 0, SIZE -1, searchNum
);
cout << endl;

if (location != -1)
{
cout << searchNum << " found in the
array at the location: " << location << endl;
}
else
{
cout << "Element not found" << endl;
}

return 0;
}

Hi Here Is Code And Output However I Want To Find Specific Number Location Without User Input That Means The Specif 1
Hi Here Is Code And Output However I Want To Find Specific Number Location Without User Input That Means The Specif 1 (14.55 KiB) Viewed 37 times
Input list is 39 19 -9 22 11 Sorted list : -9 11 19 22 39 Enter the number that you would like to search: 11 11 found in the array at the location: 2
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply