|
|
This recursive function doesnt have a end point,becuase 12 item is not exist in the array.How we can fix this recursive function to solve this problem?
int _tmain(int argc, _TCHAR* argv[])
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int sum;
sum=BinSearch(arr,12,0,9);
cout<<sum;
return 0;
}
int BinSearch(int A[],int item,int low,int height)
{
int mid;
mid=(low+height)/2;
if(A[mid]==item)
return mid;
else if(item > A[mid])
return Binary_Search(A,item,mid+1,height);
else
return Binary_Search(A,item,low,mid-1);
}
|
|
|
|
|
Add as 1st line in BinSearch:
if(low > height)return -1; // Return 'not found' code
|
|
|
|
|
|
The array passed into the function could potentially have any value in it you can't select a simple return value to represent your error condition. I'd consider throwing an exception rather than using a return value:
if( A[low] > item || A[high] < item] ) throw binary_search_out_of_bounds();
Another option is to have the function return a std::pair<bool, int> where the first item in the pair tells the caller if they have an error condition or not and the second is the value they're after. Personally I find that a bit ugly and it complicates your code more as you have to do more in the calling function so I'd go with an exception.
Cheers,
Ash
|
|
|
|
|
|
Is it safe to send a POSITION variable throgh wParam (First parameter on a SendMessage )? Thank you.
|
|
|
|
|
It is safe to send. The POSITION type is a pointer that can be passed as WPARAM or LPARAM . But you must ensure that the object referenced by the pointer still exists when handling the message.
|
|
|
|
|
Of course that I'll ensure that POSITION variable send it through wParam is valid.
POSITION pos = (POSITION)wParam;
if(NULL != pos)
{
ItemData* pData = (ItemData*)m_ItemDataList.GetAt(pos);
if(NULL != pData)
{
m_ItemDataList.RemoveAt(pos);
delete pData;
}
}
Thank you for your attention.
|
|
|
|
|
good day all ..
i need only to get any voltage out from my laptop to control a relay
how to get a signal from a LAN port from any pin ?
in my laptop there is no com Port or lpt Port
also the usb port needs interfacing circuits which is not nessesary
in my simple project ... thats why i choosed the LAN Port
i use (visual basic 6) .. any help would much appriciated .
|
|
|
|
|
coolerfantasy wrote: i use (visual basic 6) So why are you posting in the C++ forum?
In any case you need to research how signals are presented on LAN ports, which is a hardware issue.
Veni, vidi, abiit domum
|
|
|
|
|
you are right
i will repost in the vb forum .
|
|
|
|
|
As I said before this is a hardware issue, not a programming one.
Veni, vidi, abiit domum
|
|
|
|
|
I wanted to verify if I am right on this question for college class.
#6. Write a single statement that assigns the values 99, 100 and 88 to an integer array named TestScores. Use the C++ notation used for the “lotterynumbers” array. (4 points)
I came up with...
int TestScores[5];
TestScores[0] = 99;
TestScores[1] = 100;
TestScores[2] = 88;
Correct me if I am wrong
Thank you.
|
|
|
|
|
That's not a single statement.
Veni, vidi, vici.
|
|
|
|
|
That are in effect 4 statements.
You might want to read this: Initializing Aggregates[^].
What you are looking for is:
int TestScores[5] = { 99, 100, 88 };
Note that the remaining elements of the array are initialized with zero, so that the following is the same as the previous statement.
int TestScores[5] = { 99, 100, 88, 0, 0 };
0100000101101110011001000111001010000010
|
|
|
|
|
Thank you! I understand what I did wrong... i needed a single statement, not a multiple statement.
Thanks again.
|
|
|
|
|
with int array, you can do the following idiom.
TestScores[5] = { 99, 100, 88 }
// [3],[4] are initialized with 0
with char array, in addition to the grammar above, you can
wchar_t[4] = L"jac" ;
|
|
|
|
|
jackheroes wrote: // [3],[4] are initialized with 0 Not necessarily.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
that's 4 statements not a single one it is going to look something like this.
int array[3] = {99,100,88};
|
|
|
|
|
Hi I need help with Set Theory. I am in a Programming class in college and I don’t understand this question at all! If you could help me, I would appreciate it.
Write three statements about sets. The first defines set A with values 1, 2, 3, 4 and 5. The second defines set B with the values 2, 4, 6, 8 and 10. The third lists the union of sets A and B. (Use the uppercase letter U to signify union.)
Thank you
|
|
|
|
|
This has nothing to do with C++, and is not even a programming question, it's just mathematics. Use Google to search for "set theory" and learn from there. Alternatively you could try your college library.
Veni, vidi, abiit domum
|
|
|
|
|
I think I figured it out.
A = {1,2,3,4,5}
B = {2,4,6,8,10}
A U B = {1,2,3,4,5,6,8,10}
Correct me if I'm wrong.
|
|
|
|
|
You are correct.
Veni, vidi, vici.
|
|
|
|
|
|