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
ADVERTISEMENT
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
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
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
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
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
View Related
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
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
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
Feb 20, 2015
I need it to search a dynamic array which I build from an input file. When it finds the user-input string, I want it to store the line number, and continue searching, and record all lines that contain the user-input string.
Here is a link to my complete main.cpp, header file, and implementation file. The function I am having trouble with is "Bagger::lineSearch"
[URL] ....
View 2 Replies
View Related
Sep 30, 2013
How would you search in a vector array from a user input string?
ex: user input : "Hello"
output: search vector array and find the line that has the string "Hello" and output the array "Hello" is on?
View 1 Replies
View Related
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
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
Nov 22, 2013
Looking for the binary search program using c Graphics....
View 5 Replies
View Related
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
May 10, 2014
I don't know why, but my remove function doesn't seem to operate properly. The rest of my code is fine, so I am trying to pinpoint the exact location of my error. The else if statement remove(root->left, data) should've been called twice, but it only called once.
BST* smallestNode(BST* root)
// precondition: T is not null
// postcondition: return the node in the subtree rooted at T that
[Code]....
View 2 Replies
View Related
Nov 26, 2013
I am having an issue when i try to delete a node with 2 children it either doesn't delete anything, or wigs out in various manners deleting the wrong node or replacing a node with a various memory location. As follows, here is the delete function:
void BST::dele(){
bool found = false;//initialize a bool type to "find" the element to be deleted
if(root == NULL) return;//well if the tree's empty, nothing to be found right?
current = root;//set the current to the root to traverse the tree in search of the element
node* parent;//create a parent node for use once the node has been deleted
while(current != NULL){//traverse the tree
[Code] .....
View 1 Replies
View Related
Apr 29, 2013
Traversal of binary search tree. In my header file there is a function setTraversal (public) and private print file. As I understood from teacher's explanation, my setTraversal function should call the recursive print function and print the list depending on selected order (pre,in or post-order). I still cannot get my head around what should be in setTraversal function definition. All resources I read last two days explain each order separately (preorder, inorder, postorder). How can I combine them? Here is my code:
#include "NodeTypeBST.h"
#include <iostream>
enum TravType {PRE, IN, POST};
template<class T>
class BST
[Code] ....
View 1 Replies
View Related
Jul 9, 2013
I was working on binary tree implementation and came up with this implementation. optimize this code?
/*
Binary Search Tree(BST)
06/27/2013
*/
#include "iostream"
[Code].....
View 3 Replies
View Related
Mar 30, 2014
// ***This program uses a binary search and a linear search to see if a 3-digit lottery number matches the number on any of the player's tickets.***//
#include <iostream>
using namespace std;
[Code].....
bunch of errors and completely lost. what it's supposed to look like.
View 4 Replies
View Related
Mar 20, 2014
I'm working on a programming homework that asks us to implement all the functions of a Binary Search Tree using templated classes. I'm almost done with it but I'm sort of stuck with the last part which is the `search` function. For this part the homework asks for the following requirements
Quote
Node<T>* search(T value, Node<T>* subtree)
if the current node's value is the one we're search for, return a pointer to itif the current node's left and right subtree's are empty (both m_left and m_right are looking at nullptr) then return the nullptr to indicate we didn't find the valueif the value is less than the current node's value, return the search of the left subtreeif the value is greater than or equal to the current node's value, return the search of the right subtreeMake sure to only traverse a subtree if it's not null
View 1 Replies
View Related
Oct 13, 2014
I am creating a binary search program that lets the user input up to 10 integers and then the program displays the array and sorts it. When the user is prompted to select an integer, if the integer is in the array, the program responds with the array subscript part. I can get the loop to work once and maximum twice, but then it wont search for the array or say value not found even though the number in in the array. I tried making the values NULL but that only lets me go through it one more time.
Here is my code:
#include <iostream>
using namespace std;
//Functions
void printArray(int);
void selection(int);
int binarySearch(int,int,int);
[code]....
View 6 Replies
View Related
Feb 11, 2013
Fix this code? the result is Garpish>>
<iostream>
#include<fstream>
using namespace std;
class Student {
public:
char name[10];
char address[10];
[Code] ....
View 6 Replies
View Related
May 22, 2013
Standard example. I have a large text file and I wish to lex it into words. I tell the program that all words are delimited by ' ' ';' ':' and ''.
When I run the program it seems to be outputting the occurances of the letters and not the words. Im gobsmacked, I dont know what the hell is going on. Heres the function that lexes letters and not words. I want words dammit words!!
First youll see I define root node and point it to null; This forms the base of the BST. Then keep munching one character at a time until EOF reached. If the character is not a delimiter, assign it to "word" string, character by character. If it is a delimiter, take the so-far-constructed "word" and chuck it in the BST, then clear the word string through .clear().
Repeat until done. But its not working.
Code:
struct masternode* lexical_scanner(ifstream* inputfile) {
string word;
char c;
struct masternode* root = NULL;
while (c!=EOF){
[Code] .....
All the other functions in the source file are just fine, I've tested them in other apps and they are purpose built.
View 6 Replies
View Related
Apr 5, 2014
I'm playing with a guessing game program as a personal exercise, but I'm missing a vital piece - the binary search-style code.
"Have the program initially guess 50, and have it ask the user whether the guess is high, low, or correct. If, say, the guess is low, have the next guess be halfway between 50 and 100, that is, 75. If that guess is high, let the next guess be halfway between 75 and 50, and so on."
(We're assuming that the user won't cheat.) I need the average, essentially. As in, (50 + 75) / 2 = 63.. but when I use this method of "guess = (high+low)/2, it just keeps giving me 50. I can't remember what operators I should use to increment the program's response based on the user's input. It's literally a binary search, that needs to go where those TODOs are. If low was chosen, it would have to start by being at least 51, to 100, so I'd have to set that, then find the average.
Code:
#include <stdio.h> Code: #include <ctype.h>
int main(int argc, const char * argv[]) {
int low;
int high;
int guess;
int response;
int toupper ( int );
[Code] ....
View 2 Replies
View Related
Oct 24, 2013
I was working with binary search tree and came up with the solution:
Code:
#include<stdio.h>
#include<stdlib.h>
typedef struct data {
int x;
struct data *left;
struct data *right;
[Code] .....
View 6 Replies
View Related