How much are homework assignments going for nowadays? :-)
[snip]
At least you didn't ask someone to design a weapons system for you. :-)
I am trying to decide if this is a troll or a homework assignment.
Assuming that it's the latter, paying someone to do your homework for
you is only a short term solution. Part of doing homework is learning
_how_ to solve problems.
When people want help with homework, they usually ask a very specific
question, along with posting the code that they have tried to date.
If you are stuck on the problem as a whole, can't your tutor give you
some guidance on getting started ?
Sorry that I can't be more help, but we've all faced these problems
while learning, and learning how to overcome them is a required part of
the learning process.
Simon.
--
Simon Clubley, clubley@remove_me.eisner.decus.org-Earth.UFP
Microsoft: The Standard Oil Company of the 21st century
>>The is supposed to be a basic database to monitor a group of made up
>>citizens and their badness rating.
> At least you didn't ask someone to design a weapons system for you. :-)
>
> I am trying to decide if this is a troll or a homework assignment.
I'm trying to figure out what badness is, here. Or a bad citizen.
So far I have learned that in English, "He's bad!" can be a statement
approving of someone's favorable qualities. OTOH, the political
pendulum is currently swinging to the watchers side, so we might
be witnessing another large scale surveillance project.
Could someone do me a favor and elucidate?
TIA,
Georg
I don't think this is homework; I think it's worse than that. Some
unknown group of unknown people are asking us to write a database
containing actual person records in it. We don't know what country
the database will run in, what people will have their records in it,
what is the definition of "badness", and, most importantly, we don't
even know what this unknown group of people is planning to do with the
"bad" people.
"strictly_mk", who are you anyway? If you don't have the time to
learn Ada, that's fine and fair but could you please elaborate on the
more important questions I raised?
--
Ludovic Brenta.
Sounds good. I could do it for only $20 million, cash, in new $100 bills.
Sorry I don't have enough time to write the whole thing write now, but
here's a start (maybe someone else will step in to fill in another routine):
with Ada.Unchecked_Deallocation;
package body POP is
type
RO0O is record lll1 : ID; ll1l : Rating; end record; type
ROO0 is array( positive range <> ) of
RO0O; type R0OO is access all ROO0;
procedure RO00 is new Ada.Unchecked_Deallocation( ROO0, R0OO );
R00O : R0OO; R0O0 : Natural := 0;
procedure Associate (Citizen_1, Citizen_2 : in ID) is
begin
null;
end Associate;
function More_In_Group return Boolean is
begin
return More_In_Group;
end More_In_Group;
function Most_Dangerous return ID is
begin
return Most_Dangerous;
end Most_Dangerous;
function Next_Member return ID is
begin
return Next_Member;
end Next_Member;
procedure Rate
(Citizen : in ID; Badness : in Rating) is begin
if R0O0 = 0 then R00O := new ROO0( 1 .. 100 ); end if;
if R0O0 + 1 > R00O.all'length then
declare R000 : R0OO; begin R000 := new
ROO0( 1 .. R0O0 + 100 ); R000.all( 1 .. R0O0 ) :=
R00O.all( 1 .. R0O0 ); RO00( R00O ); R00O :=
R000; end; end if; R0O0 := R0O0 + 1;
R00O( R0O0 ).lll1 := citizen; R00O( R0O0 ).ll1l := badness; end
Rate;
procedure reset is
begin
null;
end reset;
end POP;
Steve
(The Duck)
>
> You cannot change this specification in any way other than to insert
> the appropriate type for ID;
> As you can see this is supposed to be a database package, it should not
> however do any input or output to the screent as the GUI is being
> designed by someone else.
>
> Rate is supposed to enter a citizen with his/her ID (integer) and their
> badness (integer) into the database.
> Associate is used to tell the database two citizens are associated to
> be in the same group.
> Most_Dangerous is supposed to return the citizen with the highest
> badness rating. This function can do anything if the database is empty.
> Next_Member reports a previously unreported member of the group to
> which the most dangerous citizen belongs. This function can do anything
> if the there are no more unreported members.
> More_In_Group which is true exactly when there are unreported members
> of the group to which the most recently reported most dangerous citizen
> belongs. This function can do anything if there is no previous call to
> Most_Dangerous.
> The procedure rest resets the database to its original state.
> Sounds good. I could do it for only $20 million, cash, in new $100 bills.
And me for only $15 million :)
Pascal.
--
--|------------------------------------------------------
--| Pascal Obry Team-Ada Member
--| 45, rue Gabriel Peri - 78114 Magny Les Hameaux FRANCE
--|------------------------------------------------------
--| http://www.obry.net
--| "The best way to travel is by means of imagination"
--|
--| gpg --keyserver wwwkeys.pgp.net --recv-key C1082595
> And me for only $15 million :)
Euros?
No $15 million is fine, but I'm ok for 15 million euros of course :)
It's cheaper to buy in Europe,
http://www.ecb.int/stats/exchange/eurofxref/html/eurofxref-graph-usd.en.html
> type Society_Ptr is access all Society;
>
> Pointer : Society_Ptr;
Are you sure you need a pointer. Most Ada compilers won't have a problem
creating the even a large array in static memory:
http://en.wikibooks.org/wiki/Programming:Ada:Types:array
Do you really need "access all"
http://en.wikibooks.org/wiki/Programming:Ada:Types:access#Access_vs._access_all
And last not least: there is no "new" to be seen anywhere:
http://en.wikibooks.org/wiki/Programming:Ada:Types:access#Creating_object_in_a_storage_pool
Martin
--
mailto://kris...@users.sourceforge.net
Ada programming at: http://ada.krischik.com
I take it that ID is range 1 .. 1_000_000? if so you should index
Society by ID.
Why do you have pointers? you could just have an array of individuals.
Names are very important. "Pointer" is a very unhelpful name for the
concept you have in mind!
type Individuals is array (ID) of Indivuals;
Society : Individuals;
How are you going to know whether a given Individual exists? (has been
Rated, I think?)
--S
I'd probably use PragmARC.Skip_List_Unbounded for the collection of
rated innocent victims (appox O(log N) for insertion and lookup), each
having a list of associates. Then finding the highest rated is O(1), and
visiting each of her associates would be approx O(log N).
--
Jeff Carter
"Why don't you bore a hole in yourself and let the sap run out?"
Horse Feathers
49
Now that you're asking for help, and not for someone to do it for you, as
you can see, you get a lot more positive response.
> package body POP is
>
> type Individual is
> record
> Person : ID;
> Danger : Rating;
> Associate : ID;
> Reported : Boolean;
> end record;
>
> type Society is array( 1 .. 1000000 ) of Individual;
>
> type Society_Ptr is access all Society;
>
> Pointer : Society_Ptr;
> Counter : Natrual := 0;
>
If the array is going to be fixed in size, why not just use:
type Society is array( 1 .. 1000000 ) of Individual;
Society_Buffer : Society;
In my original response I used a pointer because I dynamically allocated
an array to store individuals. When the array became full, I dynamically
allocated a new larger array, copied the values from the original array to
the new larger array and then deallocated the original array. It is
certainly simpler to just go with a fixed maximum size.
> procedure Rate (Citizen : in ID; Badness : in Rating) is
> begin
> Counter := Counter + 1;
> Pointer( Counter ).Person := Citizen;
> Pointer( Counter ).Rating := Badness;
> Pointer( Counter ).Associate := null;
> Pointer( Counter ).Reported := false;
> end;
> end Rate;
>
Just a note: One of the features I really like about Ada is the ability to
do record assignments, so this Rate function could be:
Counter := Counter + 1;
Pointer( Counter ) := ( Person => Citizen, Rating => Badness, Associate =>
null, Reported => False );
> procedure Associate (Citizen_1, Citizen_2 : in ID) is
>
> I : Integer
>
> for I in 0..Counter loop
> if Pointer( I ).Citizen = Citizen_1
> then
> Pointer( I ).Associate := Citizen_2;
> end if;
> end Associate;
>
> --query functions
> function Most_Dangerous return ID is
>
> MD_Counter : Integer := 1;
>
> I : Integer
>
> for I in 0..Counter loop
> if Society(I).Danger < Society(MD_Counter).Danger then
> if Society(MD_Counter).Reported = false then
> begin
> Most_Dangerous_Pointer := MD_Counter;
> MD_Counter := MD_Counter + 1;
> end;
> else
> MD_Counter := MD_Counter + 1;
> end if;
> else
> MD_Counter := MD_Counter + 1;
> end if;
> end loop;
>
> begin
> return Pointer(Most_Dangerous_Pointer).Citizen;
> Pointer(Most_Dangerous_Pointer).Reported := true;
> end;
>
> end Most_Dangerous;
>
> I've put all the required information into an array of records. But the
> problem is I realised that if a citizen has more than one associate my
> code won't work. (If the code were to compile) As it stands it would
> logically work if each person only had one associate. I remember an old
> project using code similar to Associate : Associates.Bag within the
> record instances. An I was wondering if anyone knows how this package
> might look like. This way I could put Associate, Next_Member and
> More_In_Group in that package as well. I think this is what the marker
> is looking for as I think I'm supposed to demonstrate my program being
> modular. Any comments?
>
One way to handle the "associates" is to create an association code for each
individual. When two citizens are associated, make the id's of the
associates the same.
You might, for example start out with an association id of 0 indicating no
association. When the Associate procedure is called, find the location of
each of the citizens and handle each of the cases:
If both associates have no association, create a new association id, and
assign it to both.
If just one of the associates has a non-zero association id, assign it to
the other.
If both of the associates have a non-zero association id (ie: A and B),
then change the association ID of all citizens that are currently B to A.
I am assuming that:
if A associated with B and B associated with C it is implied that A
associated with C.
I hope this helps,
Steve
(The Duck)
I thought the requirement was only to visit all the associates of the
highest rated individual. If you have to visit associates of associates
of associates of ... then it becomes a little more time intensive.
--
Jeffrey Carter
"Now go away or I shall taunt you a second time."
Monty Python and the Holy Grail
E-mail: jeffrey_r_carter-nr [commercial-at]
raytheon [period | full stop] com
generic
type ID is (<>); --some discrete type to be put here
package POP is
--update database
type Rating is new Integer range 0..255;
procedure Rate (Citizen : in ID;
Badness : in Rating);
procedure Associate (Citizen_1, Citizen_2 : in ID);
--query the database
function Most_Dangerous return ID;
function Next_Member return ID;
function More_In_Group return Boolean;
--administrative
procedure reset;
end POP;
Since the type ID is discrete, why not just use that as the array index type?
package body POP is
Society : array (ID) of Rating := (others => Rating'First);
Associacion : array (ID, ID) of Boolean := (others => others => False));
pragma Pack (Association);
Last_Reported : ID := ID'First;
procedure Rate (Citizen : in ID;
Badness : in Rating) is
begin
Society (Citizen) := Rating;
end Rate;
procedure Associate (Citizen_1, Citizen_2 : in ID) is
begin
Association (Citizen_1, Citizen_2) := True;
end Associate;
function Most_Dagerous return ID is
Result : ID := ID'First;
begin
for J in Society'Range loop
if Society (J) > Result then
Result := J;
end if;
end loop;
return Result;
end Most_Dangerous;
function Next_Member return ID is
J : constant ID := Most_Dangerous;
begin
if Last_Reported = ID'First then
Last_Reported := J;
end if;
for K in ID'Succ (Last_Reported) .. Association'Last (2) loop
if Association (J, K) then
Last_Reported := K;
exit;
end loop;
end loop;
return Last_Reported;
end Next_Member;
function More_In_Group return Boolean is
J : constant ID := Most_Dangerous;
Result : Boolean := False;
begin
for K in ID'Succ (Last_Reported) .. Association'Last (2) loop
if Association (J, K) then
Result := True;
exit;
end loop;
end loop;
return Result;
end More_In_Group;
procedure Reset is
begin
Society := (others => Rating'First);
Association := (others => others => False));
Last_Reported := ID'First;
end Reset;
end POP;
Of course, the above solution is outrageously inefficient, both
memory- and CPU-wise. Optimisations are left as an exercise to the
reader.
--
Ludovic Brenta.
Most_Dangerous = 10
Last_Reported = 20
Citizens 5 and 6 are associated.
then re-rate Citizen 5 as even more dangerous than 10.
Citizen 6 will not be noticed by Next_Member or More_In_Group.
Associate as written is not symmetric - is that what's wanted?
association is not transitive - is that what's wanted?
I would also change type Rating to Rating_Levels and array Society to
Rating. Then "if Rating(J) > " reads better than "if Society(J) > ".
I notice a couple of typos, but the compiler will point them out.
So, I think all you need to do is set your counts and indexes back to 0.
Steve
(The Duck)
Remember that a symmetric array A(1 .. N, 1 .. N) can be stored in half
the space as a vector V(1 .. (N*(N+1))/2), where A(i,j) is at
V((max(i,j)*(max(i,j)-1))/2+min(i,j))
Perhaps I'm a little naive, but I really don't understand why you need to
keep any more than:
type Persson is
record
Citizen_Id : ID;
Badness : Rating;
Group_Id : Natural;
end record;
type Population is array( 1 .. 1_000_000) of Person;
Society : Population;
Next_In : Positive; -- Add new elements here
Next_Out : Positive; -- Retrieve next from current group here
Baddest : Positive; -- Index to baddest entry
Current_Group_Id : Natural;
You do wind up scanning the list to do just about anything, but speed was
not a requirement.
Steve
(The Duck)
[snip]
>
>
> --
> > ============================================================== <
> > wlf...@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
> > wulf...@dm.net | Bestiaria Support Staff <
> > ============================================================== <
> > Home Page: <http://www.dm.net/~wulfraed/> <
> > Overflow Page: <http://wlfraed.home.netcom.com/> <
>
> Jeffrey Carter <sp...@spam.com> writes:
>
> > Sounds good. I could do it for only $20 million, cash, in new $100 bills.
>
> And me for only $15 million :)
>
comp.lang.c has a consensus that "bidding" on homework assignments can
only go up, not down. I think they got this one right.
Aside: _Are_ there any new $100 bills? I haven't seen any since the
redesigned $20's started, in what-was-it 1997 or so, and I heard the
Treasury (or Fed?) wasn't going to do new $100s since this
denomination is believed useful primarily to criminals. Of course they
get little enough circulation you could certainly ask for "like-new"
or "good condition". (I am assuming $=USD, not CA or AU or such.)
- David.Thompson1 at worldnet.att.net
all bidders forgot to specify what kind of dollars they were using for
their offer.
the OP may eventually afford ZWD 15_000_000 (Zimbabwe Dollars), but
the bidders may have requested KYD 15_000_000 (Cayman Islands Dollars)...
--
rien
you declare:
type Associates is array ( Population_Range , Population_Range ) of
Boolean;
this declares a type, not a variable. you have to declare a variable
of this type to use this data structure.
> ID is defined in the specification under generic as a long_integer.
ID is NOT generic:
- it does not appear in a generic clause to this package,
- you convert variables of type ID explicitely to Long_Integer
everywhere in this package, preventing a truly generic definition of ID.
(additionnaly, the array to store citizen association is indexed by
Population_Range, not by ID, which leads to a compilation error in the
procedure Associate)
and what about Rating ? it seems not defined anywhere...
--
rien
> subtype Population_Range is Long_Integer range 1..1000000;
> ID is defined in the specification under generic as a long_integer. Can
> anyone help?
(I'd suggest using the ID generic formal in the definition of
Population_Range then.)
Associates is a type actually, but is used as if it were variable.
yes. you can even write:
Associates : array ( Population_Range , Population_Range ) of Boolean;
though it may comes in the way later if you want to extend this
package (to manage more than one population for example).
--
rien
> I'm a little confused with the generic part though - i don't know how
> to manipulate this bit.
generic
type T is {something};
package Pack is
-- refer to T is if it were a known type.
-- What you know about the type here is
-- expressed in {something}. For example,
-- when {something} is replaced by "range <>"
-- then you can use T inside Pack as if it were a
-- signed integer type. When you make an instance
-- of Pack, use some subtype of a signed integer
-- type, like Natural, etc..
I guess you are referring to the generic Pack?
I had been assuming that ID is a generic formal type, like in
generic
type ID is new Long_Integer;
package PoP is
Is that your choice, or have they given you this piece of the
program text?
Use one of Ada's formal types. That is, specify what
the generic needs as a type using the syntax for formal
types. Examples
generic
type T is range <>;
package Pack ...
In this case the formal type T says, if you want to make
an instance of this generic package, you have to provide
a signed integer type as the (actual) type to be used in
the created instance, as in
package Foo is new Pack (T => Integer);
Integer is a signed integer type, as are Natural, or
Long_Integer. When you instantiate Pack, you can choose the type
to be used. When you write the generic package, you only
specify some assumptions about the possible types.
In this case, "range <>" specifies "some signed integer type".
Given these possible assumptions about T, you can use
T in expressions, without knowing what T is going to be in
some instance of the generic package. Like T'First (the first of
the values in T's range). You can add values of T because
T denotes some integer type, no matter which one actually,
later.
Another example is
generic
type T is (<>);
package Pack2 ...
In this case formal type T says, if you want to make
an instance of this generic package, you have to provide
a discrete type as the (actual) type to be used in
the created instance. Discrete types are either integer
types or enumeration types. Just no real types.
So if you had, somewhere
type Family is (Jack, Jakub, Florentina, Marge, Bart);
i.e. an enumeration type, you could say
package Foo2 is new Pack2 (T => Family);
Inside Pack2 (the generic) you can use T just like you
use any enumeration type. For example, you can say T'First,
but you cannot add values, because this doesn't work for
enumeration types. When you write the package, you know you
can assume T is denoting an enumeration type, but not which one.
Because that is decided when someone, possibly you, instantiate
Pack2.
Last, when you have
generic
type ID is new Long_Integer;
package PoP ...
the choice of types to be used when instantiating is pretty fixed.
Only types derived from Long_Integer (includes Long_Integer) or
subtypes thereof can be filled in when you write
package My_PoP_Instance is new PoP(ID => ...);
Someone needs to understand the concept of Booleans.
--
Jeff Carter
"If you don't get the President of the United States on that
phone, ... you're going to have to answer to the Coca-Cola
Company."
Dr. Strangelove
32
You have
generic
type ID is (<>);
package PoP is ...
Some of your subprograms inside the package take ID parameters.
A discrete type (that's what "(<>)" implies) lets you base array type
definitions inside the package on properties of the generic discrete
type ID, like so:
type Some_Array_Type is array (ID'range) of Some_Type;
This is the basically same as
type Some_Array_Type is
array(ID'first .. ID'last) of Some_Type;
'Range, 'first, and 'last are properties available with every discrete
type.
Likewise, you can write loops,
for k in ID'range loop
...
end loop;
or
some_variable: Some_Array_Type;
...
for k in some_variable'first .. some_variable'last loop
-- use some_variable(k) here
end loop;
BTW, there is no need to declare for-loop variables, these
are "automatically" declared.
So just give up thinking in terms of whatever Integer type you
have in mind. Your job, IIUC, is to write code inside a generic
that is parameterized by the type "ID". This type is a discrete
type, this is all you know, and it is all you need to know.
This is the same as
type Some_Array_Type is array (ID) of Some_Type;
Attributes are fine, but let's not overuse them.
--
Jeffrey Carter
"Now go away or I shall taunt you a second time."
Monty Python and the Holy Grail
E-mail: jeffrey_r_carter-nr [commercial-at]
raytheon [period | full stop] com
but preferably:
for k in some_variable'range loop
-- use some_variable (k) here
end loop;
The compiler will deduce there are no runtime checks required with this
construct but I've seen plenty compilers that will insert checks in the
other forms (even though we can see they aren't really needed).
Cheers
-- Martin
> type Some_Array_Type is array (ID) of Some_Type;
>
> Attributes are fine, but let's not overuse them.
Right, I wasn't seeing the wood for the trees.
-- Georg
As Dennis has explained, this is done during instantiation.
> What is the range of ID in type Some_Array__Type is array (ID'range) of
> Some_Type; ?
It's ID'Range :-)
That's right, 0 is known to belong to a universal intege type,
but (<>) says discrete type. So you cannot assume there will be
a 0.
> Line 120 refers to the return 0; in the next_member function (again I
> think its to do with 0 not being an integer) (again, is there a way
> around this?)
I think so. If there is a most dangerous person
(i.e., at least one, in math speak I think)
then this person's ID should be the one to use in identifying the person?
Not sure why you assign a default value (?) to Most_Dangerous_Person?
(If you need a default value, some ID values are available
through ID's type attributes.)
> Line 120 refers to the return 0; in the next_member function (again
> I think its to do with 0 not being an integer) (again, is there a
> way around this?)
You need to return the value of ID which means "not a valid ID". That
might be ID'First or ID'Last (your value of 0 implies 'First). But I
expect that your first Person would have an ID of 'First! Sounds like
there's a deep problem in the spec there?
IIRC, the spec states that anything may be returned if there's no valid
value.
--
Jeff Carter
"I was hobbling along, minding my own business, all of a
sudden, up he comes, cures me! One minute I'm a leper with
a trade, next minute my livelihood's gone! Not so much as a
'by your leave!' You're cured, mate. Bloody do-gooder!"
Monty Python's Life of Brian
76
> Simon Wright wrote:
>> You need to return the value of ID which means "not a valid
>> ID". That
>> might be ID'First or ID'Last (your value of 0 implies 'First). But I
>> expect that your first Person would have an ID of 'First! Sounds like
>> there's a deep problem in the spec there?
>
> IIRC, the spec states that anything may be returned if there's no
> valid value.
Oh yes, I was forgetting this is an exercise ..
Are you using the gnatmake or gcc command to compile your code ?
It sounds like you are doing the latter, but you want the former. Ie, if
your main program is called mk_main.adb, you would issue the following
command:
gnatmake mk_main.adb
Gnatmake will then compile and link files as required.
Type gnatmake by itself to see the list of available options.
Simon.
--
Simon Clubley, clubley@remove_me.eisner.decus.org-Earth.UFP
Microsoft: The Standard Oil Company of the 21st century
no switches needed except -gnato if you want integer overflow
checking (as per Ada standard).
the generic package body that you have written cannot be
used alone to compile an executable. some other compilation
unit (source file) must have code that instantiates
your generic package and calls the subprograms. Have you been
given only the spec of the generic package, or is there
something else?
> I am using gnatmake but I dont know which switch/option to use. I cant
> see an appropriate one.
You can't make an executable unless you have a main program. gnatmake
will build an executable if the source file you give it is capable of
being a main program (ie, is a parameterless procedure); otherwise it
won't.