How to resolve the algorithm General FizzBuzz step by step in the Ada programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm General FizzBuzz step by step in the Ada programming language

Table of Contents

Problem Statement

Write a generalized version of FizzBuzz that works for any list of factors, along with their words. This is basically a "fizzbuzz" implementation where the user supplies the parameters. The user will enter the max number, then they will enter the factors to be calculated along with the corresponding word to be printed. For simplicity's sake, assume the user will input an integer as the max number and 3 factors, each with a word associated with them.

For example, given: In other words: For this example, print the numbers 1 through 20, replacing every multiple of 3 with "Fizz", every multiple of 5 with "Buzz", and every multiple of 7 with "Baxx". In the case where a number is a multiple of at least two factors, print each of the words associated with those factors in the order of least to greatest factor. For instance, the number 15 is a multiple of both 3 and 5; print "FizzBuzz". If the max number was 105 instead of 20, you would print "FizzBuzzBaxx" because it's a multiple of 3, 5, and 7.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm General FizzBuzz step by step in the Ada programming language

Source code in the ada programming language

with Ada.Text_IO;              use Ada.Text_IO;
with Ada.Integer_Text_IO;      use Ada.Integer_Text_IO;
with Ada.Containers.Generic_Array_Sort;
with Ada.Strings.Unbounded;    use Ada.Strings.Unbounded;
with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO;

procedure Main is
   type map_element is record
      Num  : Positive;
      Word : Unbounded_String;
   end record;

   type map_list is array (Positive range <>) of map_element;

   function "<" (Left, Right : map_element) return Boolean is
   begin
      return Left.Num < Right.Num;
   end "<";

   procedure list_sort is new Ada.Containers.Generic_Array_Sort
     (Index_Type => Positive, Element_Type => map_element,
      Array_Type => map_list);
   
   procedure general_fizz_buzz (max : Positive; words : in out map_list) is
      found : Boolean;
   begin
      list_sort (words);

      for i in 1 .. max loop
         found := False;
         for element of words loop
            if i mod element.Num = 0 then
               found := True;
               Put (element.Word);
            end if;
         end loop;
         if not found then
            Put (Item => i, Width => 1);
         end if;
         New_Line;
      end loop;
   end general_fizz_buzz;

   fizzy : map_list :=
     ((3, To_Unbounded_String ("FIZZ")), (7, To_Unbounded_String ("BAXX")),
      (5, To_Unbounded_String ("BUZZ")));

begin
   general_fizz_buzz (20, fizzy);
end Main;


  

You may also check:How to resolve the algorithm Read entire file step by step in the Make programming language
You may also check:How to resolve the algorithm Sum of a series step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Hickerson series of almost integers step by step in the Perl programming language
You may also check:How to resolve the algorithm Unix/ls step by step in the Racket programming language
You may also check:How to resolve the algorithm Exponentiation operator step by step in the F# programming language