Nested functions/procedures in Ada, and count unique according to a certain idea of equality.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Counting is | |
type Element is | |
record | |
S : String (1 .. 3); | |
E1 : Integer; | |
E2 : Integer; | |
end record; | |
type Elements is array (Positive range <>) of Element; | |
function Count_Unique (S : Elements) return Integer; | |
end Counting; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package body Counting is | |
function Count_Unique (S : Elements) return Integer | |
is | |
Found : Boolean; | |
C : Integer := 0; | |
function Compare (I, J : Integer) return Integer | |
is | |
begin | |
if S(I).E2 = S(J).E2 then | |
return 0; | |
elsif S(I).E1 /= S(J).E1 then | |
return S(J).E1 - S(I).E1; | |
else | |
return S(J).E2 - S(I).E2; | |
end if; | |
end; | |
begin | |
for I in S'Range loop | |
Found := False; | |
for J in S'First .. I - 1 loop | |
if Compare(I, J) = 0 then | |
Found := True; | |
exit; | |
end if; | |
end loop; | |
if not Found then | |
C := C + 1; | |
end if; | |
end loop; | |
return C; | |
end Count_Unique; | |
end Counting; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with Ada.Text_IO; use Ada.Text_IO; | |
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; | |
with Counting; use Counting; | |
procedure CTest is | |
S : Elements := ( | |
("zak", 2, 0), | |
("mac", 3, 1), | |
("abc", 4, 2), | |
("zak", 5, 0) | |
); | |
begin | |
Put (Count_Unique (S)); | |
New_Line; | |
end CTest; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment