Rr Pogcamming Assignment: Abstract Data Type - Queue Application Categorizing Da
ID: 3593816 • Letter: R
Question
Rr Pogcamming Assignment: Abstract Data Type - Queue Application Categorizing Data Problem Specitication Categorizing data: it is often necessary to reattange data without destroying their relative positional ordec. As a simple example, consider a list of numbers that is to be grouped into categories while maintaining thelr original relative positional order Example: Given the data set: 3 22 12 6 10 34 65 29 9 30 81 45 19 20 57 44 99 He want to categorize the data into four different groups: 1 to 9, 10 to 19. 20 to 29, and 30 oc hoce The result is the categorized data l to 9 10 to 19 20 to 29 30 or more 34 65 30 81 574 99 12 10 19 22 29 20 The result is not a sorted list but rather a list categorized according to the specific requirements for the categories. The numbers in each group have kept their relative positional order The underlying ADT for this application program is the ADTs Queue whose implenentation has already been completed. Consequently. it should be possible to write this application progtam without having to become involved with any "low level" data structure details. Programming Notes 1. This program should make use of the ARRAY IMPLEMENTATION of the ADT: queue This requires: struct QUEUE INEO RC i1301 int back: and the Eunction prototypes/ function detinitions for create queue(q)empty(q) enque(q. i deque tq.11 purge () data type int: typedef int INFO RC: Note: this must be placed befote the data structure declaration for 2. For this application program, define INFO RC to be equivalent to the QUEUE
Explanation / Answer
#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
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.