Question: can you rewrite the selection _ sort portion without using max index, and use only my code for selection _ sort. correct my code please,

can you rewrite the selection_sort portion without using max index, and use only my code for selection_sort. correct my code please, while sticking to how i wrote it. I suspect my issue is with the sorting algorithm not working. Here is the code with purpose to randomly populate an array using unseeded rand , and sort the array in DESCENDING order, then have an output that says if key number exists or not.
#include iostream
using namespace std;
bool binary_search(int arrayx[],int size, int key);
void selection_sort(int arrayx[],int size);
void printarray(int arrayx[],int size);
int main()
{
int key1;
int key2;
const int size =40;
int arrayx[size];
cout <<"Enter key 1"<<endl;
cin >>key1;
printarray(arrayx,size);
selection_sort(arrayx, size);
binary_search(arrayx,size, key1);
cout <<"Enter key 2"<<endl;
cin >>key2;
binary_search(arrayx, size, key2);
printarray(arrayx, size);
cout <<printarray;
selection_sort(arrayx,size);
cout <<selection_sort;
}
void printarray(int arrayx[],int size){
for (int i =0; i <size; i++){
arrayx[i]=30+rand()%41;
cout <<arrayx[i]<<"";
}
}
void selection_sort(int arrayx[],int size)
{
int temp;
for (int i =0; i <size -1; i++)
{
for (int j =i+1; j <size-1; j++)
{
if (arrayx[j]<arrayx[i])
{
temp =arrayx[i];
arrayx[i]=arrayx[j];
arrayx[j]=temp;
}
}
}
}
bool binary_search(int arrayx[],int size, int key){
bool found =false;
int low =0;
int high =size -1;
int middle =(high +low +1)/2;
while ((low <=high)&& (found ==false))
{
if (arrayx[middle]==key){
found =true;
cout <<"
The key exists.";
}
else
{
if (arrayx[middle]>key)
{
low =middle +1;
}
else
{
high =middle -1;
}
}
middle =(high +low +1)/2;
}
return found;
if (found ==false){
cout <<"The key does not exist ";
}
else
{
cout <<"The key exists";
}
}
bool binary_search(int arrayx[],int size, int key){
bool found =false;
int low =0;
int high =size -1;
int middle =(high +low +1)/2;
while ((low <=high)&& (found ==false))
{
if (arrayx[middle]==key){found =true;
cout <<"
The key exists.";
}
else
{
if (arrayx[middle]>key)
{
low =middle +1;
}
else
{
high =middle -1;
}
}
middle =(high +low +1)/2;
}
return found;
if (found ==false){
cout <<"The key does not exist ";
}
else
{
cout <<"The key exists";
}
}
*Please disregard brackets and other small syntax errors because the copying messed with the code.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!