Arrays
Constrained Array
Goal: declare a constrained array and implement operations on it.
Steps:
Implement the
Constrained_Arrays
package.
Declare the range type
My_Index
.Declare the array type
My_Array
.Declare and implement the
Init
function.Declare and implement the
Double
procedure.Declare and implement the
First_Elem
function.Declare and implement the
Last_Elem
function.Declare and implement the
Length
function.Declare the object
A
ofMy_Array
type.
Requirements:
Range type
My_Index
has a range from 1 to 10.
My_Array
is a constrained array ofInteger
type.
It must make use of the
My_Index
type.It is therefore limited to 10 elements.
Function
Init
returns an array where each element is initialized with the corresponding index.Procedure
Double
doubles the value of each element of an array.Function
First_Elem
returns the first element of the array.Function
Last_Elem
returns the last element of the array.Function
Length
returns the length of the array.Object
A
ofMy_Array
type is initialized with:
the values 1 and 2 for the first two elements, and
42 for all other elements.
Colors: Lookup-Table
Goal: rewrite a package to represent HTML colors in RGB format using a lookup table.
Steps:
Implement the
Color_Types
package.
Declare the array type
HTML_Color_RGB
.Declare the
To_RGB_Lookup_Table
object and initialize it.Adapt the implementation of the
To_RGB
function.
Requirements:
Array type
HTML_Color_RGB
is used for the table.The
To_RGB_Lookup_Table
object ofHTML_Color_RGB
type contains the lookup table.
This table must be implemented as an array of constant values.
The implementation of the
To_RGB
function must use theTo_RGB_Lookup_Table
object.
Remarks:
This exercise is based on the HTML colors exercise from a previous lab (Records).
In the previous implementation, you could use a
case
statement to implement theTo_RGB
function. Here, you must rewrite the function using a look-up table.
The implementation of the
To_RGB
function below includes the case statement as commented-out code. You can use this as your starting point: you just need to copy it and convert the case statement to an array declaration.
Don't use a case statement to implement the
To_RGB
function. Instead, write code that accessesTo_RGB_Lookup_Table
to get the correct value.The following table contains the HTML colors and the corresponding value in hexadecimal form for each color element:
Color
Red
Green
Blue
Salmon
#FA
#80
#72
Firebrick
#B2
#22
#22
Red
#FF
#00
#00
Darkred
#8B
#00
#00
Lime
#00
#FF
#00
Forestgreen
#22
#8B
#22
Green
#00
#80
#00
Darkgreen
#00
#64
#00
Blue
#00
#00
#FF
Mediumblue
#00
#00
#CD
Darkblue
#00
#00
#8B
Unconstrained Array
Goal: declare an unconstrained array and implement operations on it.
Steps:
Implement the
Unconstrained_Arrays
package.
Declare the
My_Array
type.Declare and implement the
Init
procedure.Declare and implement the
Init
function.Declare and implement the
Double
procedure.Declare and implement the
Diff_Prev_Elem
function.
Requirements:
My_Array
is an unconstrained array (with aPositive
range) ofInteger
elements.Procedure
Init
initializes each element with the index starting with the last one.
For example, for an array of 3 elements where the index of the first element is 1 (
My_Array (1 .. 3)
), the values of these elements after a call toInit
must be(3, 2, 1)
.Function
Init
returns an array based on the lengthL
and start indexI
provided to theInit
function.
I
indicates the index of the first element of the array.
L
indicates the length of the array.Both
I
andL
must be positive.This is its declaration:
function Init (I, L : Positive) return My_Array;
.You must initialize the elements of the array in the same manner as for the
Init
procedure described above.Procedure
Double
doubles each element of an array.Function
Diff_Prev_Elem
returns — for each element of an input arrayA
— an array with the difference between an element of arrayA
and the previous element.
For the first element, the difference must be zero.
For example:
INPUT:
(2, 5, 15)
RETURN of
Diff_Prev_Elem
:(0, 3, 10)
, where
0
is the constant difference for the first element;
5 - 2 = 3
is the difference between the second and the first elements of the input array;
15 - 5 = 10
is the difference between the third and the second elements of the input array.
Remarks:
For an array
A
, you can retrieve the index of the last element with the attribute'Last
.For example:
Y : Positive := A'Last;
This can be useful during the implementation of procedure
Init
.
For the implementation of the
Init
function, you can call theInit
procedure to initialize the elements. By doing this, you avoid code duplication.Some hints about attributes:
You can use the range attribute (
A'Range
) to retrieve the range of an arrayA
.You can also use the range attribute in the declaration of another array (e.g.:
B : My_Array (A'Range)
).Alternatively, you can use the
A'First
andA'Last
attributes in an array declaration.
Product info
Goal: create a system to keep track of quantities and prices of products.
Steps:
Implement the
Product_Info_Pkg
package.
Declare the array type
Product_Infos
.Declare the array type
Currency_Array
.Implement the
Total
procedure.Implement the
Total
function returning an array ofCurrency_Array
type.Implement the
Total
function returning a single value ofCurrency
type.
Requirements:
Quantity of an individual product is represented by the
Quantity
subtype.Price of an individual product is represented by the
Currency
subtype.Record type
Product_Info
deals with information for various products.Array type
Product_Infos
is used to represent a list of products.Array type
Currency_Array
is used to represent a list of total values of individual products (see more details below).Procedure
Total
receives an input array of products.
It outputs an array with the total value of each product using the
Currency_Array
type.The total value of an individual product is calculated by multiplying the quantity for this product by its price.
Function
Total
returns an array ofCurrency_Array
type.
This function has the same purpose as the procedure
Total
.The difference is that the function returns an array instead of providing this array as an output parameter.
The second function
Total
returns a single value ofCurrency
type.
This function receives an array of products.
It returns a single value corresponding to the total value for all products in the system.
Remarks:
You can use
Currency (Q)
to convert from an elementQ
ofQuantity
type to theCurrency
type.
As you might remember, Ada requires an explicit conversion in calculations where variables of both integer and floating-point types are used.
In our case, the
Quantity
subtype is based on theInteger
type and theCurrency
subtype is based on theFloat
type, so a conversion is necessary in calculations using those types.
String_10
Goal: work with constrained string types.
Steps:
Implement the
Strings_10
package.
Declare the
String_10
type.Implement the
To_String_10
function.
Requirements:
The constrained string type
String_10
is an array of ten characters.Function
To_String_10
returns constrained strings ofString_10
type based on an input parameter ofString
type.
For strings that are more than 10 characters, omit everything after the 11th character.
For strings that are fewer than 10 characters, pad the string with ' ' characters until it is 10 characters.
Remarks:
Declaring
String_10
as a subtype ofString
is the easiest way.
You may declare it as a new type as well. However, this requires some adaptations in the
Main
test procedure.You can use
Integer'Min
to calculate the minimum of two integer values.
List of Names
Goal: create a system for a list of names and ages.
Steps:
Implement the
Names_Ages
package.
Declare the
People_Array
array type.Complete the declaration of the
People
record type with thePeople_A
element ofPeople_Array
type.Implement the
Add
procedure.Implement the
Reset
procedure.Implement the
Get
function.Implement the
Update
procedure.Implement the
Display
procedure.
Requirements:
Each person is represented by the
Person
type, which is a record containing the name and the age of that person.
People_Array
is an unconstrained array ofPerson
type with a positive range.The
Max_People
constant is set to 10.Record type
People
contains:
The
People_A
element ofPeople_Array
type.This array must be constrained by the
Max_People
constant.Procedure
Add
adds a person to the list.
By default, the age of this person is set to zero in this procedure.
Procedure
Reset
resets the list.Function
Get
retrieves the age of a person from the list.Procedure
Update
updates the age of a person in the list.Procedure
Display
shows the complete list using the following format:
The first line must be
LIST OF NAMES:
. It is followed by the name and age of each person in the next lines.For each person on the list, the procedure must display the information in the following format:
NAME: XXXX AGE: YY
Remarks:
In the implementation of procedure
Add
, you may use an index to indicate the last valid position in the array — seeLast_Valid
in the code below.In the implementation of procedure
Display
, you should use theTrim
function from theAda.Strings.Fixed
package to format the person's name — for example:Trim (P.Name, Right)
.You may need the
Integer'Min (A, B)
and theInteger'Max (A, B)
functions to get the minimum and maximum values in a comparison between two integer valuesA
andB
.Fixed-length strings can be initialized with whitespaces using the
others
syntax. For example:S : String_10 := (others => ' ');
You may implement additional subprograms to deal with other types declared in the
Names_Ages
package below, such as theName_Type
and thePerson
type.
For example, a function
To_Name_Type
to convert fromString
toName_Type
might be useful.Take a moment to reflect on which additional subprograms could be useful as well.