FIFO queue demonstration

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
Last edited on
Actually, I realised I was being silly, I can simply use strings rather than integers...

So I've remade it to this:

main.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
#include <iostream>
#include <cstdlib>
#include "queue.h"
using namespace std;

int main()
{
    string choice;
    int length;

    cout << "What is the queues max length: " <<endl;
    cin >> length;
	queue q(length);

do{
    cout <<"\n";
    cout << "Please choose an option ";
    cout <<"\n";
    cin >> choice;

    if (choice== "+"){
        q.enqueue();
    }
    if (choice== "-"){
        q.dequeue();
    }
    /*if (choice== "*"){
        q.show();
    }*/
    if (choice== "$"){
        exit(EXIT_FAILURE);
    }

}while (choice!= "$");

    return 0;
}


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
25
26
#include <iostream>
#ifndef QUEUE_H
#define QUEUE_H
using namespace std;

class queue{

	int *arr;   	// array
	int capacity;   // max
	int front;  	// first in queue
	int rear;   	// last in queue
	int count;  	//current size

public:
    queue(int length);      //constructor

	void dequeue();
	enqueue();
	int size();
	bool isEmpty();
	bool isFull();
	string show();

};

#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
64
65
66
67
68
#include <iostream>
#include "queue.h"

// Constructor
queue::queue(int length){

	arr = new int[length];
	capacity = length;
	count = 0;
}

// Remove from first
void queue::dequeue(){

	// Checks if theres something to remove
	if (isEmpty())
	{
		cout << "Queue is empty";
		exit(EXIT_FAILURE);
	}

	cout << "Removing " << arr[1] << '\n'; // -------- This part doesnt work, it returns a random number rather than the value that was removed??

	front = (front + 1) % capacity;
	count--;
}

// Add to queue
queue::enqueue(){

	// Checks if full
	if (isFull())
	{
		cout << "Queue is full";
		exit(EXIT_FAILURE);
	}

	string coordinates;
    cout << "Please enter the coordinates (e.g. 4,5): ";
    cin >> coordinates;

	cout << "Entering " << coordinates<< '\n';

	rear = (rear + 1) % capacity;
	count++;
}

// Retunrs queue zise
int queue::size(){

	return count;
}
bool queue::isEmpty(){

	return (size() == 0);
}

// Checks if full
bool queue::isFull(){

	return (size() == capacity);
}


//Show method that shows the current aray
string queue::show(){

}


Can someone help with how i would use the 'show' method to print out the current queue array?
The queue elements have point coordinates i.e. each element has 2 numbers e.g. 3,4 . (with the comma from what I understand).
[…]
Im having trouble understanding how to allow the user to save two numbers with a comma in a signle array element

I’m afraid you can’t. Apart from the comma or the semicolon, inside the queue you can’t use a simple C-style int array to store those data.
Storing a C-style array of std::pair<int, int> could be a possible solution.

Also if you store the elements inside a C-style array, you don’t need such information as the ‘front’ ad the ‘rear’, since you can use int indexes to identify them.

…I realised … I can simply use strings rather than integers...

You can definitely read them from the user as std::strings, but, if the assignment says you need to store them as ‘numbers’, you’d better not store std::strings or you could get a bad mark.

front = (front + 1) % capacity;

Here it seems you want to create a circular queue… Do you?

Just to give some clarity, the assignment is to demonstrate how a FIFO works.

I think it would be better if you posted the entire assignment.
The 'assignment':


Explore FIFO queue operation. Develop a FIFO queue action demonstration program.
Realize the queue as an array or as a related list. Queue elements - x and y coordinates of a point (positive integers with a sign). // I suppose this part means that we have to use integers? But I think I can get away with using strings tbh

Create a queue object class.
• Create a constructor with the ability to specify the maximum length of a queue.

Write methods for working with queue:
• Push - puts items in a row;
• Pop - removes an item from the queue;
• Show - displays the queue as a series separated by a semicolon: {x, y}; {x, y}; ...

