Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I will give thumbs up for doing the problem C++ programming language 1. a main program, which coordinates all other modules; 2. a module that

I will give thumbs up for doing the problem

C++ programming language

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

1. a main program, which coordinates all other modules; 2. a module that provides utility services including command line interpretation; 3. a module that implements the max-heap data structure (not all the functions yet); 4. a Makefile which compiles all modules and link them into the executable. ELEMENT is a data type that contains a field named key, which is of type int. Note that ELEMENT should not be of type int. HEAP is a data type that contains three fields named capacity (of type int), size (of type int), and H (an array of type ELEMENT with index ranging from 0 to capacity). The functions that you are required to implement are: Initialize(n) which creates an object of type HEAP with capacity n, size 0, and H points to a dynamically allocated array of n +1 pointers. It then returns a pointer to this object. This function requires you to perform dynamic memory allocation, given the demand n. printHeap(heap) which prints out the heap information, including capacity, size, and the key fields of the elements in the array with index going from 1 to size. The main program should react to each of the above command in the following way. S: On reading S, the program 1. Writes the following line to stdout: COMMAND: S where S is the character S. 2. Stops. C: On reading C n, the program 1. Writes the following line to stdout: COMMAND: Cn where C is the character C and n is replaced by the value of n. 2. Calls a function in the heap module to create a heap with capacity equal to n and size equal to 0, and return a pointer to this heap object to the caller. 3. Waits for the next command from stdin. R: On reading R, the program 1. Writes the following line to stdout: COMMAND: R where R is the character R. 2. Opens the file "HEAPinput.txt in read mode. If the file is not opened successfully, writes the following line to stdout: Error: cannot open file for reading and waits for the next command from stdin. 3. Reads in the first integer n from the file opened. If heap is NULL or heap->capacity is smaller than n, writes the following line to stdout: Error: heap overflow and waits for the next command from stdin. 4. Reads in the next n integers key, keya, keyn from the file, dynamically allocates memory for an ELEMENT, sets it key to key;, and let heap->H[j] points to this ELEMENT, for j = 1,2,...,n. 5. Waits for the next command from stdin. *** P: On reading P, the program 1. Writes the following line to stdout: COMMAND: P where P is the character P. 2. If heap is NULL, writes the following line to stdout: Error: heap is NULL and waits for the next command from stdin. 3. Writes the information of the heap pointed to by heap to stdout. Refer to the posted test cases for the output format. 4. Waits for the next command from stdin. W: On reading W, the program 1. Writes the following line to stdout: COMMAND: W where W is the character W. 2. Opens the file HEAPout.txt" in write mode. If the file is not opened successfully, writes the following line to stdout: Error: cannot open file for writing and waits for the next command from stdin. 3. If heap is NULL, writes the following line to stdout: Error: heap is NULL and waits for the next command from stdin. 4. Writes the information of the heap pointed to by heap to the file "HEAPoutput.txt. "HEAPoutput.txt" should have exactly the same format as HEAPinput.txt. 5. Waits for the next command from stdin. The file HEAP input.txt is a text file. The first line of the file contains an integer n, which indicates the number of array elements. The next n lines contain n integers, one integer per line. These integers are the key values of the n array elements, from the first element to the nth element. Refer to the posted test cases for the exact format of "HEAPinput.txt. As an aid, the following is a partial program for reading in the commands from the keyboard. You need to understand it and to expand it. typedef struct TAG_ELEMENT int key; }ELEMENT; typedef ELEMENT *ElementT; typedef struct TAG_HEAP{ int capacity; /* max size of the heap */ int size; /* current size of the heap / ElementT *H; /* pointer to pointers to elements */ }HEAP; #include "util.h" //=========== int nextCommand(int *n, int *f) { char c; while(1) { scanf("%c", &c); if (c == ' ' || c == '\t II c == ' ') { continue; } if (c == 'S'){ break; } if (c == 'C' || c == 'c'){ scanf("%d", n); break; } if (...){ } } return c; } //===== The following is a partial program that calls the above program. #include #include #include "util.h" int main() { // variables for the parser... char c; int i, v; while(1) { c = nextCommand(&n, &f); switch (c) { case 's': case 'S': printf("COMMAND: %c ", c); exit(0); case 'c': case 'C': printf ("COMMAND: %c %d ", c, n); heap - heapInit(n); break; case 'r': case 'R': printf ("COMMAND: %c ", c); ifile fopen("HEAP input.txt", "r"); if (!ifile) { } fscanf (ifile, "%d", &n); default: break; } } exit(0); } // The following is a partial Makefile. EXEC - run CC - g++ CFLAGS - -c-Wall # $ (EXEC) has the value of shell variable EXEC, which is run. # run depends on the files main.o util.o heap.o $(EXEC) main.o util.o heap.o # run is created by the command g++ -o run main.o util.o # note that the TAB before $(CC) is REQUIRED... $(CC) -0 $(EXEC) main.o util.o heap.o # main.o depends on the files main.h main.cpp main.o: main.h main.cpp # main.o is created by the command g++ -c -Wall main.cpp # note that the TAB before $(CC) is REQUIRED... $(CC) $ (CFLAGS) main.cpp util.o util.h util.cpp $(CC) $ (CFLAGS) util.cpp heap.o heap.h heap.cpp $(CC) $(CFLAGS) heap.cpp clean : rm *.0 1. a main program, which coordinates all other modules; 2. a module that provides utility services including command line interpretation; 3. a module that implements the max-heap data structure (not all the functions yet); 4. a Makefile which compiles all modules and link them into the executable. ELEMENT is a data type that contains a field named key, which is of type int. Note that ELEMENT should not be of type int. HEAP is a data type that contains three fields named capacity (of type int), size (of type int), and H (an array of type ELEMENT with index ranging from 0 to capacity). The functions that you are required to implement are: Initialize(n) which creates an object of type HEAP with capacity n, size 0, and H points to a dynamically allocated array of n +1 pointers. It then returns a pointer to this object. This function requires you to perform dynamic memory allocation, given the demand n. printHeap(heap) which prints out the heap information, including capacity, size, and the key fields of the elements in the array with index going from 1 to size. The main program should react to each of the above command in the following way. S: On reading S, the program 1. Writes the following line to stdout: COMMAND: S where S is the character S. 2. Stops. C: On reading C n, the program 1. Writes the following line to stdout: COMMAND: Cn where C is the character C and n is replaced by the value of n. 2. Calls a function in the heap module to create a heap with capacity equal to n and size equal to 0, and return a pointer to this heap object to the caller. 3. Waits for the next command from stdin. R: On reading R, the program 1. Writes the following line to stdout: COMMAND: R where R is the character R. 2. Opens the file "HEAPinput.txt in read mode. If the file is not opened successfully, writes the following line to stdout: Error: cannot open file for reading and waits for the next command from stdin. 3. Reads in the first integer n from the file opened. If heap is NULL or heap->capacity is smaller than n, writes the following line to stdout: Error: heap overflow and waits for the next command from stdin. 4. Reads in the next n integers key, keya, keyn from the file, dynamically allocates memory for an ELEMENT, sets it key to key;, and let heap->H[j] points to this ELEMENT, for j = 1,2,...,n. 5. Waits for the next command from stdin. *** P: On reading P, the program 1. Writes the following line to stdout: COMMAND: P where P is the character P. 2. If heap is NULL, writes the following line to stdout: Error: heap is NULL and waits for the next command from stdin. 3. Writes the information of the heap pointed to by heap to stdout. Refer to the posted test cases for the output format. 4. Waits for the next command from stdin. W: On reading W, the program 1. Writes the following line to stdout: COMMAND: W where W is the character W. 2. Opens the file HEAPout.txt" in write mode. If the file is not opened successfully, writes the following line to stdout: Error: cannot open file for writing and waits for the next command from stdin. 3. If heap is NULL, writes the following line to stdout: Error: heap is NULL and waits for the next command from stdin. 4. Writes the information of the heap pointed to by heap to the file "HEAPoutput.txt. "HEAPoutput.txt" should have exactly the same format as HEAPinput.txt. 5. Waits for the next command from stdin. The file HEAP input.txt is a text file. The first line of the file contains an integer n, which indicates the number of array elements. The next n lines contain n integers, one integer per line. These integers are the key values of the n array elements, from the first element to the nth element. Refer to the posted test cases for the exact format of "HEAPinput.txt. As an aid, the following is a partial program for reading in the commands from the keyboard. You need to understand it and to expand it. typedef struct TAG_ELEMENT int key; }ELEMENT; typedef ELEMENT *ElementT; typedef struct TAG_HEAP{ int capacity; /* max size of the heap */ int size; /* current size of the heap / ElementT *H; /* pointer to pointers to elements */ }HEAP; #include "util.h" //=========== int nextCommand(int *n, int *f) { char c; while(1) { scanf("%c", &c); if (c == ' ' || c == '\t II c == ' ') { continue; } if (c == 'S'){ break; } if (c == 'C' || c == 'c'){ scanf("%d", n); break; } if (...){ } } return c; } //===== The following is a partial program that calls the above program. #include #include #include "util.h" int main() { // variables for the parser... char c; int i, v; while(1) { c = nextCommand(&n, &f); switch (c) { case 's': case 'S': printf("COMMAND: %c ", c); exit(0); case 'c': case 'C': printf ("COMMAND: %c %d ", c, n); heap - heapInit(n); break; case 'r': case 'R': printf ("COMMAND: %c ", c); ifile fopen("HEAP input.txt", "r"); if (!ifile) { } fscanf (ifile, "%d", &n); default: break; } } exit(0); } // The following is a partial Makefile. EXEC - run CC - g++ CFLAGS - -c-Wall # $ (EXEC) has the value of shell variable EXEC, which is run. # run depends on the files main.o util.o heap.o $(EXEC) main.o util.o heap.o # run is created by the command g++ -o run main.o util.o # note that the TAB before $(CC) is REQUIRED... $(CC) -0 $(EXEC) main.o util.o heap.o # main.o depends on the files main.h main.cpp main.o: main.h main.cpp # main.o is created by the command g++ -c -Wall main.cpp # note that the TAB before $(CC) is REQUIRED... $(CC) $ (CFLAGS) main.cpp util.o util.h util.cpp $(CC) $ (CFLAGS) util.cpp heap.o heap.h heap.cpp $(CC) $(CFLAGS) heap.cpp clean : rm *.0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

1. Discuss the four components of language.

Answered: 1 week ago

Question

a. How many different groups were represented?

Answered: 1 week ago