Need Help with this Homework problem! Please show all your steps and the answer.
ID: 3600863 • Letter: N
Question
Need Help with this Homework problem! Please show all your steps and the answer. Thanks!
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 QUEUEExplanation / Answer
#include <iostream>
#include <fstream>
#define SIZE 100
using namespace std;
typedef int info_rc;
struct queue {
info_rc i[SIZE];
int back;
};
void create_queue(queue &);
bool empty(queue &);
void enque(queue &, info_rc &);
void deque(queue &, info_rc &);
void purge(queue &);
void form_queue(queue &);
bool openfile(fstream &);
void print_queue(fstream &, string, queue &);
int main () {
fstream outfile;
queue data;
queue q_1_9;
queue q_10_19;
queue q_20_29;
queue q_30_plus;
info_rc item;
if (!openfile(outfile))
cout << "Couldn't open file";
else {
form_queue(data);
create_queue(q_1_9);
create_queue(q_10_19);
create_queue(q_20_29);
create_queue(q_30_plus);
while(!empty(data)) {
deque(data, item);
if (item < 10)
enque(q_1_9, item);
if (item >= 10 && item < 20)
enque(q_10_19, item);
if (item >= 20 && item < 30)
enque(q_20_29, item);
if (item >= 30)
enque(q_30_plus, item);
}
}
/*
print_queue(outfile, q_1_9);
print_queue(outfile, q_10_19);
print_queue(outfile, q_20_29);
print_queue(outfile, q_30_plus);
*/
print_queue(outfile, "1 through 9: ", q_1_9);
print_queue(outfile, " 10 through 19: ", q_10_19);
print_queue(outfile, " 20 through 29: ", q_20_29);
print_queue(outfile, " 30 plus: ", q_30_plus);
system("pause");
return 0;
}
void create_queue(queue &q) {
q.back = -1;
}
bool empty (queue &q) {
return (q.back == -1);
}
void enque(queue &q, info_rc &item) {
++q.back;
q.i[q.back] = item;
}
void deque(queue &q, info_rc &item) {
int ct;
item = q.i[0];
for(ct = 1; ct <= q.back; ++ct)
q.i[ct-1] = q.i[ct];
--q.back;
}
void form_queue(queue &data) {
create_queue(data);
int num;
int value;
cout << "How many items are in the queue? ";
cin >> num;
for (int q = 0; q < num; q++) {
cout << "Please enter number " << q + 1 << ": ";
cin >> value;
enque(data, value);
}
}
void print_queue(fstream &outfile, string s, queue &q) {
int item;
outfile << s;
while(!empty(q)) {
deque(q, item);
outfile << item << " ";
}
}
/*
for(int c = 0; c < SIZE; c++) {
if(!empty(q))
outfile << q.i[c] << " ";
else
return;
--q.back;
}
*/
bool openfile(fstream &outfile) {
outfile.open("output.txt", ios::out);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.