Demo program user interface (commands):
• + x, y [Enter] - insert the coordinates of a point in a queue;
• - [Enter] - remove an item from the queue and display it on the screen;
• * [Enter] - display the queue on the screen;
• $ [Enter] - exit the program;
• The first character you enter, other than [+ - * $], is an error message. If the text you enter is not a number, then an error message is displayed.

front = (front + 1) % capacity; This was made for a different assignment so it might have some remnants of the old code. This is useless now I believe.

Any suggestions on how anyone else would tackle this assignment? i.e. what kind of methods, or structures, or arrays you would use?
I simply thought I could adapt a basic queue code I found online to my needs...
Last edited on
I could adapt a basic queue code I found online

That’s a confession! :-D

Well, it seems your teacher proved cannier than you: s/he made things a bit more complicated :-)

Queue elements - x and y coordinates of a point (positive integers with a sign)

You need to store something like:
1
2
3
4
5
6
std::pair<int, int>
// or
struct Coord {
    int x;
    int y;
};


what kind of methods, or structures, or arrays you would use?
methods: your teacher already listed them for you:

1
2
3
4
5
6
class Queue {   // or whatever name you like
    Queue(int length);
    /* type? */ push ( /* parameters? */);
    /* type? */ pop ( /* parameters? */);
    /* type? */ show ( /* parameters? */) const;
};


arrays: question has already been answered.
(Anyway, if you can use std::array instead of C-style arrays, I think it would be better)
@Enoizat Im not that experiences with strucures, but I think I get the whole jist.
And this is what I have so far:

Main.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
#include <iostream>
#include <array>
#include "queue.h"

using namespace std;

int main()
{
    string choice;
    queue q;

    do{
    cout <<"\n";
    cout << "Please choose an option: ";
    cout <<"\n";
    cin >> choice;
    cout <<"\n";

    if (choice== "+"){
        q.push();
    }
    if (choice== "-"){
        q.pop();
    }
    if (izvele == "*"){
        q.show();
    }
    if (choice== "$"){
        exit(EXIT_FAILURE);
    }

}while (choice!= "$");

    return 0;
}


queue.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef QUEUE_H
#define QUEUE_H
#include <iostream>

class queue
{
    public:
        queue();
        virtual ~queue();
        void push();
        void pop();
        void show() const;

    private:
        int capacity;
};

#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
#include <iostream>
#include "queue.h"
#include <array>
using namespace std;

queue::queue(){
    int length;
    cout <<"Queue max length: ";
    cin >> length;
    cout <<"\n";

    int array[length];
    capacity = length;
}

queue::~queue()
{
    //dtor
}

void queue::push(){
     struct Coordinates{ //And this whole part wont work either cause I need to create a structure before I can enter data into it. 
//I assume I need to use a for loop in order to createa strcutre everytype the 'push' method is called in order to create a new structure every time?
            int x;
            int y;
        }arr[capacity];

for (i = 0; i<capacity; i++){ //Something like this to createa a struct for each array element?
 
}

    cout << "Please enter the desired values (x, y): ";
    cin >> Coordinates.x >> Coordinates.y;
    cout <<"\n" << "You entered: " <<Coordinates[1]; //This is obviously wrong, I dont actually get how I will print the structures that are saved in the array?

}

void queue::pop(){

}

void queue::show() const{

}


If i understand this correctly, the whole idea with the struct is to have two values for a single structure. And the amount of structures will be defined by an array that is created once the user enters a value. Please correct me if Im way off?

And please point out any other mistakes Im making, or if theres easier ways to do something.
Last edited on
1
2
3
4
struct Coordinates{
    int x;
    int y;
}arr[capacity];

That’s a start, but I’d declare that before, so that the array can become a property of the class.
For example:
1
2
3
4
5
6
7
8
9
10
11
12
struct Coordinates {
    int x {};
    int y {};
};

class queue {
public:
    queue();

private:
    Coordinates * arr;
};


or:
1
2
3
4
5
6
7
8
9
10
11
12
class queue {
public:
    struct Coordinates {
        int x {};
        int y {};
    };

    queue();

private:
    Coordinates * arr;
};

I feel like im taking 1 step forward, 2 steps back at this stage..

