• Create Account

Calling all IT Pros from Canada and Australia.. we need your help! Support our site by taking a quick sponsored surveyand win a chance at a \$50 Amazon gift card. Click here to get started!

# Sid_TheBeginner

Member Since 23 Jun 2012
Offline Last Active Jul 01 2012 12:13 AM

### Binary Search Help Repost.

28 June 2012 - 11:40 PM

I've got the logic right and the algo seems to work fine. Now, that's epic for me But..

I want to add 2 more features to this code:

1) Exit on pressing 'x'. For now I'm using -1 to exit.
2) Print a message "Element not found" when an element not present in the array is searched for. Yeah, I'm not able to figure that out :|
Well, Here goes my code:

```[/size][/color][/font]

#include<iostream>
using namespace std;

int BinSearch(int data[], int numElements, int searchKey); // Prototype

int main()
{
int data[] = {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95};
int numElements = 23;  // Size of the Array is 23!!!
int searchKey;
int found;

for(int i = 0; i < numElements; i++)
cout << data[i] << ", ";
cout << endl;

for(;;)
{
cout << "Enter search key: (-1 to exit) "; // I want to quit when I press 'x'. Please let me know.
cin >> searchKey;

if(searchKey == -1)
{
cout << "Exiting\n";
break;
}

found = BinSearch(data, numElements,searchKey);

cout << searchKey << " is in position " << found << endl;
}

}

int BinSearch(int data[], int numElements, int searchKey)
{
int position, mid;
int beg = 0;
int end = numElements;

// Algorithm Begins.
for(int i = beg; i <= end;)
{
mid = (beg+end)/2;

if(data[mid] == searchKey)
{
return mid;
}
else if(data[mid] > searchKey)
{
end = mid - 1;
}
else if(data[mid] < searchKey)
{
beg = mid + 1;
}
}

}

[font=verdana, arial, helvetica, sans-serif][color=#000000][size=3]```

Thanks,
--Sid

### Binary Search Help Repost.

28 June 2012 - 11:39 PM

I've got the logic right and the algo seems to work fine. Now, that's epic for me But..

I want to add 2 more features to this code:

1) Exit on pressing 'x'. For now I'm using -1 to exit.
2) Print a message "Element not found" when an element not present in the array is searched for. Yeah, I'm not able to figure that out :|
Well, Here goes my code:

[code=auto:0]
#include<iostream>
using namespace std;

int BinSearch(int data[], int numElements, int searchKey); // Prototype

int main()
{
int data[] = {1, 4, 5, 6, 9, 14, 21, 23, 28, 31, 35, 42, 46, 50, 53, 57, 62, 63, 65, 74, 79, 89, 95};
int numElements = 23; // Size of the Array is 23!!!
int searchKey;
int found;

for(int i = 0; i < numElements; i++)
cout << data[i] << ", ";
cout << endl;

for(;;)
{
cout << "Enter search key: (-1 to exit) "; // I want to quit when I press 'x'. Please let me know.
cin >> searchKey;

if(searchKey == -1)
{
cout << "Exiting\n";
break;
}

found = BinSearch(data, numElements,searchKey);

cout << searchKey << " is in position " << found << endl;
}

}

int BinSearch(int data[], int numElements, int searchKey)
{
int position, mid;
int beg = 0;
int end = numElements;

// Algorithm Begins.
for(int i = beg; i <= end;)
{
mid = (beg+end)/2;

if(data[mid] == searchKey)
{
return mid;
}
else if(data[mid] > searchKey)
{
end = mid - 1;
}
else if(data[mid] < searchKey)
{
beg = mid + 1;
}
}

}
[code=auto:0]

Thanks,
--Sid

### Programming Language for Multiplayer Programming

25 June 2012 - 09:27 AM

Which Languages are used for specifically Multiplayer Programming in Games? I've basic knowledge of Computer Networks and would also be having Advanced Computer Networks soon and also "High Speed Network Architecture and Protocols, Neural Networks later" in my college..Will these subjects help me in learning Multiplayer Programming?
I'm interested in Networking altogether. So how does Networking come in games and which languages are used?
I'm new to programming and have begun with C++. Sorry for not narrowing my question. In short please enlighten me about multiplayer programming in games...
Thanks,
--Sid

### Breaking out of an infinite loop

24 June 2012 - 08:26 PM

Suppose i have a code like this:

for(;;)
{
int value;

cout << "Enter value(q to quit): ";
cin >> value;
}

I want to quit when q is entered. How do i do it? I'm yet not introduced to the ctype library in the book I'm following. So which are the other methods to do so?

Thanks,
--Sid

### An awesome website

24 June 2012 - 04:58 AM

This website is wonderful. If you're stuck with a Sorting algorithm or anything....

http://xoax.net/cpp/crs/console/index.php

Cheers!
--Sid

PARTNERS