I need this program done in c++ It is a program about queues P roblem Specificat
ID: 3709594 • Letter: I
Question
I need this program done in c++
It is a program about queues
Explanation / Answer
COPY CODE HERE :
#include<stdio.h>
#include <stdlib.h>
//Rename int to INFO_RC
typedef int INFO_RC;
//Creates a structure QUEUE
struct QUEUE
{
INFO_RC i[30];
int back;
};//End of structure
//Rename struct QUEUE to QUEUE
typedef struct QUEUE QUEUE;
//Function to create a queue
void create_queue(QUEUE *q)
{
int x;
//Initialize the back to zero
q->back = 0;
//Initializes all elements of the array to zero
for(x = 0; x < 30; x++)
q->i[x] = 0;
}//End of function
//Function to delete all elements of the queue
void empty(QUEUE *q)
{
int x;
//Loops till end of the queue
for(x = 0; x < q->back; x++)
//Initializes to zero
q->i[x] = 0;
//Re set the back to zero
q->back = 0;
}//End of function
//Function to add an element to the queue
void enque(QUEUE *q, int i)
{
//Adds an element at back index position of the queue
q->i[q->back] = i;
//Increase the back by one
q->back++;
}//End of function
//Function to delete an element from the queue
void deque(QUEUE *q, int i)
{
//Set zero to the queue back position
q->i[q->back] = 0;
//Decrease the back by one
q->back--;
}//End of function
//Display the contents of the queue
void purge(QUEUE q)
{
int x;
//Loops till end of the queue
for(x = 0; x < q.back; x++)
printf("%d, ", q.i[x]);
}//End of function
//Function to write queue contents to the file
void print_queue(FILE *fp1, char *message, QUEUE q)
{
int x;
//Writes the category message
fputc(*message, fp1);
//Loops till end of the queue
for(x = 0; x < q.back; x++)
{
//Writes data to file
fputc(q.i[x], fp1);
}//End of for
//Close file
fclose(fp1);
}//End of function
//Main function definition
int main()
{
int x;
//Creates an array and initializes numbers
int arr[] = {3, 22, 12, 6, 10, 34, 65, 29, 9, 30, 81, 4, 5, 19, 20, 57, 44, 99};
//Creates queue objects for different category
QUEUE q_1_9, q_10_19, q_20_29, q_30_ab;
//Creates queues
create_queue(&q_1_9);
create_queue(&q_10_19);
create_queue(&q_20_29);
create_queue(&q_30_ab);
//Loops till end of the queue
for(x = 0; x < 18; x++)
{
//Checks the category of the number and adds it to appropriate queue
if(arr[x] <= 9)
enque(&q_1_9, arr[x]);
else if(arr[x] <= 19)
enque(&q_10_19, arr[x]);
else if(arr[x] <= 29)
enque(&q_20_29, arr[x]);
else
enque(&q_30_ab, arr[x]);
}//End of for
//Prints the contents of each category queue
printf(" Category 1 - 9 -> ");
purge(q_1_9);
printf(" Category 10 - 19 -> ");
purge(q_10_19);
printf(" Category 20 - 29 -> ");
purge(q_20_29);
printf(" Category 30 and above -> ");
purge(q_30_ab);
//Opens a file for writing
FILE *fp1 = fopen("Category.txt", "w");
//Checks file can be opened or not
if (fp1 == NULL)
{
puts("Could not open files");
exit(0);
}//End of if
//calls the function to write data to the file
print_queue(fp1, "1 to 9", q_1_9);
print_queue(fp1, "10 to 19", q_10_19);
print_queue(fp1, "20 to 29", q_20_29);
print_queue(fp1, "30 and above", q_30_ab);
}//End of function
Sample Run:
Category 1 - 9 -> 3, 6, 9, 4, 5,
Category 10 - 19 -> 12, 10, 19,
Category 20 - 29 -> 22, 29, 20,
Category 30 and above -> 34, 65, 30, 81, 57, 44, 99,
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.