Hi I need help with this c++ programming homework and this program is about the
ID: 3601931 • Letter: H
Question
Hi I need help with this c++ programming homework and this program is about the queue and also this project should make a use of the array implementation. Also, all other instructions are in the attached photo please read that and don't copy and paste from old work. please try to explain the code. thank you.
*************************************************************************************************************************************************************************************************
Question
************************************************************************************************************************************************************************************************
Example: Given the data set: 3 22 12 6 10 34 65 29 9 30 81 5 19 20 57 44 99 We want to categorize the data into tour difterent groups 1 co 9,10 to 19.20 to 29.and 30 ormoce _ The result is the categorized data: CategosY 1 to 9 10to19 20 to 29 30 or more 12 10 19 22 29 20 34 65 30 81 57 44 99Explanation / 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);
}