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


ADVERTISEMENT

C++ :: Increase Sizes Of Queue In A Vector Of Queues And Find Shortest Queue

Feb 20, 2013

I have a paradigm in a loop of queues of a vector,if a condition is true,increase sizes of the queue of that particular queue in the loop of queues, if condition is false, the queuesize is left as such in loop of queues. After this operation i need to search the queue sizes of all queues and enqueue in the shortest queue.

I want to do something like the code given below

#include <vector>
#include <queue>
int min_index = 0;
std::vector<std::queue<int> > q
std::size_t size = q.size();

[Code] ....

How to implement this logic?

will q[i].size=q[i].size+5 increase the queuesize by 5 for the ith queue?

View 12 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 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/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 View Related

C/C++ :: Linked List Search Function - Find Specified Value

Feb 9, 2014

I'm have troubles with this program that requires me to make a search through a Linked List and find a specified value. It also needs to be a template function. I've completed the rest of the program fine and everything runs ok except for the search function. Code below:

Linked.h
#ifndef LINKED_H
#define LINKED_H
#include<iostream>
template <class T>
class Linked {

[Code] ....

I have commented out my attempt at the search function. When uncommenting the function I get several errors about <template> being incorrect syntax.

View 8 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 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++ :: Use Find Function To Search For A Line In Text File

Oct 28, 2013

I'm trying to make a program that will search for a line in a text file using a non default delimitor at the start of the line. An example line in the text file would be as follows:

F Mary Smyth, 19, United Kingdom

I have been able to use the find function to search for and return the 'F' character but would like it to then display the whole corresponding line. Is this possible with the find function?

ifstream readFromFile("data.txt");
string Destinations[1] = {"F "};
string data;
while(!readFromFile.eof()){
getline(readFromFile,data);

[code]...

View 2 Replies View Related

C++ :: Dijkstra's Algorithm - Find Shortest Path In A Dense Graph

Apr 19, 2012

Code that finds a shortest path in a dense graph, using Dijkstra's algorithm in the simple array version. You write a function

struct listnode shortest path(int n, int s, int t, int *dist)
with
struct listnode f struct listnode * next; int vertexnumber; g ;

Being used to return the list of vertices on the shortest path. Your function has the following arguments:

- n: the number of vertices of the graph,
- s: the start vertex,
- t: the target vertex
- dist: the matrix of edgelengths.

The vertices are numbered from 0 to n -1, so s and t are numbers in that range. dist is a pointer to the n * n matrix of edgelengths; vertices which are not connected will be joined by an edge of length 9999. To access the array element dist[i][j], we can use *(dist + i*n + j). Your function should return the list of vertices on the shortest path from s to t, starting with s and ending with t.

View 4 Replies View Related

C++ :: Program To Find Shortest Path Between Vertices Using Matrix Power Multiplications

Feb 24, 2013

given matrix A find shortest path between vertices using matrix power multiplications untill u get nonzero values,use adjacency matrix

View 1 Replies View Related

C++ :: How To Search For Certain Value

Mar 28, 2013

I am having difficulties doing this program. i dont know how to search for the value 80.

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
class SortArray {

[Code] ....

View 1 Replies View Related

C++ :: How To Search For The Value 80

Mar 29, 2013

i am having difficulties doing this program. i dont know how to search for the value 80.

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
class SortArray {
public:
int randomArray[];
int find(int randomArray[], int len, int seek);

[Code]...

View 1 Replies View Related

C :: How To Do Search In Struct

Jul 9, 2014

So i create a simple structure,that has a firstname, lastname, age, dateofbirth.I would like it if i type what I am searching for print out there is one or not.But i have some trouble with equals(in array).. i type in the same "keresettnev" as "tanulok[i].Firstname" but it thinks not the same. (it works with age..)

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct student {
char *Firstname[40];
char *Lastname[40];
int age;

[Code] ....

View 12 Replies View Related

C++ :: Search For The Key Given A Content In Map?

Jan 7, 2014

In a C++ map, is there any way to search for the key given a content? Example:

I have this map:

map<int,string> myMap;
myMap[0] = "foo";

Is there any way that I can find the corresponding int, given the value "foo"?

cout << myMap.some_function("foo") <<endl;

Output: 0

View 3 Replies View Related

C++ :: How To Correct The Search In The End Of Program

Jun 1, 2013

Code:
#include<conio.h>#include<iostream.h>
class air_line {
private:
char name[5][100],name1;
long double opt,opt1,opt2,cnic[5],ticket_number[5],ticket_number1,cnic1;
public:
void input()

[code].....

View 1 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 :: Depth-first Search Of A Graph

May 18, 2013

You have to implement a data structure to represent graphs,directed or undirected,that tries to avoid the wasted space in the representation of a graph with adjacency matrix and the difficulty of searching the edges with adjacency list representation.We consider that the vertices are numbered from 1 to nverts and the exit degree of each vertex is at most MAXDEG. If deg[i] is the exit degree of the vertex i then the neighbors of the vertex i can be saved at the matrix edge[i][j], 1<=j<=deg[i].

Write a program that reads the datas from a file: if the graph is directed or undirected(1 or 0), the number of vertices (nverts),the number of edges (nedges) and the first and the last vertex of each edge.Write the function dfs that, with argument the data structure that you implemented before for the representation of a graph, prints the edges by the depth-first search of a graph. What I've done so far is: I wrote a program that reads these information from a file, calculates the exit degree of each vertex and creates the matrix edge[i][j]. What data structure do I have to implement???

View 1 Replies View Related

C :: Linked List Search

May 7, 2013

I read an article on linked list here: C Linked List Data Structure Explained with an Example C Program

Code:

struct test_struct* search_in_list(int val, struct test_struct **prev)
{
struct test_struct *ptr = head;
struct test_struct *tmp = NULL;
bool found = false;
}

[code].....

What is "if(prev)"? Wouldn't "prev" always have the same value? Secondly, if tmp is NULL (which will be the case when the loop if(ptr->val == val) finds a match the first time it is run), is *prev assigned a NULL?

View 1 Replies View Related

C :: Search File For String

Aug 22, 2014

I should state that I am recently new to C programming. I have dabbled with Python in the past, but nothing formal. I am taking a C programming class, but it is an introductory course. As such, the instructor is moving extremely slow through the material.

With this program, I am trying to search a given text file (line-by-line) for user-submitted data. I then want to be able to store which line that data was found on. The program runs, but it always results in "Sorry, couldn't find a match." Even if it shouldn't.

Code:

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
}

[code]...

I should probably explain my overall goal with this program.I would like to write a program that will calculate the molecular weight of a compound. I expect the user entered molecular compound to be something like this: CH3COOH (An equivalent entry could be C2H4O2)Also, it needs to be case sensitive because the chemical formulas are case sensitive.

The output will be "The molecular weight of CH3COOH is xxx.xxxxx grams".I have two text files as well. One contains a list of all chemical symbols, and the second is a list of all atomic weights in the same order as the first. I want to place the user inputted data into an array, then search for each element of that array in the chemical symbol text file. It will show which line it was found on, then I can use that line to grab the atomic weight from the second text file.

View 5 Replies View Related

C :: How To Search For Record And Deletes It

Apr 9, 2013

I am trying to get my program to search for the account ID in the structure and deletes the record as the account ID is found. The problem is that my codes are not working. (since my whole program is too long, I only show a part of it.) Here are my codes:

Code:

#include <stdio.h>
#include <string.h>
struct Account
{
char* Account_ID;
char* AccountOwner_FirstName;
char* AccountOwner_LastName;
}

[code]....

View 4 Replies View Related

C :: Depth First Search Implementation

Mar 12, 2013

I'm having problems with implementing depth first search.

Code:
6 10 //6vertices 10edges
0 2 //vertex and its adjacent vertex
1 0
1 2
2 3
2 4
3 1
4 1
4 3
4 5
5 3

Output to terminal: 0 2 3 1 4 5
but it should be: 0 2 4 5 3 1

Here's my code:

#include<stdio.h>
#include<assert.h>
/* maxVertices represents maximum number of vertices that can be present in the graph. */
#define maxVertices 100
void Dfs(int graph[][maxVertices], int *size, int presentVertex,int *visited)

[Code] ....

View 1 Replies View Related

C :: Search Chain In String

Mar 23, 2013

i have a work to do that consists on a given char pointer to a chain and a also given char pointer to a string. i have to search that chain in the string, and return the position where the beginning of the two it's equal. but only if they are equal.

example: string is "abcd" and the chain is "bc". want to find the chain, and if it exists, the position return will be 2. in the work it says that we should use sprintf() too. here is my code, which is incomplet yet.

Code:

char* pos(char* C,char* S){
char *str = S;
char *chain = C;
int i, j, temp;
int lengthStr = strlength(C);
int lengthChain = strlength(S);
}

[code]....

my doubt is how to use sprintf() and where. if i have to use malloc() and free(),

other examples:
pos(AGA,CTGCA)
return:
0

pos(AGA, ATAGATA)
return:
3

pos(AGA AGATAGA)
return
1 5

View 6 Replies View Related

C++ :: Cannot Search Words From Input

Apr 14, 2013

i am trying to create a c++ program that asks for an input and determines if it is a question or not by searching each word from the input across a database (txtfile)..so far I have managed to make it search the database for the word but it only works on one word.... i would like to be able for it to search every word on the input separatly..here is the code:

#include <cstdlib>
#include <cmath>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

[code]....

View 4 Replies View Related

C++ :: How To Search For An Item In The Array

Jun 29, 2014

If the item is in the array. Count how many times it is stored in the array and what are their respective array locations.

How can I add this output in my program.

Here's my code...

int array[15]={28,3,16,4,3,16,5,8,4,12,5,4,8,21,40};
int x=0;
int done=0;
int item;

cout<<"Input an item to be searched:";
cin>>item;

[Code] ....

This should be the output:

Item to be searched: 4
number of occurence : 3
array locations : 3 8 11

View 3 Replies View Related

C++ :: How To Search For A Substring In A File

Jan 12, 2013

How I can search a sub string in a file? Length of sub string is Variable and the number of search is indefinite.

View 15 Replies View Related







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