PROGRAM 1
-- Filename: program1.adb Author: Anthony F. Ortiz -- This program computes and displays the hailstones sequence. -- rule 1. start with a postive integer n(1), for i >= 1. -- rule 2. if n(i) is even, n(i + 1) = n(i) / 2. -- rule 3. if n(1) is odd, n (i + 1) = 3 * n(i) + 1. -- rule 4. the sequence terminates with a value of 1. -- Here is an example 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8 -- 4, 2, 1. with Text_IO; use Text_IO; procedure Hailstones is package Int_IO is new Integer_IO (Integer); use Int_IO; Hailstone: Integer; -- Next hailstone. Temp : Integer; -- Temporary placement for current hailstone. begin -- Enter starting hailstone. Put ("enter a number from 2 to 500. "); Get (Hailstone); New_Line; -- Loop until hailstone = 1. while (Hailstone /= 1) loop -- Compute formula 1 if hailstone is odd. Compute formula 2 if -- hailstone is even. Place the value in temp so the second if -- statement doesn't affect hailstone. After both if statements -- execute, put the value in temp into hailstone. if ((Hailstone mod 2) = 0) then Temp := Hailstone / 2; else Temp := 3 * Hailstone + 1; end if; Hailstone := Temp; -- Display next hailstone. Put (Hailstone, 10); end loop; end Hailstones; -- outfile: program1.out enter a number from 2 to 500. 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
BACK TO CS3120 PAGE.