C/C++ :: Use Binary Search To Find A Number In The Array?

Mar 23, 2014

i'm trying to use binary search to find a number in the array but i dont know whats wrong with my code. When l enter a number which DOES exist in the array, everything is ok... but when i enter a number which does NOT exist in the array, i have problem...i cant exit the program, it just continues to run.Here is my code

int main()
{
int FirstPosition;

[Code]....

View 2 Replies


ADVERTISEMENT

C++ :: Binary Search Algorithm - Find Sum Of Used Number In Sequence

Mar 27, 2014

I have to made a programme which will search for given number and it must work in O(log(n)). The problem is that this programme beside finding this number have to find how many times this given number is used in this sequence.

Sequence is from lowest to highest only one possibility to use binary search algorithm

For example when we have squence
-1 -2 3 3 3 3 4 5 6 7 7 8 9 9 9 9 10

The numbers we need to search are
1 , 3 , 7 , 9 , 11 , 12

The answer is
0 , 4 , 2 , 4 , 0 , 0

So we need to find the sum of used number in sequence.

I have written algorithm Code: int start = 0;
int end = sequencelenght - 1;
int mid = 0;
/// Binary serach
while (start<=end) {
int mid=(start+end)/2;
if (sequence[mid]==givennumber) {

[Code] .....

As u see i search for given numer with binary with O(log(n)) but when i have to sum the duplicates the only good way i see is using loop to right and left but this have got log(n) specification (because when sequence would be for example 7 7 7 7 7 7 7 7 7 and given number to search will be 7 this will be O(n) loop).

How would look the most optimal algorithm look for this exercise? I mean O(log(n)) the fastest algorithm....

View 6 Replies View Related

C/C++ :: Find Non-key Item In Binary Search Tree

Apr 16, 2014

I am creating a menu-driven program which creates a small database using a binary search tree. I am having issues with the last method I need for the program. The way it is coded now only gives me blank data when I print it. Here is the exact wording for the requirements for this method:

"Output a complete report to the text file named BillionaireReports.txt in increasing order by billionaire rank, that contains all data fro each record stored in the BST. Note: this is not a simple traversal. You may assume that the rank is never less than 1 and never greater that 500. Do NOT copy the data into another BST, into an array, into a linked list, or into any other type of data structure. Retrieve the data directly from the binary search tree which is stored in order by name."

Here is the method in question:

void BillionaireDatabase::displayRankReport() {
int i = 1;
for(i; i <=500; i++) {
billionaireDatabaseBST.contains(Billionaire& anItem);

[Code] ....

I know how to sort the database by the key, but I'm not sure how to sort by something that is not the key.

Here are the functions I have available via the BST (these were written by the professor)

//------------------------------------------------------------
// Constructor and Destructor Section.
//------------------------------------------------------------
BinarySearchTree();
BinarySearchTree(const ItemType& rootItem);
BinarySearchTree(const BinarySearchTree<ItemType>& tree);
virtual ~BinarySearchTree();

[Code] ....

View 3 Replies View Related

C++ :: Binary Search Or Linear Search For 3D Array

Oct 7, 2014

inputting a search array. I tried putting a binary search but I can't get it to work. everything else works up until I put the value I am searching for in the array, then it just crashes.

How it suppose to work: input 2 coordinates with a value each then it calculates the distance between them then it suppose to let user search the coordinates for a value and state if found which coordinate it is at.

#include "stdafx.h"
#include <iostream>
#include <iomanip> //for setprecision
#include <math.h>

[Code].....

View 3 Replies View Related

C++ :: Number Guessing Game Binary Search

Sep 3, 2013

Basically i need to make a number guessing game where user thinks of a numbver from 1 - 100 and the machine will try to guess it in the least number of times. Once it guesses the number it will also say how many tries it took to guess.

My code so far is

#include<iostream>
using namespace std;
const int MAX = 100;
int main() {
char ch;

cout << "Think of an integer number between 0 and " << MAX<<endl;
cout << "Write it down on a piece of paper then hit a key to continue"<<endl<<endl;
cin.get(ch);

[Code] ....

View 2 Replies View Related

C/C++ :: Display Number Of Comparisons Using Binary Search

Apr 23, 2015

My assignment is "Search Benchmarks: Write a program that has a sorted array of at least 20 integers. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep count of the number of comparisons it makes. Display these values on the screen."

I'm unsure how to make it show the number of comparisons it takes to find the value for the binarySearch function. Also, where to put the selectionSort function the teacher said we need. This is what I have...

#include <iostream>
using namespace std;
int linearSearch(const int a[], int num);
int binarySearch(const int a[], int num);
void selectionSort(int a[]);

[Code] .....

View 1 Replies View Related

C/C++ :: Making Binary Search In Array?

Jul 15, 2014

i was trying to make a program that will asks the user for 10 numbers.then asks the user to enter integer search key.next,the program should find the value in element.i used linear search.my code is like this:

#include <iostream.h>
#include <conio.h>
int linearsearch(const int [], int, int);

[Code]...

how should i make a program that will uses binary search instead of linear search?

View 4 Replies View Related

C++ :: Convert Binary Search With Int Array To Use String Array

May 8, 2014

I'm having trouble converting this binary search with an int array to use a string array..

Code:

#include <iostream>
#include <string>
using namespace std;
// Function prototype
int binarySearch(string [], int);

[Code] .....

View 3 Replies View Related

C++ :: Binary Search Of String In Array Of Objects

Oct 20, 2013

I need to search for a string in an array of objects, this is what I have but it does not seem to work, it always gives me the second string in the array instead of the one that i search for.

void binarySearch(Student S[], string name) {
int first = 0;
int last = 9;
int middle;
int position = -1;
bool found = false;

[Code]...

View 3 Replies View Related

C++ :: Array Of 20 Integers - Binary Search Algorithm To Locate Same Value

Nov 28, 2014

Write a program that has an array of at least 20 integers. It should call a function that uses the linear search algorithm to locate one of the values. The function should keep a count of the number of comparisons it makes until it finds the value. The program then should call a function that uses the binary search algorithm to locate the same value. It should also keep count of the number of comparisons it makes. Display these values on the screen.

/*
search benchmark.cpp
this program searchs through a array of 20 integers
*/

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int array [20];
int count;

[Code] .....

View 1 Replies View Related

C++ :: Search And Find The Shortest Queue And Search After Some Condition?

Mar 7, 2013

I am trying to implement a Task scheduler where i have n number of tasks. The Idea behind my task scheduler is that in a loop of queues of a vector, task should get enqueued to the shortest queue among the loop of queues, which is done by the following code.

#include <vector>
#include <queue>
std::vector<std::queue<int> > q
int min_index = 0;
task t // implemented in the other part of the program

[Code] ....

Next i am trying to extend this paradigm to reduce the overhead time of the scheduler, Instead of searching the shortest queue every time, search after some condition ie. search the shortest queue after 5 tasks gets enqueued to the shortest queue.

i need to do something like this

#include <vector>
#include <queue>
std::vector<std::queue<int> > q
task t // implemented in the other part of the program
while(q[min_index].size()!=q[min_index].size()+5) // check whether current min_index queue's size is increased 5 more times if not goto enqueue

[code].....

View 1 Replies View Related

C++ :: Binary Search And Sequential Search Algorithm

Sep 16, 2013

Write a program to find the number of comparisons using the binary search and sequential search algorithms

//main.cpp
#include <cstdlib>
#include <iostream>
#include "orderedArrayListType.h"
using namespace std;
int main() {
cout << "." << endl;

[code]....

View 4 Replies View Related

C++ :: Binary Search Function - Return True If Element Inputted By User Found In Array And False If Not

Nov 9, 2014

I was instructed to write a binary search function which would return true if an element, inputted by the user, was found in the array, and false if it was not. I'm not sure why, but my function always returns false. My code is as follows.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//binary search function
bool search (int array[], int item)

[Code] ....

Why my code does not fulfill it's purpose???

View 7 Replies View Related

C/C++ :: Iterate Through Array And Search For A Number That Was Stored By User

Nov 23, 2014

So I have an issue with a homework assignment that I am coding. I am attempting to get a function to iterate through an array and search for a number that was stored in an array by the user. So far I can take the number, get the numbers displayed but in my menuChoice2 function, for some reason the program is not confirming whether or not the number is entered, and is only telling me that the number has not been found, instead of confirming that the number is in the array.

Here is my code thusfar:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// Variables that are Globally declared
int menuChoice = 0;
int usernum[1000] = { ' ' };

[Code] .....

To be clear I am not getting any errors but something is telling me that the error is in the formatting of menuChoice2.

View 14 Replies View Related

C++ :: Search User Input Number In Array Filled With Random Numbers

Nov 6, 2014

I need to create A program That makes a 12x10 Array Grid Filled With Random Numbers From 0-99.

Then I need To Allow The User To Input A Number Between 0-99 And Then The program will then search through the array and count how many of the users number there is inside the array.

Code:

#include <iostream>
using namespace std;
int main() {
int input;
int number;
int row=0;
int col=0;
int Array [12][10];

[Code] ....

View 1 Replies View Related

C++ :: Find Smallest Number In 2D Array

Oct 21, 2014

I need to generate a grid of 10x8 and fill it with random numbers (max 70), then i need to find the smallest number within the random numbers generated and my "findSmallest" function does not seem to work and i do not know how to make it work...

Here my Code:

include <iostream>
using namespace std;
int main() {
int row=0;
int col=0;
int ArrayGrid [9][11];
srand(time(NULL));

[Code] .....

View 4 Replies View Related

C++ :: Find Largest Prime Number K Of Array 1

Aug 30, 2013

While finding the primes , I do not know how to make it into one array so that...

View 7 Replies View Related

C/C++ :: Find Number Of 1 In Array For Error Detection

Mar 24, 2015

I'm receiving a char array consist of 8 characters, what I want to do is find the number of set bits in that array and then send it(total number) as a character to the transmitter so he can check, data is transmitted using serial port(RS232), where processing of data is done by DE0 FPGA.I've tried to use TestBit but the compiler doesn't know this function(I added #include "bit-manipulation.h" but it doesn't recognize this either) ...

-- the code of my project:

filed = open( "/dev/uart_0", O_RDWR );
if (filed < 0){
printf("the port can't be opened
");

[Code] .....

View 1 Replies View Related

C++ :: Array Of Hex Numbers Representing A Binary Number

Sep 24, 2013

So basically I have an array of hex numbers representing a binary number. Each binary number is 1/5th layer of the over all font.

For example... the letter A

........ B00000000 0x00
.****... B01111000 0x78
...*.*.. B00010100 0x14
...*..*. B00010010 0x12
...*.*.. B00010100 0x14
.****... B01111000 0x78
........ B00000000 0x00

As you can see each HEX number is a layer in the font which consists of in the above example 7 layers.

Now what I would like to do is create a C++ program, so I can visualize a HEX font array that I got off the internet.

#include <iostream>
using namespace std;
const char font[][5] = {
{0x00,0x00,0x00,0x00,0x00}, // 0x20 32
{0x00,0x00,0x6f,0x00,0x00}, // ! 0x21 33
{0x00,0x07,0x00,0x07,0x00}, // " 0x22 34
{0x14,0x7f,0x14,0x7f,0x14}, // # 0x23 35

[Code]...

Basically I am asking two things. How can I make this display the correct representing letter in the array when a user inputs his own text.

And secondly, how can I output the HEX numbers that is in the array as a binary number.

View 1 Replies View Related

C/C++ :: Find Smallest Number In Array Filled With Random Numbers

Oct 24, 2014

I need to find the smallest number in my 10x8 arraygrid with random numebr filled in it

Here my Code:

#include <iostream>
using namespace std;
int main() {
int total,average,smallest;
int row=0;
int col=0;

[Code] ....

View 2 Replies View Related

C++ :: Search For A Number When Vector Is In Order - Count How Many Times That Number Appears

Feb 6, 2014

In this program what i'm doing is to search for a number when the vector is in order, and count how many times that number appears, the problem is that count how many times the number does not appear, but when its appear the program stays in an "standby mode" and the cursor does not move.

int buscarNumOrdenado (int x [MAX]) //MAX is the define {
int num,d, LimiteInferior, LimiteSuperior,mitad;
d=0;
LimiteInferior = 0;
LimiteSuperior = MAX-1;

[Code] ....

View 2 Replies View Related

C++ :: Search Binary Tree

Aug 12, 2014

It has been a while since I built a binary tree from scratch so I decided to do it. Everything works fine but this one function. When I enter a number to search it just keeps running and allowing me to keep enter numbers.

Code:
void tree::search(int key,Node* leaf) {
if (leaf == NULL) {
std::cout<<"The tree is empty

[Code] ......

View 3 Replies View Related

C++ :: Binary Search Tree With Templates?

Oct 27, 2014

I am trying to implement BST using templates. This is the code that i have written.

Code: template <class T>
struct node
{
struct node *left;

[Code].....

There are no errors but the program hangs. What is the mistake with this program ?

View 2 Replies View Related

C :: Binary Search Program Using Graphics

Nov 22, 2013

Looking for the binary search program using c Graphics....

View 5 Replies View Related

C :: Binary Search With String Algorithm

Oct 3, 2013

I'm trying to use the biSearch function to search for a keyword in the dictionary.

Code:
int biSearch(Dict DictEntries[MAXENTRIES],int start, int finish,char *keyword) {
int mid = (start+finish)/2;
int index = strcmp(DictEntries[mid].key,keyword);
printf("%s=%s
",DictEntries[mid].key,keyword);

[Code] ....

View 4 Replies View Related

C++ :: Binary Search Tree Printing

Apr 19, 2014

I am not sure how to use the parameters ostream and enum type in a print function.

enum TraversalOrderType {preorder, inorder, postorder};
template <class ItemType>
void Tree<ItemType>::print(ostream & Outstream, TraversalOrderType order) {

[Code] ....

Is this the correct way to call the print function?

int main() {
Tree<int> tree1;
tree1.print(cout, preorder);
return 0;
}

View 2 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved