Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Prog ramming Assignment:Abstract Data Type Queue Application: Categorizing Data

ID: 3600930 • Letter: P

Question

Prog ramming Assignment:Abstract Data Type Queue Application: Categorizing Data Problem Specification Categorizing data: it is often necessacy to destroying their relative positional order. As a simple example consider a List of numbers that is maintaining their original elative positiona [ea r range daa wihout to be grouped into categories while l ordet Example Given the data set: 3 22 12 6 10 34 65 29 9 30 81 4 5 19 20 57 44 99 ategorize the data into four different group.s 1 to 9, 10 to 19, 20 to 29, and30 or mote The cesult is the categorized data: Categor l to 9 10 to 19 20 to 29 30 or more 12 10 19 22 29 20 34 65 30 8 57 44 99 ther a list categorized açcording The result is not a sorted list but ra to the specific requirements for the categories. group have kept their relative positional order. The numbers in each The underlying ADT for this application program is the ADT: Queue whose implementation has already been completed. Consequently, it should be possible to write this applica become involved with any "low level tion program without having to " data structure details. Prograiruning Notes This program should make use of the ARRAY IMPLEMENTATION of the ADT: queue This requires: struct QUEUE INFO RC i[30]; back; and the function prototypes / function definitions for create queueq) empty (qenque g, (q, i purge(q 2. For this application ion program, define INFO RC to be equivalent to the INFO data type int: typedef int INFO RC; Note this must be placed before the data structure declara QUEUE

Explanation / Answer

Given below is the C++ code for the question along with output. Please do rate the answer if it helped. Thank you.

#include <iostream>
#include <fstream>
using namespace std;
typedef int INFO_RC;
struct QUEUE{
INFO_RC i[30];
int back;
};

void create_queue(struct QUEUE &q);
int empty(struct QUEUE &q);
int enque(struct QUEUE& q, INFO_RC i);
void deque(struct QUEUE& q, INFO_RC& i);
void purge(struct QUEUE& q);
void form_queue(struct QUEUE& q);
void print_queue(ofstream &ofs, string name, struct QUEUE& q);

int main()
{
struct QUEUE data, q_1_9, q_10_19, q_20_29, q_30;
INFO_RC item;
string filename;
ofstream ofs;
  
form_queue(data);
create_queue(q_1_9);
create_queue(q_10_19);
create_queue(q_20_29);
create_queue(q_30);
while(!empty(data))
{
deque(data, item);
if(item < 10)
enque(q_1_9, item);
else if(item < 20)
enque(q_10_19, item);
else if(item < 30)
enque(q_20_29, item);
else
enque(q_30, item);
}
  
cout << "Enter output filename: ";
cin >> filename;
ofs.open(filename.c_str());
if(!ofs.is_open())
{
cout << "Could not open output file " << filename << endl;
return 1;
}
ofs << "Category" << endl;
ofs << "--------" << endl;
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 or more ", q_30);
ofs.close();
ofs << "Please check output file " << filename;
return 0;
}

void create_queue(struct QUEUE& q)
{
q.back = 0;
}

int empty(struct QUEUE& q)
{
return q.back == 0;
}

int enque(struct QUEUE& q, INFO_RC i)
{
if(q.back < 30) //check if there is space
{
q.i[q.back++] = i;
return 1;
}
else
{
return 0;
}
}

void deque(struct QUEUE& q, INFO_RC& i)
{
if(q.back > 0)
{
q.back--;
i = q.i[q.back];
}
}

void purge(struct QUEUE& q)
{
q.back = 0;
}


void form_queue(struct QUEUE& q)
{
int j, n ;
INFO_RC item;
create_queue(q);
cout << "How many items? " ;
cin >> n;
cout << "Enter the items: ";
for(j = 0; j < n; j++)
{
cin >> item;
enque(q, item);
}
}

void print_queue(ofstream &ofs, string name, struct QUEUE& q)
{
INFO_RC item;
ofs << name ;
while(!empty(q))
{
deque(q, item);
ofs << " " << item;
}
ofs << endl;
}

output

How many items? 18
Enter the items: 3 22 12 6 10 34 65 29 9 30 81 4 5 19 20 57 44 99
Enter output filename: categories.txt
Please check output file categories.txt

output file: categories.txt

Category

--------

1 to 9 3 6 9 4 5

10 to 19 12 10 19

20 to 29 22 29 20

30 or more 34 65 30 81 57 44 99

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote