Sign in to follow this  

Ada anyone?

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Quote:
Original post by Maega
It'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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by deadlydog
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 :)


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 this post


Link to post
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:
priorityqueue.adb:04:01: compilation unit expected


Any ideas?

Thanks for the help by the way.

Share this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites
Quote:
Original post by deadlydog
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


Your array declaration might be wrong, what i remember is we needed to do something like the following


Item: ARRAY(1..100) OF ItemType;


I'll dig through some stuff and see if i can find an array being used :D

Share this post


Link to post
Share on other sites
In the PriorityQueue.ads file where u declare the array, replace that line with the following lines

original line

type ItemsArray is array(1..100) of ItemHolder;

new lines

type tItemsArray is array(1..100) of ItemHolder;
ItemsArray : tItemsArray;


I think that should do it, only thing that i can see that can cause that error.

btw, np i'm quite enjoying looking at Ada again :D

Share this post


Link to post
Share on other sites
Yes you are correct, as much as i remember you can't instantiate a package (like a namespace in C/C++, or like a lib as you said).

You have 2 possible solutions for this, 1 is that you can just allow one priority queue for the whole program, to do this just change your calls from 'Queue.Push' to 'PriorityQueue.Push' (the name of your package is 'PriorityQueue'). Remove the delcaration at the top (Queue : PriorityQueue).

Solution 2 is to dump 'ItemsArray : tItemsArray;' into a record type of its own and then pass that into the functions Push and Pop (they will operate on the structure that you pass in and not on the global array). For this method you will need to instantiate the record and pass it into the functions, code will look something like.


-- after you declare the tItemsArray type (just put the array into a record)
TYPE PQ IS RECORD
ItemsArray : tItemsArray;
END RECORD;


and your functions will look like this


function Push(pq : PQ, Number, Priority : Integer) return Boolean;
function Pop(pq : PQ, Garbage : Integer) return Integer;


so instead of operating on 'ItemList' you now operate on 'pq.ItemList'

In the main program, replace the Queue : PriorityQueue declaration with Queue : PriorityQueue.PQ;. You can then call the functions as PriorityQueue.Push(Queue, ...);

Both methods will work, depends on what you need though (what was required for the assignment, i guess). I personally would go for method 2 but if you dont need it then you can very well go for method 1.


Hope that helped

Share this post


Link to post
Share on other sites
GAH!!! I'm so close. Thanks everyone for all the help so far. Now, everything compiles fine, but when I run it I get an error message. First off, here's all my files again - now they compile :)


PriorityQueue.ads

-- PriorityQueue.ads
-- Priority Queue Specification (Functions accessible by users)

package PriorityQueue is
-- Holds a Number and its Priority
type ItemHolder is record
Number, Priority : Integer := -1;
end record;

-- Array of Item Containers
type tItemsArray is array(1..100) of ItemHolder;

type PQArray is record
ItemsArray : tItemsArray;
end record;

procedure Push(PQ : in out PQArray; Number, Priority : in Integer);
procedure Pop(PQ : in out PQArray; Number : out Integer);
end PriorityQueue;





PriorityQueue.adb

-- PriorityQueue.adb
-- Priority Queue Implementation

package body PriorityQueue is

-- Pushes an Item into the Priority Queue
procedure Push(PQ : in out PQArray; Number, Priority : in Integer) is
Index, ItemSpot : Integer := 0;
NumPriority : Integer := Priority;
begin
-- Make sure Priority is valid
if NumPriority < 0 then
NumPriority := 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 NumPriority <= PQ.ItemsArray(Index).Priority; -- Line 20 - Causing the problem
-- Break out of loop if we found where to insert Item
exit when PQ.ItemsArray(Index).Priority = -1; -- Break out of loop if we found an empty spot
Index := Index + 1;
end loop;

-- 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 Counter in 100..1 loop
exit when Counter = ItemSpot; -- Exit loop once we've reached ItemSpot

-- Move Item up one spot in array
PQ.ItemsArray(Counter).Number := PQ.ItemsArray(Counter - 1).Number;
PQ.ItemsArray(Counter).Priority := PQ.ItemsArray(Counter - 1).Priority;
end loop;

-- Insert new Number and Priority
PQ.ItemsArray(ItemSpot).Number := Number;
PQ.ItemsArray(ItemSpot).Priority := NumPriority;

end Push;


-- Pops an item off the stack and returns the Number
procedure Pop(PQ : in out PQArray; Number : out Integer) is
begin

-- Save Number with the lowest priority number to be returned
Number := PQ.ItemsArray(1).Number;

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

end Pop;

end PriorityQueue;





Assingment3.adb

-- Assignment3.abd
-- Include text_io for input/output
with text_io, PriorityQueue;
use text_io, PriorityQueue;

-- Declare "Main"
procedure Assignment3 is
Num : Integer := 0;
PQ : PQArray;
begin

Push(PQ, 31, 3);
Push(PQ, 4, 4);
Push(PQ, 0, 0);
Push(PQ, 51, 5);
Push(PQ, 52, 5);
Push(PQ, 7, 7);
Push(PQ, 1, 1);
Push(PQ, 6, 6);
Push(PQ, 2, 2);
Push(PQ, 32, 3);

for Index in 1..10 loop
Pop(PQ, Num);
put(" is popped off the queue");
new_line;
end loop;
end Assignment3;




And here's the error I get when running the program:
raised CONTRAINT_ERROR : priorityqueue.adb:20

Any ideas? I can't see why that line would cause a problem. Thanks again for all the help everyone

Share this post


Link to post
Share on other sites
Quote:
Original post by deadlydog
GAH!!! I'm so close. Thanks everyone for all the help so far. Now, everything compiles fine, but when I run it I get an error message. First off, here's all my files again - now they compile :)


PriorityQueue.ads
*** Source Snippet Removed ***


PriorityQueue.adb
*** Source Snippet Removed ***


Assingment3.adb
*** Source Snippet Removed ***

And here's the error I get when running the program:
raised CONTRAINT_ERROR : priorityqueue.adb:20

Any ideas? I can't see why that line would cause a problem. Thanks again for all the help everyone



Index out of range probably, change it to 100. I believe the array runs from 0 to 99

Share this post


Link to post
Share on other sites
Blaaaaaaaa. All the hard stuff is out of the way, and I thought I was done, but there's one more thing. How do I output (print) a variable. Basically I just want to print the values that were popped out of my priority queue. So lets say Num is an integer. How do I display it?

put(Num); doesn't work

I'm assuming it's like any other language in that I need to first convert the integer to a character before printing it - like itoa() in C++. This is the very last thing then I'm done. Can anyone help me out one last time. Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by deadlydog
Blaaaaaaaa. All the hard stuff is out of the way, and I thought I was done, but there's one more thing. How do I output (print) a variable. Basically I just want to print the values that were popped out of my priority queue. So lets say Num is an integer. How do I display it?

put(Num); doesn't work

I'm assuming it's like any other language in that I need to first convert the integer to a character before printing it - like itoa() in C++. This is the very last thing then I'm done. Can anyone help me out one last time. Thanks


Since this is homework, dont u have some kinda book with the class? Since this is assignment 3 didn't u have the first 2 assignments that covered that?

the answer anyways:

Ada.Float_Text_Io.Put()
Ada.Integer_Text_Io.Put()

Share this post


Link to post
Share on other sites
Yup, it is homework, but no it isn't covered in class or the text. It's CS 350, which is learning different languages. So each assignment is a relatively easy project, but we have to do it in a language we've never used before. The text talks about the pros/cons of the languages, but no specifics like syntax or anything. We also don't cover it in class. We're basically told to just look up tutorials online (which I've done) and do it ourselves. I just couldn't find the answer to the above problem in any of the tutorials I read. Thanks for the answer. I'll let you know if it works or not once I get home and try it.

Share this post


Link to post
Share on other sites
OFFTOPIC: read at your discretion...

Quote:
Original post by deadlydog
We're basically told to just look up tutorials online (which I've done) and do it ourselves.


You would have to have one of the slackest lecturers I have ever heard of. Basically, you're doing a subject where someone devises a problem and then says go away and solve it and offers no guidance as to how to solve it. Why bother paying University fees for that!???

Timkin

Share this post


Link to post
Share on other sites
Quote:
Original post by Timkin
OFFTOPIC: read at your discretion...

Quote:
Original post by deadlydog
We're basically told to just look up tutorials online (which I've done) and do it ourselves.


You would have to have one of the slackest lecturers I have ever heard of. Basically, you're doing a subject where someone devises a problem and then says go away and solve it and offers no guidance as to how to solve it. Why bother paying University fees for that!???

ONTOPIC: btw, Ada is still used in the aerospace industry... mostly because they're slow to update! ;)

Timkin

Share this post


Link to post
Share on other sites
Yeah, the prof is actually just a graduate student going for his masters I believe. Well, since there is not going to be any programming on the midterm or final, I don't mind learning it ourselves. They just want us to get a glimpse at what programming in something other than C/C++ is like, since that's what all the other classes use, except one I think which teaches Java.

[Edited by - deadlydog on October 16, 2004 11:56:50 AM]

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this