Hi all,
Im having some trouble figuring out how to complete a assignment in C++ involving queue, array and FIFO. Hoping someone can point me in the right direction.
Just to give some clarity, the assignment is to demonstrate how a FIFO works.
The queue elements have point coordinates i.e. each element has 2 numbers e.g. 3,4 . (with the comma from what I understand).
And then you have to create a method that allows the user to enter (queue) two new numbers and adds it to the array queue.
Another (dequeue) method that removes the first element from the queue
And a final (show) method that displays the remaining elements in a {x, y}; {x, y}; ... fashion.
Im having trouble understanding how to allow the user to save two numbers with a comma in a signle array element
Right now I have somewhat of a start going. This is a queue system based on humans (single integer format)
Main:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#include <iostream>
#include <cstdlib>
#include "queue.h"
using namespace std;
#define HUMAN 1
int main(){
int size;
cout << "Please enter the queue size: " <<endl;
cin >> size;
queue q(size);
q.enqueue(HUMAN);
q.enqueue(HUMAN);
q.enqueue(HUMAN);
cout << "Maximum size of the queue is: " << size<<endl;
cout << "Queue size currently is: " <<q.size();
cout <<"\n";
q.dequeue();
cout << "Queue size currently is: " <<q.size();
return 0;
}
|
Then queue.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <iostream>
#ifndef QUEUE_H
#define QUEUE_H
using namespace std;
class queue{
int *arr; // array
int capacity; // max size
int front; // first element in the queue
int rear; // last element in queue
int count; // current size of the queue
public:
queue(int size); //constructor
void dequeue();
void enqueue(int x);
int size();
bool isEmpty();
bool isFull();
};
#endif // QUEUE_H
|
and queue.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include <iostream>
#include "queue.h"
// Konstruktors
queue::queue(int size){
arr = new int[size];
capacity = size;
front = 0;
rear = -1;
count = 0;
}
// Method to remove from the queue
void queue::dequeue(){
// Checks if queue is empty, if is, then terminates
if (isEmpty())
{
cout << "Queue is empty";
exit(EXIT_FAILURE);
}
cout << "Dequeing " << arr[front] << '\n';
front = (front + 1) % capacity;
count--;
}
// Method that adds to the queue
void queue::enqueue(int item){
// Checks if queue is full
if (isFull())
{
cout << "Queue is full";
exit(EXIT_FAILURE);
}
cout << "Entering" << item << '\n';
rear = (rear + 1) % capacity;
arr[rear] = item;
count++;
}
// returns queue size
int queue::size(){
return count;
}
bool queue::isEmpty(){
return (size() == 0);
}
// checks if queue is full
bool queue::isFull(){
return (size() == capacity);
}
|
When using the program, it should look something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
Please enter the size of the queue: 15
What would you like to do? + (positive being they have to enter two new numbers)
Please enter the coordinates to be saved: 5,4
What would you like to do? + (again)
Please enter the coordinates to be saved: 3,5
What would you like to do? * (star being show method)
(Show method) The current Queue is: {5,4}, {3,5};
What would you like to do? - (negative being dequeue)
(Show method) The current Queue is: {3,5};
What would you like to do? + (positive being they have to enter two new numbers)
Please enter the coordinates to be saved: 7,8
(Show method) The current Queue is: {3,5}; {7,8};
And so forth. I hope this explains the end results.
|
This is far from complete, and I will make the changes that will ask to enter the coordinate numbers at a later stage, once I understand how to save multiple integers with commas in a single array element.
Thank you for any help