Hi I need help with this c++ programming homework and this program is about the
ID: 3601311 • Letter: H
Question
Hi I need help with this c++ programming homework and this program is about the queue and also tho s project should make a use of the array implementation. Also all other instructions are on the attached photo please read that and don't copy and past from old work. please try to explane the code. thank you.
Explanation / Answer
#include <iostream>
#include<fstream>
using namespace std;
typedef int INFO_RC;
//create a queue structure
struct QUEUE {
INFO_RC i[30]; //array to store the queue element
int back1; //index to keep track of last element(rear)
};
typedef struct QUEUE q;
//initialise the queue
void create_queue(q& queue1)
{
queue1.i[0] = 0;
queue1.back1 = 0;
}
//Add elements from back of queue
void enque(q& queue1, INFO_RC item)
{
if(queue1.back1 == 30)
{
cout<<"queue is full";
}
else {
//insert the value at the end of the queue and increment the back;
queue1.i[queue1.back1] = item;
queue1.back1++;
}
}
//return the front element
int deque(q & queue, int front)
{
return queue.i[front];
}
//create queue of initial data set
void form_queue(q & data)
{
int no_item, item;
//initialise data queue
create_queue(data);
cout<<"Enter number of elements in the data set"<<endl;
cin>>no_item;
cout<<"Enter the data set"<<endl;
for(int i = 0; i < no_item; i++)
{
cin>>item;
enque(data, item);
}
}
//check if queue is empty
bool empty(q & queue1, int front)
{
//if front index is greater than or equal to back index queue is empty;
if(front >= queue1.back1)
return true;
return false;
}
//print the output to a file
void print_queue(ofstream & ofs, string name , q & queue)
{
ofs<<name<<":";
int index = 0;
//remove element from queue and write it to a file
while(!empty(queue,index))
{
int item = deque(queue,index);
ofs<<" "<<item;
index++;
}
ofs<<endl;
}
int main()
{
q data;
form_queue(data); //create a queue for data set
// Initialise the category queues
q q_1_9, q_10_19, q_20_29, q_30_more;
create_queue(q_1_9);
create_queue(q_10_19);
create_queue(q_20_29);
create_queue(q_30_more);
int index = 0; //index to keep track of front of the queue
while(!empty(data, index))
{
int item = deque(data, index);
if(item >= 1 && item <= 9)
enque(q_1_9, item);
if(item >= 10 && item <= 19)
enque(q_10_19, item);
if(item >= 20 && item <= 29)
enque(q_20_29, item);
else
enque(q_30_more, item);
index++;
}
// write the output to a file
ofstream ofs;
ofs.open("output.txt");
print_queue(ofs, "1 to 9", q_1_9);
print_queue(ofs, "10 to 19", q_10_19);
print_queue(ofs, "20 to 29", q_20_29);
print_queue(ofs, "30 to more", q_30_more);
ofs.close();
return 0;
}