Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Modifying Code for Priority Queue in Unified Computers & Programming, Spring 2006, Exercises of Aeronautical Engineering

A system problem for a unified computers & programming course in spring 2006. Students are required to modify and add sections to existing code for a priority queue. What a priority queue is, provides information on the files to be modified, and offers hints for implementation. The goal is to parse/read a file for information, store it in a priority queue, and dynamically draw the pointer manipulations.

Typology: Exercises

2011/2012

Uploaded on 07/22/2012

sehgal_98
sehgal_98 🇮🇳

4.8

(4)

137 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Unified Computers & Programming, Spring 2006
Page 2 of 6
III. Overview
This System Problem is a departure from the C&P work you have done in the past.
While you are typically asked to create the programs, this one will ask you to
modify and add sections to existing code. For this SPL you will be pulling
together many of the C&P lessons. Your program will parse/read a file for
information, store this information in a priority queue, and then dynamically draw
how the pointers are being manipulated in the queue to make this all possible. The
final result should be similar to “test_queue.exe”.
First, what is a priority queue? As you should know, a queue is a method of
dynamically storing information using pointers. By definition, a queue is
supposed to be FIFO (First In, First Out). A priority queue is a little different. You
give the element a value when it is Enqueued. Upon Dequeue, the element in the
queue with the smallest or largest associated value is returned. Our priority queue
will return the element with minimum associated value current in the queue.
In the two files you are expected to modify: priority_queue.adb and
test_queue.adb” you will find comments “--Fill in here as needed.” These
are locations that may need code. Since one of the skills tested in this SPL is code
reuse, read the comments in the given code or associated .ads file! You will
find that most of the .adb files are poorly commented, while the .ads files are
richly commented. This is typical of most programming languages, the
specification file or header file is has more explanations.
The procedures in “priority_queue.adb” that require modification are:
1. MakeEmpty
2. EnqueueSort the element into the queue using whatever sort you like.
3. DequeueNo Sorting, just return the head element
A few words on “priority_queue.adb
The record types that will form the nodes/elements of the queue have
already been defined and can be found in priority_queue.ads.
The general structure of the priority queue is depicted in Figure 1. The
Queue Type holds the basic information you need to know about the
Queue, the size and where its Head and Tail are. The Nodes form the
backbone of the Queue. Each Node has a pointer linking it to the next
Node (which should have a greater Value). Each Node also has a data
Element which gives the (X, Y) coordinates at which each Node should be
drawn in the window. The Tail pointer points to the last Node in the
Queue, which has no pointer to any other Node.
ToDo:
! Modify these procedures so that they act as described in
priority_queue.ads”. (Hint: Look at the “Linked_List” package
on the Unified C&P Website Extra Material, Lecture C5 for
implementation ideas).
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Modifying Code for Priority Queue in Unified Computers & Programming, Spring 2006 and more Exercises Aeronautical Engineering in PDF only on Docsity!

Page 2 of 6

III. Overview

This System Problem is a departure from the C&P work you have done in the past. While you are typically asked to create the programs, this one will ask you to modify and add sections to existing code. For this SPL you will be pulling together many of the C&P lessons. Your program will parse/read a file for information, store this information in a priority queue, and then dynamically draw how the pointers are being manipulated in the queue to make this all possible. The final result should be similar to “test_queue.exe”.

First, what is a priority queue? As you should know, a queue is a method of dynamically storing information using pointers. By definition, a queue is supposed to be FIFO (First In, First Out). A priority queue is a little different. You give the element a value when it is Enqueued. Upon Dequeue, the element in the queue with the smallest or largest associated value is returned. Our priority queue will return the element with minimum associated value current in the queue.

In the two files you are expected to modify: “priority_queue.adb” and “test_queue.adb” you will find comments “--Fill in here as needed.” These are locations that may need code. Since one of the skills tested in this SPL is code reuse, read the comments in the given code or associated .ads file! You will find that most of the .adb files are poorly commented, while the .ads files are richly commented. This is typical of most programming languages, the specification file or header file is has more explanations.

The procedures in “priority_queue.adb” that require modification are:

  1. MakeEmpty
  2. Enqueue – Sort the element into the queue using whatever sort you like.
  3. Dequeue – No Sorting , just return the head element

A few words on “priority_queue.adb” The record types that will form the nodes/elements of the queue have already been defined and can be found in “priority_queue.ads. ” The general structure of the priority queue is depicted in Figure 1. The Queue Type holds the basic information you need to know about the Queue, the size and where its Head and Tail are. The Nodes form the backbone of the Queue. Each Node has a pointer linking it to the next Node (which should have a greater Value). Each Node also has a data Element which gives the (X, Y) coordinates at which each Node should be drawn in the window. The Tail pointer points to the last Node in the Queue, which has no pointer to any other Node.

ToDo: ! Modify these procedures so that they act as described in “priority_queue.ads”. (Hint: Look at the “Linked_List” package on the Unified C&P Website Extra Material, Lecture C5 for implementation ideas).

