C++ :: Creating STL Queue Of Arrays?
Dec 3, 2013Is there any possible way to create an STL queue of arrays? I tried
queue<int[]>q;
It didn't work?
Is there any possible way to create an STL queue of arrays? I tried
queue<int[]>q;
It didn't work?
I'm trying to write a code that proves a queue like fifo (first in first out). I have four characters :
p(rint),e(nqueue),d(equeue) and q(uit).
The problem is when I press d a first character must get rid of, but not. When I press d the numbers get double.
Example input:
e 2 3 9 8 7
p
2 3 9 8 7
d
p
3 9 8 7
d
p
9 8 7
#include <stdio.h>
void enqueue(int queue[], int newnum, int *tail_p, int maxsize);
void deque(int queue[], int *tail_p, int *elem);
void printqueue(int queue[],int count);
[Code] ....
Well, basically, what I've been doing was creating a class that would implement the concept of Double Linked List, but that would behave like a queue ( the STL implementation is deque, or Double Ended Queue ).
The problem occured when I have been trying to generalize the class using templates. I mean, why use only integers ? Why not double or char or what not ?
Worked perfectly before including all the template stuff..
// Source.cpp <=> Main.cpp
#include <iostream>
#include "DList.h"
using namespace std;
int main(void) {
DList<int> *list;
list = new DList<int>();
[Code] .....
The errors returned by the compiler:
Error1error C2955: 'Node' : use of class template requires template argument listc:usersjumperdesktopc++ otherdouble linked listdouble linked listdlist.h6
Error2error C2955: 'Node' : use of class template requires template argument listc:usersjumperdesktopc++ otherdouble linked listdouble linked listdlist.h6
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 &'
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?
How do you prompt the user to enter the number of elements for the array and use that information to creatr a variable length array? And then how do you prompt the user to enter in a number for each element of the array and scan in the appropriate numbers? the numbers are double precision floating point.
for example,
Enter the numbe of elements in the array: 3
Enter element 0: 3
Enter element 1: -1
Enter element 2: 4
I know it starts with
int main() {
double N;
int a[size];
printf("Enter the number of elements in the array:" );
scanf("%f", &size);
//I'm pretty sure this is wrong
I'm working on a project where I have the user enter storm data about hurricanes for multiple seasons. I need to make separate arrays for:
1. the month, wind speed, and min. press.
2. the category of the storm
What I'm having trouble understanding is how to make the software take an input, assuming from a scanf, and put it in an array. Then taking that value and output-ing it back out when needed.
So here is piece of my code. Note: Disregard the commented sections of the code, my current project is building off a previous one, where in the last project we were using loops, now we are using arrays.
case 1:
{
if (year > 0) //begins counting
{
/*num_trop_storms = 0*/ num_hurricanes = 0, year = 0, wind_speed = 0,
hcat1 = 0, hcat2 = 0, hcat3 = 0, hcat4 = 0, hcat5 = 0, hrun = 1, totalhurr = 0;
knots = 0, cummwindspeed = 0.0;
[Code] ....
In the code above, how exactly the scanf takes the inputs from the user and then stores them for me to access. So, I want my array to be 3 columns, for months , wind_speed, & minpress. The array has a undetermined amount of rows. I think if I figure out the first array, I can handle the second.
We are supposed to create a menu with an option to add a car (make, model etc) to inventory. I have the structure format set up as far as adding the car, but I am stuck on how to make it a dynamic array of structures or whatnot (I'm not really sure on what I'm trying to do in the first place!) Basically, the user can choose to add a car and its info in. How do you set up the array to add whatever number of cars is needed? Will it be able to add more cars should the user come back to the program later?
This is the instruction of this portion of the project :
Menu Application
Add a car to inventory
Prompted to add
Make, Model and Year, Color, Miles, Price
Finish (adds the car to the file)
Cancel takes you to the main menu
I have an array of "2,3,4,5,6,9,10,11,12,99". I need to create a table of this which i have done using case
1. Find the maximum value of the array
2. Find the sum of the first and last element of the array
3. Swap the adjacent pairs of arrays
4. Display the values in array
5. Quit
Please enter choice:
but when i try running the program i did i keep having the break or continue pop up ....
#include <iostream>
using namespace std;
int main() {
int choice;
int number[10] = {2,3,4,5,6,9,10,11,12,99};
[Code] .....
I receive unexpected outcome in my program. Instead of getting all combinations of 0, 1, 2 (i.e. 000, 001, ..., 222) I get only 000 001 002 010 011 012. The idea of the progarm is to create a crystal lattice. Each atom of the lattice has 3 coordinates (x, y, z). That's why I create class Atom. Then I create 3-dim array of the type derived from class Atom. Now each element of the class will represent an atom.
Code:
#include <iostream>
using namespace std;
class Atom {
public:
float x, y, z;
[Code] .....
I'm supposed to create a "game" that is similar to Candy Crush or Bejeweled. The assignment takes in a .txt file that contains a matrix of values from 1-5 and then assigns each value to a spot in a [10][10] array. Then an Escape Code function prints out colored pixels in place of the number values for each spot, creating a "game board" looking output. Finally, the program is supposed to look for any matches of 3 same-colored pixels and replace them with a white pixel and "XX". Then the program prints the corrected game board with the matches X'd out.
I have it mostly coded, but I've encountered a couple of issues.
1.) I am supposed to label the columns and rows 0-9, and while I have no problem coding the labels for the columns using printf( ), when I try to print the row labels I get a random string of numbers.
2.) I replaced the matches with white pixels by reassigning the value in the array to 7, for which the ANSI Escape Code is white. However, I'm unsure about how to print the "XX" in the same spot.
Here is the input(.txt) file and the output of the program so far:
And here is what I have coded at this point:
Code:
#include <stdio.h>
void printEscapeCode(int c);
int main(void)
{
/* Declare an image array to be gameboard */
int gameboard[10][10];
/* Declare variables and load in how many rows and columns */
int Nrows;
Nrows = 0;
[code]....
So first I have to display a 2D array with all 0s, which is pretty easy.
#include <iostream>
using namespace std;
int main (){
int array[5][5];
for(int a=0; a<5; a++){
for(int b=0; b<5; b++){
array[a][b] = 0;
[Code] ....
So this displays
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
Next, this is where it gets confusing. I have to create a virtual creature by storing a letter into a random position in the array (the array can be up to 20x20 in size). Then make a function that searches the array for creatures, so it would search for that character. When it finds a creature, it should randomly decide to either move the creature to an adjacent position, or have it stay where it is. After, it should ask the user to create a new creature, or quit.
So how would I go about adding & modifying the current code to achieve what is listed above?
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] .....
I'm having a problem with removing an item from a queue. At first in the debugger I got SIGTRAP but I don't get it anymore but the problem still exists. When you try to remove an item from the queue the first nothing happens. Here's the code below compile it and you see what I'm talking about.
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
char let;
struct Node *nextNode;
};
[code]....
I have an assignment that needs to display the names of customers to be served according to a sequence. coding to display the names accordingly?
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAXIMUM 20
void create();
[Code].....
This is my output. I have trouble displaying the names of the customers as it outputs null when I try to display my position in queue.
Why is this code not marking that the queue is full.. I'm just including my Add function that verifies if they want to add another number to the queue. The isFull function works fine, have used it on other programs.
template <class T> //Template currSize function
int Queue<T> :: currSize ()
{
return (rear - front + arraylength) % arraylength; //
[Code].....
The output goes all the way down to 1 spot left, lets the user enter the last element. Then it asks if they want to add another.. At this point, when they hit Y, it lets them add it and it says there are 5 spots left!
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] .....
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); } };
I've been working on a little project and hit a snag. I'm using nodes for a queue and stack class that were created using an existing list node class. I create an object for a student class and I want to enqueue that object.
int main() {
Queue sLine;
Customer stu;
Queue<Student &>
cLine.enqueue(cust);
}
That's basically the coding of it in main. However when I follow the error which says uninitialized reference member ListNode<Student& info>::data;
#ifndef LISTND_H
#define LISTND_H
template< class T > class List;
template< class NODETYPE >
class ListNode {
friend class List< NODETYPE >;
[Code] .....
What I may have been doing wrong? Trying to work within certain contexts.
what is the empty state of queue?
View 5 Replies View RelatedWrite a program to simulate a printer queue (priority queue or heap – Bentley article has code) where 3 printers are “fed” by this queue. Each print job has an unchanging priority and higher priority jobs go first (although once started, a job isn't stopped to let a higher one go). For each unit of time, there is a 26% probability that a job will be added to the queue and all jobs take 8 time units to complete. Print job priorities are between 5 and 100. The queue opens 20 time units prior to the start of printing for adding print “jobs”. A print “day” is 1000 time units. The output from your program is:
1) A list of jobs completed with their priorities for each printer
2) A list of jobs not completed (if any).
#include <iostream>
#include <cstdio>
#include <cstring>
[Code].....
while the prog. running compiler say " access violation .. "
this my code ...
solved :just forget to make r=NULL ; rf =NUll ; frontm=NUll; frontf=NULL ;
#include <iostream>
#include<conio>
struct stu{
int id;
float gba ;
char gender ;
stu *next ;
}*r ,*rf ;
stu read()
[code]....
I have encountered errors when trying to display the customers' names in a priority queue.
#include <stdio.h>
#include <stdlib.h>
#define MAXIMUM 20
void create();
void priority_insert(int data);
void check(int data);
[code]....
I have gotten the following output:
Peter
Peter
<Null>
How do I correct my coding to display like below:
John
Peter
my code is already finished. im using parallel queues and im having problem in the queue customer name if i dont input space the code is fine but if i input space in the name it skips the bagcode and immediately jump to the number of bags what can i do to include the white spaces in the customer name and push it to the queue?
here's my code
#include <iostream>
#include <string>
#include <queue>
#include<conio.h>
using namespace std;
int main() {
queue<string> customer;
[code].....
Here is the assignment I get:
Write an email simulator that processes mail at an average of 40 messages per minute. As messages are received, they are placed in a queue.assume that the messages arrive at an average rate of 30 messages per minute.messages must arrive randomly.
Each minute, you can dequeue up to 40 messages and send them. Assume that 25% of the messages in the queue cannot be sent in any processing cycle.use a random number to determine whether a given message can be sent. If it can't be sent, enqueue it.
Run the simulation for 24 hours, At the end of the simulation, print the statistics that show:
-The total messages processed.
-The average arrival rate.
-The average number of messages sent per minute.
-The average number of messages in queue in a minute.
-The number of messages sent on the first attempt, the number sent on the second attempt, and so forth.
-The average number of times messages had to be requeued (do not include the messages sent the first time in this average)
Well, actually I've done a part of the coding. But how to continue it. And here is my code :
#include <iostream>
using namespace std;
#define SIZE 40
class Queue {
int queue[SIZE];
int head, tail;
public:
Queue();
void Enq(int num);
[code]....
Program requirements: Read two Queues (Male and Female) and pair them up.
Example:
inFile
M Bob
F Mary
outFile
Bob + Mary
Here is what I have so far. Code:
////////////////////////////main.cxx//////////////////////////
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include "queue.cxx"
using namespace std;
int main() {
ifstream inFile;
ofstream outFile;
[Code]....