`Hello I am working on a project where I have to sort through a text file and find the number of times a character occurs, I have everything just about done with it but I cant figure out how to add number of occurrences of each letter into an array so I can sort it. I believe once I can add the number of occurrences into an array as it goes through the Counters(I), I can get it to sort but I don't know how to add those items into an array can anyone help?
This is where I go through the txt file and print the results
This is how i declare everything and the sorting alg I am trying to use
with Ada.Strings.Maps;
with Ada.Text_IO; use Ada.text_io;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO; -- this if for float operations needed for percentages
--
procedure test is
Counters: array (Character) of Integer := (others => 0); -- initialize all Counters to 0
type Arr is array (Positive range <>) of Integer;
C: Character;
Infile , OutFile : File_Type;
Uppercount : Natural;
Lowercount : Natural;
Specialcount : Natural;
--Occ : array :
subtype lower is Character range 'a'..'z'; -- lowercase letters
subtype upper is Character range 'A'..'Z'; -- uppercase letters
--P : Arr := Counters;
subtype Non_Negative_Float is Float range 0.0 .. Float'Last;
Total : Non_Negative_Float;
Total2 : Natural;
--Counters : array_type;
--sorting alg I am tring to use but cant get Counter(I) values to go into an array so I can sort it here
procedure Sort (A : in out Arr) is
Min : Positive;
Temp : Integer;
begin
for I in A'First..A'Last - 1 loop
Min := I;
for J in I + 1..A'Last loop
if A (Min) > A (J) then
Min := J;
end if;
end loop;
if Min /= I then
Temp := A (I);
A (I) := A (Min);
A (Min) := Temp;
end if;
end loop;
end Sort;
begin
Uppercount := 0;
Lowercount := 0;
Specialcount := 0;
Open(InFile, In_File, ".\src\Exam2data.txt");
Create(OutFile, Out_file, ".\src\Maginsky.txt");
while not Ada.Text_IO.End_Of_File(InFile) loop
Ada.Text_IO.Get(InFile, C);
if C in lower then
Lowercount := Lowercount + 1;
elsif C in upper then
Uppercount := Uppercount + 1;
else
Specialcount := Specialcount + 1;
end if;
Counters(C) := Counters(C) + 1;
end loop;
-- goes through the file and prints the letter and times is occures
--needs to set Counters(I) values into an array as it goes through the for loop
for I in Counters'Range loop
if Counters(I) >= 0 then
if I in lower or I in upper then
-- I was not able to figure out how to put the Counters(I) into another array so it can get sorted
-- I believe that if I can put the Counter(I) values into a new array i can sort that that array and get the
-- correct decenting order as needed but I cant figure out how to put these values into an array for sorting
--Sort(Counters(I));
Ada.Text_IO.Put_Line("'" & I & "':" & Integer'Image(Counters(I)) & " Times");
Ada.Text_IO.Put_Line(OutFile, "'" & I & "':" & Integer'Image(Counters(I)) & " Times");
end if;
end if;
end loop;