Professor Kristina I. Lundqvist

Page 3 of 6

! Interleave into your code, procedures that graphically draw (and erase) the pointers as they are being manipulated. Look at the helper functions in “priority_queue” and graphical functions in “priority_queue_graphics” to make drawing much easier. Sections of your code that have been modified to have graphics will typically look like: EraseSomething(…);-- This is the added graphics Pointer1 := Pointer2 –- This is the important part DrawSomething(…); -- This is the added graphics

Figure 1: Structure of Priority Queue. Nodes should be linked from one to the Next starting with the minimum Value first.

The procedures in “test_queue.adb” that require modification are:

  1. Body of Test_Priority_Queue ToDo: You will need to look at “Test_Ada_Graphics.adb” to get an idea of what is missing. While most of the initialization needed to create a window is there, a few lines of file I/O and thread registering need to be done.
  2. Body of Draw_Queue ToDo: Add the code that allows this procedure to read from the file, create the element, and call Enqueue to put the element in the priority queue. The file you will be reading from is “Input.txt.” Note that Draw_Queue, when properly registered, will be called continuously in a loop this has an important impact on the code for File I/O.

Head Tail Size : Integer

Queue

Value : Float Element Next

Node

X : Integer Y : Integer

Element

Value : Float Element Next = Null

Node

X : Integer Y : Integer

Element

Value : Float Element Next

Node

X : Integer Y : Integer

Element

Professor Kristina I. Lundqvist

Page 5 of 6

Green Pointer – Pointer to the Head of the Queue Yellow Pointer – Pointer to the Tail of the Queue Orange Pointer – Pointer to the Newly Created Node/Element Red Pointer – Pointer using for sorting Purple Pointer – Pointer used for sorting Blue Circles – Graphical representation of a Node/Element of the Queue Green Links – The pointer from one Element to the Next in the Queue. Note that every time a pointer is given a new value, the old pointer is erased and the new one drawn. (The .exe has been known to not display correctly on some machines)

  1. Compare “test_ada_graphics.adb” and “test_queue.adb.” What are the similarities and differences? This should be enough to fill in the majority of the Test_Priority_Queue body.
  2. Double click on “input_generator.exe” to create a new “Input.txt” file. Open this file and get an idea of its layout. You will see numbers in groups of 3, a floating point number, followed by two integers. Each element/node of the priority queue will need to store the float number and use it as the value by which to sort. The remaining two numbers are the X and Y coordinates at which the graphical representation of that element should be drawn.
  3. Look at the Enqueue procedure in “priority_queue.ads,” what parameters does it require?
  4. Look over your notes on File I/O. Remember that manipulating a file has three phases: open, read or write, and close. How do you open a file? How do you close a file? It is one thing to get text from a file, but how do you get numbers from a file (Hint: Ada.Float_Text_IO)? How do you know when there is nothing left in a file to be read?
  5. We know that code is executed one line at a time. Where in the body of Test_Priority_Queue is procedure Draw_Queue called? You should now know enough to write the code that parses Input.txt.
  6. Write the rest of the Draw_Queue procedure. Make sure you call Priority_Queue_Enqueue.
  7. Look over “Linked_List.ads” and .adb. Do you understand how it works? Code the Prority_Queue procedures. Do NOT worry about the graphical part. Print text to the output to help you debug what your code is doing!
  8. Copy ALL of your code over to a new folder for safe keeping!
  9. Look over the ‘Helper Functions’ in “priority_queue.ads” and procedures in “priority_queue_graphics.ads.” Add the appropriate draw and erase functions to graphically draw the pointer manipulations.

Professor Kristina I. Lundqvist

Page 6 of 6

V. Grading Scheme (Out of 100)

10 - Complete Window Creation in “Test_Priority_Queue.” 10 - File Parsing in “Test_Priority_Queue” and “Draw_Queue.” 10 - Correct pointer manipulation in “MakeEmpty” procedure 25 - Correct pointer manipulation in “Enqueue” procedure 15 - Correct pointer manipulation in “Dequeue” procedure 5 - Working Graphics for “MakeEmpty” 10 - Working Graphics for “Enqueue” 5 - Working Graphics for “Dequeue” 10 – Paragraph or Pseudo Code on how the pointers are manipulated by Enqueue and how your graphics window relates (what are all the colorful pointers doing)?

VI. Turn In Instructions

1. Please zip and turn in ALL the. ads and .adb files you used in developing this

assignment via the on-line submission system (At least make sure “priority_queue.adb” and “test_queue.adb” are included).

  1. Name the zip file: “2006SPL5_LastName_FirstName.zip”
  2. Go to the class web site to submit the file. 4. Submit the paragraph you wrote in hardcopy.

Reminder: ! READ THE COMMENTS! ! “Linked_List.ads” and “Test_Ada_Graphics.adb” are good resources. ! Remember to check for a null pointer before using it