Public Group

This topic is 4997 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm working on my first Ada program, just a simple priority Queue, but I can't seem to get it working. Here's what I've got so far: (I put it in C Quotes just so that the post wouldn't be so large, seeing as how there's no ada quotes) Here's my PriorityQueue.abs file:
-- PriorityQueue.ads
-- Priority Queue Specification (Functions accessible by users)

package PriorityQueue is
function Push(Item, Priority : Integer) return Boolean;
function Pop(Garbage : Integer) return Integer;
end PriorityQueue;


This file compiles fine Here's my PriorityQueue.adb file:
-- PriorityQueue.adb
-- Priority Queue Implementation

package body PriorityQueue is
-- Holds a Number and its Priority
Type ItemType is
record
Number, Priority : Integer := -1;
end record;
type Item is array (1..100) of ItemType;

begin

-- Pushes an Item into the Priority Queue
function Push(Number, Priority : Integer) is      -- Line 15
Index, ItemSpot : Integer := 0;
begin
-- Make sure Priority is valid
if Priority < 0 then
Priority := 0;
end if;

-- Find where to insert the new Item
Index := 0;
loop
exit when Index = 101;                        -- Break out of loop if the Queue is full
exit when Priority <= Item(Index).Priority;   -- Break out of loop if we found where to insert Item
exit when Item(Index).Priority = -1;          -- Break out of loop if we found an empty spot
Index := Index + 1;
end loop;

if Index = 101 then
return false;
end if;

-- Save the spot to insert the new item in ItemSpot
ItemSpot := Index;

-- Move all Items above ItemSpot up one
-- Start from Index = 100 and go down to Index = 1, unless an "exit" is reached
for Index in 100..1 loop
exit when Index = ItemSpot;      -- Exit loop once we've reached ItemSpot
Item(Index) := Item(Index - 1);   -- Move Item up one spot in array
end loop;

-- Insert new Number and Priority
Item(ItemSpot).Number := Number;
Item(ItemSpot).Priority := Priority;

return true;

end Push;

function Pop(Garbage : Integer) return Integer is
Index, ItemNumber : Integer := 0;
begin

-- Save Number with the lowest priority number to be returned
ItemNumber := Item(0).Number;

-- Move all Items down one spot
for Index in 100..1 loop
Item(Index) := Item(Index-1);
end loop;

-- Return the Number
return ItemNumber;
end Pop;

end PriorityQueue;             -- Line 70


And here are the compile errors I get while compiling PriorityQueue.adb: priorityqueue.adb:15:04: declarations must come before begin priorityqueue.adb:70:01: statement expected And here is my main file First.adb:
-- Include text_io for input/output
with text_io, PriorityQueue;
use text_io, PriorityQueue;

-- Declare "Main"
procedure First is
Index : Integer := 0;
Queue : PriorityQueue;           -- Line 8
begin

Queue.Push(31, 3);             -- Line 11
Queue.Push(4, 4);
Queue.Push(0, 0);
Queue.Push(51, 5);
Queue.Push(52, 5);
Queue.Push(7, 7);
Queue.Push(1, 1);
Queue.Push(6, 6);
Queue.Push(2, 2);
Queue.Push(32, 3);

for Index in 1..10 loop
put(Queue.Pop(Index) + "is popped off the queue");
new_line;
end loop;
end First;


And here's the compile errors I get with First.adb: First.adb:8:09: subtype mark required in this context First.adb:8:09: found "PriorityQueue" in priorityqueue.adb First.adb:11:03: invalid prefix in selected component "Queue" - I get one of these for each of the .Push functions. From what I understand, the .abs is like the .h in C++, and the .adb is like the .cpp, is this right? Also, can you spot some of the mistakes in my code. Thanks. [Edited by - deadlydog on October 8, 2004 11:22:35 PM]

##### Share on other sites
It's doubtful. Ada was built for the US Government :D.

##### Share on other sites
Quote:
 Original post by MaegaIt's doubtful. Ada was built for the US Government :D.

And I don't think they even use it anymore?
Anyways the only thing I know about ada is that it came up when I was learning SQL but I don't remember what the connection was?

##### Share on other sites
they use it in (some) german universities to teach the basics of programming... but i doubt ada could be used to make a graphical game... is this homework or are you actually working on an ada game?

ps: i dont know ada :/

##### Share on other sites
It is homework, but I thought since I've got all the code done and the concept down that it would be alright, seeing as how I'm only having trouble with the syntax pretty much. I've been reading through quite a few ada tutorials and it seems like it actually is a pretty cool language. It's very tight so not many errors get through at all. Maybe Windows should consider using it to code their next installment. ha :)

##### Share on other sites
Quote:
 Original post by deadlydogIt is homework, but I thought since I've got all the code done and the concept down that it would be alright, seeing as how I'm only having trouble with the syntax pretty much. I've been reading through quite a few ada tutorials and it seems like it actually is a pretty cool language. It's very tight so not many errors get through at all. Maybe Windows should consider using it to code their next installment. ha :)

Ah Ada, brings back memories :D

The first file i can see that you have an extra begin at line 12, remove that. Can't spot the other errors yet (been too long), i'll dig up some very old ada code and compare to mine :D

*** EDIT: The extra begin should also get rid of the line 70 error

*** EDIT2: Oh I think i have it, you are trying to instantiate a package, which i believe acts like a 'namespace' in C/C++ so u can't do that. Create a structure for the priority queue and pass it into the functions.

something like...

TYPE PriorityQueue IS RECORD -- variables in here (array/list, etc)END RECORD;

HTH

##### Share on other sites
So will I need 2 files then, both the .ads and .adb, or just the .adb? Or can I simply just declare the record in my main file?

I tried switching the package to type - record like you said, and now I am getting this error on this line:

type PriorityQueue is record

Here's the error:

Any ideas?

Thanks for the help by the way.

##### Share on other sites
Keep the ADS and ADB files, it will be much cleaner this way (you can do it all in one file but dont)

Sorry, i didn't mean switch the package i meant add the type/record to the package which stores your array of types (move the type definition into the ads file also, might cause errors otherwise), and then declare your functions to take that record/type (Priority Queue type instance) and item to push and then return the same record out. Your functions will then operate on this record passed in and return it out again.

FUNCTION Push(PQ : PriorityQueueRecord, Item : Integer, Priority : Integer) RETURN PriorityQueueRecord;FUNCTION Pop(PQ : PriorityQueueRecord, Item : Integer) RETURN PriorityQueueRecord;

So you could do

PQ = Push(PQ, 10, 100);

on second thought u could also use procedures (no need to return out the type or you could do operator overloading so you could just do PQ-- to pop, i think u can call push too with operator overloading, not sure how to pass the params tho :D)

Well Hope That helped, brings back memories

##### Share on other sites
At the moment my problem seems to be in referencing my array of records. When I do this:

Item(Index).Priority := -1;

I'm getting this error:

invalid prefix command in selected component "Priority"

Am I not referencing the array right? Wrong type of brackets maybe. None of the tutorials I've been reading say how to access an array of records so I'm just guessing. Thanks again for all the help so far

• 11
• 18
• 12
• 22
• 9
• ### Forum Statistics

• Total Topics
631397
• Total Posts
2999822
×

## Important Information

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!