C/C++ :: Unable To Use Priority Queue Sorting In Reverse Order To Take Vector Or 2D Array
Aug 7, 2014
I'm trying to use a priority queue sorting in reverse order to take a vector or 2d array. The problem is that I want to sort by the vector/array cell value but keep the reference to the vector/array index of the value. I don't know quite howto keep them both related so when I pop. I can find the corresponding cell.
priority_queue<int, vector<int>, greater<int> > Open;
View 2 Replies
ADVERTISEMENT
Jan 27, 2013
So i have been banging my head against a wall with this problem for awhile. I have also copy and pasted direct examples from the internet but nothing seems to sort it right.
Node Header
Code:
#ifndef _NODE_H
#define _NODE_H
#include <stdio.h>
class Node {
public:
Node();
Node(int weight, char value, Node* left = NULL, Node* right = NULL);
[Code] ....
View 1 Replies
View Related
Mar 1, 2015
There appears to be some kind of error in by removeMin() function. Inserting items seems to work just fine but attempting to remove any items gives me the error "vector subscript out of range".
Here is my SortedPQ class... Below it you will find the quicksort implementation, in case looking at it is necessary.
template <class Type>
class SortedPQ {
private:
int front;
int rear;
vector<Type> V;
public:
SortedPQ(void) : front(-1), rear(-1), V(0){}
[Code] ....
View 1 Replies
View Related
Jul 15, 2013
I have two vectors of float that I need to sort in reverse order. reverse() seems to work for one of them, but not the other. I have toggled between sort() and reverse(). for one vector, the expected behavior is seen and the order reverses. For the other, there is no change in order.
This is very simple code, so it's hard to imagine what is going wrong.
Code:
vector<float> vec1, vec2;
vec1[0] = 14.1102; vec1[1] = 14.1145;
vec2[0] = 15.8508; vec2[1] = 26.0842;
sort( vec1.begin(), vec1.end() );
sort( vec2.begin(), vec2.end() );
[Code] ......
Printout is,
Code:
vector 1 sort
14.1102
14.1145
vector 2 sort
15.8508
26.0842
vector 1 reverse
14.1102
14.1145
vector 2 reverse
26.0842
15.8508
You can see that the order of the first vector did not change. Am I right in suspecting that the numbers are too similar for what ever method reverse() uses to determine the difference between values?
View 8 Replies
View Related
Jan 24, 2013
i am working on creating a priority queue, i seem to have done the algorithm for inserting from the top correctly but the algorithm for inserting from the bottom doesnt seem to work it just overwrites the existing data.
below is my code :
list is an array and listlength is the size of the array declared as a integer
void inserttop(string task) {
//int head = 0 ;
string temp ;
listlength++ ;
for (int i = 1; i < listlength; i++) {
[Code]...
View 2 Replies
View Related
Feb 27, 2015
This assignment is about Heap and PQ's to sort out jobs inside a printer. I'm far from finishing the assignment but the most important part isn't working. My issue is that nothing is getting stored inside the array. I keep getting crashes and at this point I'm not sure what to do. I notice that my destructor runs right after my "addJob" Function finishes, which is destroying the memory. Which might be why nothing gets stored inside OR I think my implementation of Heap/PQ is wrong.
Functions inside my test.cpp aren't properly done, they are made just to see if something is stored inside.
1. Check if I created the array correctly [PQtype.cpp / Heap.h/ PQType.h]
2. Am I even using/storing into the array. [Test.cpp "addJob" Function]
3. I'm also new to working with Class Templates.
PQType.h
template<class ItemType>
class PQType {
public:
PQType(int);
PQType(const PQType&); /
[Code] .....
View 4 Replies
View Related
Feb 17, 2015
I want to sort a vector int in ascending order, but when I test, the output isn't correct - the vector is still unsorted. Am I calling it incorrectly?
int sorted (vector <int> a) {
int size = a.size();
sort(a.begin(), a.end());
View 2 Replies
View Related
Oct 26, 2013
I know there is a function in algorithm class called sort() but I need to sort ignoring the case of the alphabet
Is there any function that does that?
View 2 Replies
View Related
Nov 28, 2014
How to build a FiFO queue without using the STL (done that no problem), get it to dequeue (again, done that no problem). However, to get those extra marks, I need to be able to order it using a priority system.
I've tried ordering the NodeDequeue class that I'll show at the bottom of this post, but I just cannot get it to order appropriately. The closest I have got is everything in order but I lose a Node from the memory completely. So, that's no good.
The most logical idea I have thought of right now is to send the largest number to the back of the queue each time it's iterated, eventually, the largest number will end up at the front.
class PriorityQueue : public Queue {
public:
Node* NodeDequeue(void) {
Node* tmp = front;
Node* seek = tmp->getPrev();
[Code] .....
View 3 Replies
View Related
Mar 4, 2014
Code:
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <math.h>
void reverseAr(char ar[], int n);
int main() {
int choice;
char *abc= ar[];
[Code]...
My codes keep couldn't get the reverse array of characters.
View 4 Replies
View Related
Jan 19, 2013
I'm trying to implement Prim's Algorithm and for that I need to have a decreaseKey method for a priority queue (to update the key value in a priority queue). Can I implement this in the STL Priority Queue?
This is the algorithm I'm following:
for each vertex u in graph G
set key of u to INFINITY
set parent of u to NIL
set key of source vertex to 0
en-queue to priority queue Q all vertices in graph
[Code] .....
View 1 Replies
View Related
Nov 4, 2013
I've got a priority queue of items who, from time to time, returns a different comparison result. Any way to re-sort a priority queue who is storing them?
Example:
#include <cstdlib>
struct Random {
bool operator<(const Random&) const { return !(rand()&1); } };
View 10 Replies
View Related
Jun 19, 2013
how double ended priority queue be used to implement external quick sort?
View 8 Replies
View Related
Oct 28, 2014
I know queue but with priority queue i am really amateur. How to implement some functions in priority queue.
template <class T> class PRIORITY_QUEUE {
private:
T *items; // array of queue items
int rear;
int maxSize; // maximum size of queue
[Code] ....
and what does heapify mean???
View 8 Replies
View Related
Jan 4, 2014
write a program, pfpq.c, that maintains a priority queue of music tracks (songs) and ratings. The struct is given by:
Code:
struct track
{
char artist[20];
char title[20];
int rating;
};
The program is only designed to demonstrate the concept so get the user to enter a small number of tracks and create a priority queue. (Use an empty string title or an empty string artist to end the program). Print the priority queue as it grows. Your program should initially ask the user if they wish to order the priority queue by rating in ascending order, or by rating in descending order, or if they wish to order the priority queue by artist name. Then build the priority queue accordingly. in the last case, "if they wish to order the priority queue by artist name." which is case 3. How do I sort the priority queue by artist name using in the following code.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pqueue.h"
struct track
{
char artist[20];
char title[20];
int rating;
}
[code]....
View 11 Replies
View Related
Aug 19, 2013
On several occasions in my project, I need to sort elements (indeces) based on their linked values. Those values are stored in an array. This means the comparison looks like this:
bool operator()(int i, int j) { return someArray[i] > someArray[j]; }
Because this form returns often but someArray may differ, I wrapped this in a template class:
template<class T>
struct sorter {
std::vector<T>& data;
sorter(std::vector<T>& __d) : data(__d) {}
bool operator()(int i, int j) const { return data[i] > data[j]; }
};
Now, I want to use this in a different way: I want to select the K indeces with the lowest value from someArray attached to it. My idea was to build a priority_queue, push the first K elements and then compare all the next elements to PQ.top(), as such:
INDEX t(0);
for (; t < someLimit; ++t) {
pq.push(t);
if (pq.size() == K) break;
}
for (; t < someLimit; ++t) {
if (someArray[t] < someArray[pq.top()]) { pq.pop(); pq.push(t); }
}
My problem, however, is the definition / initialization of the priority_queue object. My first idea was simply std::priority_queue<INDEX> pq(sorter<VALUE>(someArray));, but calling any function on pq provides the error "expression must have class type" (?) when hovering over pq.
My second guess, std::priority_queue<INDEX, std::vector<INDEX>, sorter<VALUE>(someArray)> pq;, provides the error 'expected a ')'' when hovering over someArray.
What is the correct way to initialize this data structure?
View 1 Replies
View Related
Jun 14, 2013
The program should store a character array in reverse order then display the reversed array. I have also included in the code that will display the actual characters into the array as it loops through. So I know the characters are being stored, but why doesn't it display the entire string when I call it?
Code:
#include<stdio.h>
int main(){
char str[50];
char rev[50];
printf("Enter Desired Text: ");
scanf ("%[^
[Code] ....
Is it just my compiler?
View 5 Replies
View Related
Dec 1, 2014
I need fastest method to reverse order of bytes in my char array.
For example i have:
unsigned char buf[8];
// consider data stored in buf is 88 77 66 55 44 33 22 11
// how to reverse it to: 11 22 33 44 55 66 77 88
// currently i can do it by equal assignment , i make another buf like:
unsigned char buf_ok[8];
[Code] ....
// This does reverse the bytes as i want but its very slow , i am looking for fast method ..
View 3 Replies
View Related
Dec 4, 2013
I've got 2 classes, Store and Transaction and I would like to create a priority queue of objects Transaction as a variable of Store class.
My store.h
#ifndef __STORE_H_INCLUDED__
#define __STORE_H_INCLUDED__
#include <queue>
using namespace std;
#include "Transaction.h"
struct TransactionCompare;
[Code] ....
The error im getting with this set up is
error C2664: 'bool TransactionCompare::operator ()(const Transaction &,const Transaction &)
const' : cannot convert parameter 1 from 'Transaction *' to 'const Transaction &'
View 6 Replies
View Related
Sep 13, 2013
I tried to make this code to output an array in Ascending Order but the output is showing weird output like 001fgc123 multiple times instead of the array.
What is wrong with my code?
#include <iostream>
using namespace std;
void ascOrder(int Array[],int length) {
int n=0,i,orderNum=0;
while(n<length) {
for(i=0;i<length;i++)
[Code]...
View 2 Replies
View Related
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
Jan 28, 2014
Using the array to accept 10 testscore. Calculate and print the highest, lowest, fifth test score entered and all the test score entered in reverse order.
How i would get it to print the highest,and lowest and in reverse order. I'm confused as to what to do...
View 4 Replies
View Related
Jan 4, 2014
This code prints 10 20 40 50 30.
#include “stdafx.h”
#include
#include
using namespace std;
int main() {
int anarray[5] = {40,10,50,30,20};
for (int iii=0 ; iii <= 4 ; iii++)
[Code] .....
View 1 Replies
View Related
Nov 25, 2014
I need to find the Mean, median, mode, and make a histogram of a 99 value array. How ever my sorting function is not sorting the array at all how can I fix this.
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <math.h>
#include <cctype>
[Code].....
View 8 Replies
View Related
Apr 14, 2014
I am trying to read information in from a file, each line of which contains a name in the form 'last, first middle'. I can successfully open the file and read it into an array that is roughly 2500 string in size. The problem comes when I try to sort the array into alphabetical order. I am using a sorting algorithm that we were instructed to use, found in our book. It is shown in the code. I would add the file that needs to be opened, but it is a few thousand lines long.
The error I am getting is:
Unhandled exception at 0x00A28628 in PeopleSearch.exe: 0xC0000005: Access violation reading location 0x00CC400C.
and I know it has to do with the sorting algorithm from using break points and running the program with that section commented out. I am stumped to why it is giving this error, as it seems that it is trying to access the array outside of the bounds of the array, but it shouldn't be
Here is my code:
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main() {
string NameArray[2500], filename; //declare array and file to open
[Code] ....
View 2 Replies
View Related
Nov 18, 2014
I'd like to modify this code so that there is 1000 numbers instead of 100, and that they're sorted in ascending order instead of descending order.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
double* sort_array(double* sort) {
for (int a=1; a<100; a++) {
[code]....
View 2 Replies
View Related