Soo I figured I could do 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
void queue::push(){
int x, y;
    if (isFull()){
        cout << "Queue is full.";
        return;
    }

struct Coordinates coord[capacity];
    int i;
    for (i = 0; i<capacity; i++){
        cout <<"Enter coordinate x: ";
        cin >> coord[i].x;
        cout <<"Enter coordinate y: ";
        cin >> coord[i].y;
        //return;   ---- If i dont use return, it also continues asking the coordinates until the full capacity is reached - this is not the goal, we only want to add to it if the user selects push method.
    }

    for (i=0; i<capacity; i++){
        cout << "Coordinates" << i +1 <<endl;
        cout << "Coordinates" << koord[i].x << ", " << koord[i].y <<endl;
    }
}


This would in essence ask to enter the two values for each structure based on the size of the queue that was entered.
However, this is contained within this method and isnt global, and therefore it doesnt make the check 'isFull' and doesnt save the entered data outside of this method.

How would I go about making sure the entered data is saved in the structure, so it can be accessed outside of this method? (I hope that makes sense)

I assume I would have to save the created coordinates to a seperate property of the class, but I have no idea how that would look
Last edited on
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <deque>

struct queue
{
    struct coordinates { int x ; int y ; };

    // Create a constructor with the ability to specify the maximum length of a queue.
    explicit queue( std::size_t max_size ) : max_sz(max_size) {}

    // Push - puts items in to the queue
    void push( coordinates coord )
    {
       if( q.size() < max_sz ) q.push_back(coord) ;
       else throw "queue is full" ;
    }

    // Pop - removes an item from the queue and return what was removed
    coordinates pop()
    {
        if( !q.empty() )
        {
            const coordinates f = q.front() ;
            q.pop_front() ;
            return f ;
        }
        else throw "queue is empty" ;
    }

    // Show - displays the queue as a series separated by a semicolon: {x, y}; {x, y}
    void show() const
    {
        for( coordinates c : q ) std::cout << '{' << c.x << ',' << c.y << "}; " ;
        std::cout << '\n' ;
    }

    private:
         std::size_t max_sz ;
         std::deque<coordinates> q ;
};

int main()
{
    std::size_t max_sz ;
    std::cout << "max size? " ;
    std::cin >> max_sz ;

    queue fifo(max_sz) ;

    char command ;
    do
    {
      std::cout << "\ncommands:\n----------\n"
                << "+ x, y [Enter] - insert the coordinates of a point in a queue\n"
                << "- [Enter] - remove an item from the queue and display it on the screen\n"
                << "* [Enter] - display the queue on the screen\n"
                << "$ [Enter] - exit the program\n"
                << "\nenter command: " ;

      std::cin >> command ;
      switch(command)
      {
        case '+' :
        {
          queue::coordinates c ;
          char comma ;
          if( std::cin >> c.x >> comma >> c.y && comma == ',' )
          {
              try
              {
                  fifo.push(c) ;
                  std::cout << "pushed {" << c.x << ',' << c.y << "}\n" ;
              }
              catch( const char* emsg ) { std::cout << "***error: " << emsg << '\n' ; }
          }

          else
          {
              // If the text you enter is not a number, then an error message is displayed.
              std::cout << "***error: coordinates: invalid input\n" ;
              std::cin.clear() ;
              std::cin.ignore( 1000, '\n' ) ;
          }

          break ;
        }

        case '-' :
        {
              try
              {
                  const queue::coordinates c = fifo.pop() ;
                  std::cout << "popped {" << c.x << ',' << c.y << "}\n" ;
              }
              catch( const char* emsg ) { std::cout << "***error: " << emsg << '\n' ; }

              break ;
        }

        case '*' :
            fifo.show() ;
            break ;

        case '$' :
            break ;

        default:
            // The first character you enter, other than [+ - * $], is an error message
            std::cout << "*** error: invalid command\n" ;
            std::cin.clear() ;
            std::cin.ignore( 1000, '\n' ) ;
      }
    }
    while( command != '$' ) ;
}
@JLBorges hah, thanks for this. I think I understand the whole process of it
Topic archived. No new replies allowed.