I am an Ada noob, and am writing a simple function that takes a list of integers and decrements each element by 1. My bronze mode proof passes ok but trying to actually use the function in the main to see if it is actually doing what it is supposed to is proving a struggle. I'm not sure how I initialize the array and assign values (which are supposed to be 0..10). I'm also not sure if I could have implemented this with just the one decrement function as opposed to decrement(integer) and a decrementList(ArrayOfNumbers) functions. Or if I'm doing it right at all. Any help would be ace, there is not any good documentation that I have found so I feel like I'm supposed to guess my way round. The package is called flip_coin as it is part of an earlier task, anything related to flipping coins can be ignored!
here is the spec file:
package flip_coin with SPARK_Mode is
type Coin is (Heads, Tails);
type Index is range 0 .. 10;
type Numbers is array (Index) of Integer;
function flip (x : Coin) return Coin with
Post => flip'Result /= x;
procedure flipCoin (x : in out Coin);
function decrement (i : Integer) return Integer;
procedure decrementList (n : in out Numbers);
end flip_coin;
Here is the body file:
package body flip_coin with SPARK_Mode is
function flip (x : Coin) return Coin
is
begin
if x = Heads then return Tails; else return Heads; end if;
end flip;
procedure flipCoin (x : in out Coin)
is
begin
x := flip(x);
end flipCoin;
function decrement (i : Integer) return Integer
is
begin
return i-1;
end decrement;
procedure decrementList (n : in out Numbers) is
a : Index := n'First;
b : Index := n'Last;
begin
for i in a..b loop
n(i) := decrement(n(i));
end loop;
end decrementList;
end flip_coin;
and here is the main file:
with flip_coin; use flip_coin;
with Ada.Text_IO; use Ada.Text_IO;
procedure Main is
newCoin : Coin := Heads;
numbers : Numbers := (0,1,2,3,4,5,6,7,8,9,10);
begin
Put_Line("Element 9, before decrement:");
Put_Line(numbers(9)'Image);
decrementList(numbers);
Put_Line("Element 9, after decrement:");
Put_Line(numbers(9)'Image);
Put_Line("Coin before flip:");
Put_Line(newCoin'Image);
flipCoin(newCoin);
Put_Line("Coin after flip:");
Put_Line(newCoin'Image);
end Main;
here is my error when I try to run:
main.adb:6:14: object "Numbers" cannot be used before end of its declaration
Any help again greatly appreciated. TIA
Ada is case insensitive language. Thus, numbers and Numbers are the same. How about change numbers to numbers_array.