with Ada.Text_IO; with Ada.Integer_Text_IO; procedure Triangle is -- This program displays an equilateral triangle of dollar signs -- -- Input -- The size of the triangle (number of rows of dollar signs) -- -- Output -- Prompts -- The triangle ---------------------------------------------------------------------------- procedure Display_Blanks (Number : in Integer) is -- Displays a group of blanks -- -- Number is the number of blanks to display Blank_Count : Integer; -- Loop control variable for displaying blanks begin Blank_Count := 1; -- Display the leading blanks -- Each iteration, display one blank Blank_Loop : loop exit Blank_Loop when Blank_Count > Number; Ada.Text_IO.Put (Item => " "); Blank_Count := Blank_Count + 1; end loop Blank_Loop; end Display_Blanks; ---------------------------------------------------------------------------- procedure Display_Signs (Number : in Integer) is -- Displays a group of dollar signs -- -- Number is the number of dollar signs to display Dollar_Count : Integer; -- Loop control variable for displaying signs begin Dollar_Count := 1; -- Display the dollar signs -- Each iteration, display one dollar sign Dollar_Loop : loop exit Dollar_Loop when Dollar_Count > Number; Ada.Text_IO.Put (Item => "$"); Dollar_Count := Dollar_Count + 1; end loop Dollar_Loop; end Display_Signs; ---------------------------------------------------------------------------- procedure Display_One_Line (Size : in Integer; Line : in Integer) is -- Display one line of the triangle -- -- Size is the total number of rows in the triangle -- Line is the number of the line being displayed here -- (1 for top line of triangle, Size for bottom line of triangle) Num_Blanks : Integer; -- Number of leading blanks Num_Signs : Integer; -- Number of dollar signs begin Num_Blanks := Size - Line; -- Display Num_Blanks blanks Display_Blanks (Number => Num_Blanks); Num_Signs := 2 * Line - 1; -- Display Num_Signs dollar signs Display_Signs (Number => Num_Signs); Ada.Text_IO.New_Line; end Display_One_Line; ---------------------------------------------------------------------------- procedure Get_Valid_Size (Size : out Integer) is -- Returns a positive even whole number begin -- Get a valid size -- Each iteration, get and check one number Size_Loop : loop Ada.Text_IO.Put (Item => "Enter an even positive triangle size: "); Ada.Integer_Text_IO.Get (Item => Size); exit Size_Loop when Size > 0 and Size rem 2 = 0; Ada.Text_IO.Put (Item => "Size must be positive and even"); Ada.Text_IO.New_Line (Spacing => 2); end loop Size_Loop; end Get_Valid_Size; ------------------------------------------------------------------------------- -- Variables for the main subprogram Size : Integer; -- The number of rows in the triangle Line_Count : Integer; -- Loop control variable for displaying rows begin -- Get a valid size Get_Valid_Size (Size => Size); Ada.Text_IO.New_Line (Spacing => 2); Line_Count := 1; -- Display the triangle -- Each iteration, display one line Line_Loop : loop exit Line_Loop when Line_Count > Size; Display_One_Line (Size => Size, Line => Line_Count); Line_Count := Line_Count + 1; end loop Line_Loop; end Triangle;