Home > Back-end >  C wrote dichotomy function return but no action?
C wrote dichotomy function return but no action?

Time:10-30

Below is the code, my goal is defined in the main function of a vector, then enter a value in the vector, and then use dichotomy implementation to find the value of position, but I wrote the return dichotomy function clearly mid; Still do not return values, run results following



Thank you so much for help.

#include
#include
using namespace std;

Int binary_find (int low, int, vector Array_v, int in)
{
Int mid;
While (low & lt;=high)
{

Mid=+ high (low)/2;
If (in & lt; Array_v (mids))
{
High=mid - 1;
cout <& lt; "Mid high=mid - 1=" & lt; & lt; Mid<" \n";
cout <& lt; "High:" & lt; & lt; High;
cout <& lt; "Low:" & lt; & lt; Low;
}
Else if (in & gt; Array_v (mids))
{
Low=mid + 1;
cout <& lt; "Low=+ 1 mid=" mid & lt; & lt; Mid<" \n";
cout <& lt; "High:" & lt; & lt; High;
cout <& lt; "Low:" & lt; & lt; Low;
}

} return mid;

}

Void array_print (const vector Array_v)
{
Int x=0;
for (; Ix & lt; Array_v. The size (); Ix++)
{
cout <& lt; Array_v [ix] <& lt; "";
}
}

Int main ()
{
An int array []=,32,45,64,621,820,830,840,900,999 {2};
Vector Array_v (array, the array + 10);
Array_print (array_v);
cout <& lt; "Both please input number \ n";
Int in;
cin> In;
Int out;
Out=binary_find (0, 9, array_v, in);
cout <& lt; "Sequence=" & lt; & lt; Out;
return 0;
}

CodePudding user response:

Will remove while judgement conditions is equal to, can run normally,

CodePudding user response:

Infinite loop.
Perform to the last step low=9, high=9, then both the if not into did not change
Also do not judge whether equal,
Don't quit the loop,
In addition to the code format stick in, keep the indentation code look better.

CodePudding user response:

Int binary_find (int low, int, vector Array_v, int in)
{
Int mid;
While (low & lt;=high)
{
Mid=+ high (low)/2;
If (in & lt; Array_v (mids))
{
High=mid - 1;
cout <& lt; "Mid high=mid - 1=" & lt; & lt; Mid & lt; & lt; "\ n";
cout <& lt; "High:" & lt; & lt; High;
cout <& lt; "Low:" & lt; & lt; Low;
}
Else if (in & gt; Array_v (mids))
{
Low=mid + 1;
cout <& lt; "Low=+ 1 mid=" mid & lt; & lt; Mid & lt; & lt; "\ n";
cout <& lt; "High:" & lt; & lt; High;
cout <& lt; "Low:" & lt; & lt; Low;
}
The else
{
return mid;
}
}

return mid;
}
  • Related