I am trying to create a decrement program in SPARK Ada. D1 to D3 are the input digits entered by the user and the program asks to decrement a 3 digit number by one and output 3 digits O1, O2, O3. I am not sure how to modify this to element of type digits. Then I will adapt it so the numbers are given as a record type consisting of 3 digits. Any help to helpful websites/explanation will be highly appreciated.
Eg1 of program) if d1=1 d2=2 d3=3 then output = 122. Eg2 of program) input d1=0 d2=0 d3=0 then output = 999.
This is my source code so far:
pragma SPARK_Mode;
package Decrement is
type d1 is new Integer range 0 .. 9;
type d2 is new Integer range 0 .. 9;
type d3 is new Integer range 0 .. 9;
procedure Dec(d1,d2,d3 : in Integer; o1,o2,o3 : out Integer);
end Decrement;
What behavior do you want when the value of the digit to decrement is 0? Should the result be 9? If so you want to define the digit type as a modular type
Your package should then be defined as:
Upon looking at the package definition above you might want to consider how you really want the procedure Dec to behave. For instance, should the three in parameters actually be an array of digits? If so the output parameters can also be an array of digits. Do you want to modify the input parameters directly rather than making a decremented copy of their values? If so you should change the package definition as follows:
If, on the other hand you want a decremented copy of the input values you can declare a function:
If you really want the user to input a three digit number and you want to decrement each digit of the number then the package should look something like:
Inside the package body you will then need to isolate each digit, decrement the digit, and then return the reconstructed integer from the decremented digits.
Your description of what you need to do must be clarified so that you know what to do.