with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;
with Ada.Numerics.Elementary_Functions; use Ada.Numerics.Elementary_Functions;
with Ada.Numerics; use Ada.Numerics;
procedure AdaO11 is
procedure Calculate_Angles (Sa, Hc, : in Float;
Va, Vb, Vc : out Float) is
begin
Va := Arcsin(Sa / Hc);
Vb := Arccos(Sa / Hc);
Vc := 90.0;
end Calculate_Angles;
procedure Angle_Program is
Hc, Sa : Integer;
Va : Float;
begin
Put("Type in the length of the hypotenuse: ");
Get(Hc);
Put("Type in the opposite catheus: ");
Get(Sa);
New_Line;
Put("Va: ");
Put(Calculate_Angles(Va));
end Angle_Program;
Selection : Integer;
begin
Put_Line("Welcome to the calculator!")
end AdaO11;
So, I wonder why I am getting an error when I try to calculate the angles. Did I do something wrong? I really don’t see the error in this. I call in 2 parameters and then I send out 3. The error probably is in that part of my code.
Any help would be appreciated.
I call in 2 parameters and then I send out 3.
Yes, your subprogram named Calculate_Angles has five formal parameters, two with mode in and three with mode out.
The error probably is in that part of my code.
No, the problem is where you call the subprogram in Angle_Program:
Calculate_Angles is a procedure, not a function, so there's no result for Put to use.
You must supply actual variables in your call and they must match the formal parameters in number and type. As a result Angle_Program needs five variables—two from the user and three to hold the results of Calculate_Angles.
procedure Angle_Program is
Hc, Sa : Float;
Va, Vb, Vc : Float;
begin
…
Calculate_Angles (Sa, Hc, Va, Vb, Vc);
…
end;
As an aside, the trigonometric Elementary Functions use radians. To match your Vc value, specify a suitable Cycle parameter to get degrees:
Va := Arcsin (Sa / Hc, 360.0);
Vb := Arccos (Sa / Hc, 360.0);
Typical output, which compares well with this calculator, might look like this:
Type in the length of the hypotenuse: 5
Type in the opposite cathetus: 3
Va: 36.8699
Vb: 53.1301
Vc: 90.0000
My concern is the identifier expected.
Given this,
procedure Calculate_Angles (Sa, Hc, : in Float;…
^
As #Jesper comments, the corresponding error message is this:
ada011.adb:17:39: identifier expected
This arises from an error in line 17 at position 39—a spurious comma. As you are declaring a subprogram's parameter specification, the compiler reasonably expects the defining identifier list to have another defining identifier after the comma.
Related
I want to code this picture
The terminal should show this:
For DS2:
Type your data: viDIn fJFN1 r1f29 M6YFu LDuWG fxFQ6 FkaSL JCGiq niVGg wLAUK
-- User types in data displayed in BOLD TEXT
Your data: viDIn fJFN1 r1f29 M6YFu LDuWG fxFQ6 FkaSL JCGiq niVGg wLAUK
So I'm having trouble with the boolean expressions.
This is my approach
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
procedure Array is
type Array_Inner_Type is
array (5..9) of String(1..5);
type Boolean_Type is
array (False..True) of Array_Inner_Type;
MyArray : Boolean_Type;
procedure Get(MyArray : out Boolean_Type) is
begin
for I in True..False loop
Get(MyArray(I));
end loop;
end Get;
end Array;
Once I get no errors in my terminal I believe I could solve this problem but so far I'm getting errors it is due to my for loop. Any help is appreciated!
I don't know what all those strings are supposed to mean, but I'd be tempted to approach this as
subtype Word is String (1 .. 5);
type Funny_Map is array (Boolean, 5 .. 9) of Word;
Map : Funny_Map;
Here is my code:
procedure String_To_Int(str: String) is
str_length : Integer := str'Size / 8;
ASCII_Values_Array: array (Integer range 1 .. str_length) of Integer;
begin
Text_Io.Put_Line(str & " has a length of " & natural'image(str_length));
for x in 1 .. str_length loop
ASCII_Values_Array(x) := Character'Pos(str(x));
Text_Io.Put_Line(natural'image(ASCII_Values_Array(x)));
end loop;
end String_To_Int;
and I am trying to call it with:
String_To_Int(str => "abcdefghijklmnopqrstuvwxyz");
but the compiler is telling me:
Saw '(', expected: , :
And I have no clue what is wrong about how I am calling my procedure. I have looked at many other examples of procedure calls and this looks exactly the same. Any help is appreciated!
There's something you aren't telling us.
./test_sti;
abcdefghijklmnopqrstuvwxyz has a length of 26
97
98
99
...
121
122
Now you don't say what you're actually doing, but here's what I did.
I called String_To_Int from another procedure, in a file test_sti.adb.
with String_To_Int;
procedure test_sti is
begin
String_To_Int(str => "abcdefghijklmnopqrstuvwxyz");
end;
Note that String_To_Int is a separate procedure, in its own file, so the with clause tells the compiler to look for it. I could have declared it locally, i.e. between "is" and "begin" and saved both the files below, but separation is probably better design.
Now anything "with"ed will have both a specification and a body - in this case, specification string_to_int.ads :
procedure String_To_Int(str: String);
and body string_to_int.adb :
with Ada.Text_IO;
use Ada;
procedure String_To_Int(str: String) is
str_length : Integer := str'Size / 8;
ASCII_Values_Array: array (Integer range 1 .. str_length) of Integer;
begin
Text_Io.Put_Line(str & " has a length of " & natural'image(str_length));
for x in 1 .. str_length loop
ASCII_Values_Array(x) := Character'Pos(str(x));
Text_Io.Put_Line(natural'image(ASCII_Values_Array(x)));
end loop;
end String_To_Int;
and to build the lot, simply
gnatmake test_sti.adb
and the compiler works out its own dependencies, no Makefile necessary.
It's actually a bit odd (but perfectly legal) to have a separate "compilation unit" like this for just one procedure. More normally it would either be declared locally, or it would be a part of a package - either a collection of utilities like Ada.Text_IO, or something like a class if you are familiar with Java or C++.
Incidentally, String_To_Int is a very odd procedure : instead of declaring its variables as
str_length : Integer := str'Size / 8;
ASCII_Values_Array: array (Integer range 1 .. str_length) of Integer;
it's cleaner to use the attributes more consistently:
str_length : constant natural := str'Length;
ASCII_Values_Array: array (str'range) of Integer;
and express the loop condition as
for x in str'range loop
Difference between out mode and in out mode? As far i have collected the info is
the main difference i know is that in both out and in out mode the actual parameter is expected to be altered even writing and reading is also possible then what is main difference please help me to understand?
The best explanation of the difference is in RM 6.4.1(11-15).
Semantically, there is a difference only for parameters that are passed by copy. If you call a subprogram that has an in out parameter, the actual parameter is expected to have a legitimate value before the call. On entry, the subprogram makes a copy of the variable and checks to make sure it satisfies the constraints.
For an out parameter, the actual parameter does not need to have a value going in; it could be uninitialized garbage. The intent is that the subprogram will not use the value of this parameter until it has been set, by the subprogram (it can be set either by assignment or by being passed as an out parameter to some other subprogram, or to a Default_Value, or perhaps by other means). This is not enforced, however.
This can cause different behavior in some cases. For example:
subtype Int10 is Integer range 1 .. 10;
procedure Proc (Param : in out Int10) is
begin
Param := 5;
end Proc;
Y : Integer := 100;
...
Proc (Y);
Since Param is an in out parameter, the constraints are checked on entry. Thus, the call to Proc(Y) raises a Constraint_Error. However:
subtype Int10 is Integer range 1 .. 10;
procedure Proc (Param : out Int10) is
begin
Param := 5;
end Proc;
Y : Integer := 100;
...
Proc (Y);
In this case, no Constraint_Error is raised. In fact, Y does not need to be initialized at all, because the expectation is that the procedure will not use the input value. According to the RM, the value is not even copied, for certain classes of parameter types. So in this case:
Save_Param : Integer;
procedure Proc (Param : out Int10) is
begin
Save_Param := Param; -- legal but not recommended
Param := 5;
end Proc;
Y : Integer := 3;
...
Proc (Y);
Save_Param will probably be set to some garbage value, not 3. (In Ada 2012, there is a Default_Value aspect that can be applied to subtypes; in a case like that, an out parameter would be set to this default value instead of uninitialized garbage, while an in out parameter would still get the value from the actual parameter.)
For parameters that are passed by reference, there really is no difference in behavior.
The rules in Ada 83 were different. A subprogram with an in out parameter could both read and write that parameter, while a subprogram with an out parameter was allowed to assign to the parameter but could not do anything that would read that parameter's value (barring some cases where there was no way around reading discriminants). Thus:
procedure Proc (Param : out Int10) is
begin
Param := 5;
Save_Param := Param; -- illegal in Ada 83, legal in Ada 95 and later versions
end Proc;
In other words, an out parameter really was output-only. The rule was relaxed in Ada 95, though, as programmers found it too restrictive.
I am attempting to understand how to fix this circular dependency. All the examples I can find online suggest using a limited with, but then they demonstrate the use with two basic types, whereas this is a bit more advanced. The circular dependency is between the two files below. I thought it was between package Chessboard ... and the Piece type, but now I am not so sure. Attempting to put the package Chessboard ... line within chess_types.ads after the Piece type is declared and removing the use and with of Chessboard results in an error: this primitive operation is declared too late for the Move procedure. I am stuck on how to get out of this dependency. Any help would be much appreciated!
Thank you
chessboard.ads:
with Ada.Containers.Indefinite_Vectors;
use Ada.Containers;
with Chess_Types;
use Chess_Types;
package Chessboard is new Indefinite_Vectors(Board_Index, Piece'Class);
chess_types.ads:
with Ada.Containers.Indefinite_Vectors;
use Ada.Containers;
with Chessboard;
use Chessboard;
package Chess_Types is
subtype Board_Index is Integer range 1 .. 64;
type Color is (Black, White);
type Piece is tagged
record
Name : String (1 .. 3) := " ";
Alive : Boolean := False;
Team : Color;
Coordinate : Integer;
end record;
procedure Move_Piece(Board: in Vector; P: in Piece; Move_To: in Integer);
end Chess_Types;
More Code for question in comments:
Chess_Types.Piece_Types.ads:
package Chess_Types.Piece_Types is
type Pawn is new Piece with
record
First_Move : Boolean := True;
end record;
overriding
procedure Move_Piece(Board: in CB_Vector'Class; Po: in Pawn; Move_To: in Board_Index);
-- Other piece types declared here
end Chess_Types.Piece_Types;
Chess_Types.Piece_Types.adb:
with Ada.Text_IO;
use Ada.Text_IO;
package body Chess_Types.Piece_Types is
procedure Move_Piece(Board: in CB_Vector'Class; Po: in Pawn; Move_To: in Board_Index) is
Index_From, Index_To : Board_Index;
Move_From : Board_Index := Po.Coordinate;
begin
-- Obtain locations of Pawn to move from (Index_From) and to (Index_To)
-- in terms of the single dimension vector
for I in Board.First_Index .. Board.Last_Index loop
if Board.Element(I).Coordinate = Move_From then
Index_From := I;
end if;
if Board.Element(I).Coordinate = Move_To then
Index_To := I;
end if;
end loop;
-- Determine if the requested move is legal, and if so, do the move.
-- More possibilties to be entered, very primitive for simple checking.
if Move_To - Move_From = 2 and then Po.First_Move = True then
Board.Swap(I => Index_From, J => Index_To); -- "actual for "Container" must be a variable"
Board.Element(Index_From).First_Move := False; -- "no selector for "First_Move" for type "Piece'Class"
elsif Move_To - Po.Coordinate = 1 then
Board.Swap(Index_From, Index_To); -- "actual for "Container" must be a variable"
end if;
-- Test to make sure we are in the right Move_Piece procedure
Put_Line("1");
end Move_Piece;
-- Other piece type move_piece procedures defined here
end Chess_types.Piece_Types;
As a note to understand further, the Coordinate component of each piece correspond to ICCF numeric notation, which is two digits, so there needs to be some type of conversion between the vector and the ICCF notation, hence the reason for the whole for loop at the start.
This is a tough one. It looks like limited with and generics don't play nice together. The only way to make it work is to go back to using your own access type:
with Ada.Containers.Vectors;
use Ada.Containers;
limited with Chess_Types;
use Chess_Types;
package Chessboard_Package is
subtype Board_Index is Integer range 1 .. 64;
type Piece_Acc is access all Piece'Class;
package Chessboard is new Vectors(Board_Index, Piece_Acc);
end Chessboard_Package;
I had to put the instantiation into a new package, and move the Board_Index there too. Also, I changed it to Vectors since Piece_Acc is a definite type and there's no point in using Indefinite_Vectors. But in any event, this defeats the purpose. I'm just not sure Ada gives you a way to do what you want with two packages like this.
Even doing it in one package is not easy:
with Ada.Containers.Indefinite_Vectors;
use Ada.Containers;
package Chess_Types is
subtype Board_Index is Integer range 1 .. 64;
type Color is (Black, White);
type Piece is tagged record ... end record;
type CB_Vector is tagged;
procedure Move_Piece (Board : in CB_Vector'Class;
P : in Piece;
Move_To : in Board_Index);
package Chessboard is new Indefinite_Vectors(Board_Index, Piece'Class);
type CB_Vector is new Chessboard.Vector with null record;
end Chess_Types;
This compiles, but I had to add extra stuff to get around some of the language rules (in particular, when you instantiate a generic, that "freezes" all prior tagged types so that you can no longer declare a new primitive operation of the type); also, I had to make the Board parameter a classwide type to avoid running into the rule about primitive operations of multiple tagged types.
As I understand it, this will do what you want.
with Ada.Containers.Indefinite_Vectors;
use Ada.Containers;
package Chess_Types is
subtype Board_Index is Integer range 1 .. 64;
type Color is (Black, White);
type Piece is abstract tagged
record
Name : String (1 .. 3) := " ";
Alive : Boolean := False;
Team : Color;
Coordinate : Board_Index;
end record;
type Piece_Ptr is access all Piece'Class;
package Chessboard is new Indefinite_Vectors(Board_Index, Piece_Ptr);
procedure Move_Piece (Board : in Chessboard.Vector;
P : in Piece'Class;
Move_To : in Board_Index) is abstract;
end Chess_Types;
NOTES:
Piece is now abstract, as is the Move_Piece method. This will mean you now need to derive your other piece types (package piece_type-rook.ads, with a move_piece method for rook) etc...
Chessboard now contains pointers (Class wide pointers), beware allocating, deallocating, deep copy, shallow copy issues when using it.
You should now be able to call Move_Piece on any dereference of a piece_ptr and have it dispatch to the correct method.
The Move_To parameter is now the same type as the Board_Index. (Coordinate also brought in line) -- this seems a bit clunky, perhaps rethink this. (Row & Column Indices defining a 2D array perhaps? --No need for Indefinite_Vectors)
To answer the second question in the comment:
To use First_Move, the procedure has to know that it's a Pawn. If the object is declared with type Piece'Class, you can't access components that are defined only for one of the derived types. (That's true in most OO languages.) This may indicate a flaw in your design; if you have a procedure that takes a Piece'Class as a parameter, but you want to do something that makes sense only for a Pawn, then maybe you should add another operation to your Piece that does a default action for most pieces (perhaps it does nothing) and then override it for Pawn. Other possibilities are to use a type conversion:
procedure Something (P : Piece'Class) is ...
if Pawn(P).First_Move then ...
which will raise an exception if P isn't a Pawn. If you want to test first, you can say "if P in Pawn". I sometimes write code like:
if P in Pawn then
declare
P_Pawn : Pawn renames Pawn(P);
begin
if P_Pawn.First_Move then ...
end;
end if;
But defining a new polymorphic operation is preferable. (Note: I haven't tested the above code, hope I didn't make a syntax error somewhere.)
I'm an absolute beginner in Ada and I'm trying to calculate sin(x) [sin(3) now] by using Taylor-series, but I just can't get it to work.
So here is my procedure:
with Ada.Float_Text_IO;
with Mat;
procedure SinKoz is
X:Float:=3.0;
Szamlalo:Float:=0.0;
begin
for I in 1..100 loop
Szamlalo := Szamlalo + ((-1.0)**I)*(X**(2.0*I+1.0))/Mat.Faktorialis(2*I+1);
end loop;
Ada.Float_Text_IO.Put( Szamlalo );
end SinKoz;
And inside Mat, here is my Faktorialis, which calculates the factorial of 2*I+1:
function Faktorialis( N: Float ) return Float is
Fakt : Float := 1.0;
begin
for I in 1..N loop
Fakt := Fakt * I;
end loop;
return Fakt;
end Faktorialis;
When i'm trying to compile my code, this error comes up:
exponent must be of type Natural, found type "Standard.Float"
I hope you can help me trying to figure out what went wrong with my types!
The first question is : do you need to raise X to a non-integer power?
It looks to me as if you don't : in which case replace X**(2.0*I+1.0) with X**(2*I+1) and all will be well.
But if you really do (perhaps not here, but in another application) you just need to make such an operator visible : there's one for Float in the package Ada.Numerics.Elementary_Functions so precede your function with
with Ada.Numerics.Elementary_Functions;
use Ada.Numerics.Elementary_Functions;
and it should work as written.
Finally, if you have created your own float type, you can instantiate the generic package Ada.Numerics.Generic_Elementary_Functions with your type as its parameter, to create a set of these functions specifically for your type.
Gotta love Ada's strong typing.
Off the top of my head, I suspect your problem may be this line:
Szamlalo := Szamlalo + ((-1.0)**I)*(X**(2.0*I+1.0))/Mat.Faktorialis(2*I+1);
2.0*I+1.0 is going to return a Float. Not a Natural. You could try wrapping that in Integer() or Natural() (Natural is a subtype of Integer) and see if that helps.