Ada Datatypes:
integers: signed integers, unsigned integers(modular)
enumerations
real numbers: floating point, fixed point (ordinary fixed point, decimal fixed point)
array and strings
access type (pointers)
records
enumeration type (including boolean) ==> Ada.Text_IO.ENUMERATION_IO() (type, is)
signed integers ==> Ada.Text_IO.INTEGER_IO() (type, is, range)
modular integers (unsigned integers) ==> Ada.Text_IO.MODULAR_IO() (type, is, mod)
------------------
floating point ==> Ada.Text_IO.FLOAT_IO() (type, is, digits, range)
ordinary fixed point ==> Ada.Text_IO.FIXED_IO() (type, is, delta, range)
decimal fixed point ==> Ada.Text_IO.DECIMAL_IO() (type, is, delta, digits, range)
-----------------
array/strings - type, is, array, of
record - type, is, record, end
access - type, is, access
-------------------
unconstrained array, functions with array as parameters, strings
-----------------
Signed integers:
with Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Text_IO;
with Ada.Integer_Text_IO;
procedure Hello is
type int_type is range 1 .. 10; -- user defined signed int
package int_IO is new Ada.Text_IO.Integer_IO(int_type); -- package for our user defined signed integer type int_type
use int_IO;
a: int_type; -- a range is 1 to 10
begin
a := 10;
put(a);
end Hello;
with Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Text_IO;
with Ada.Integer_Text_IO;
procedure Hello is
type int_type is range -10 .. 10; -- user defined signed int
package int_IO is new Ada.Text_IO.Integer_IO(int_type); -- package for our user defined signed integer type int_type
use int_IO;
a: int_type; -- a range is -10 to 10
begin
a := -10;
put(a);
end Hello;
with Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Text_IO;
with Ada.Integer_Text_IO;
procedure Hello is
type int_type is range -100 .. -10; -- user defined signed int
package int_IO is new Ada.Text_IO.Integer_IO(int_type); -- package for our user defined signed integer type int_type
use int_IO;
a: int_type; -- a range is -100 to -10
begin
a := -20;
put(a);
end Hello;
Unsigned Integers (modular datatype)
with Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Text_IO;
with Ada.Integer_Text_IO;
procedure Hello is
type unsigned_int_type is mod 10; -- user defined unsigned integer (range 0 to 9)
package unsigned_int_IO is new Ada.Text_IO.Modular_IO(unsigned_int_type); -- package for our user defined unsigned integer type unsigned_int_type
use unsigned_int_IO;
a: unsigned_int_type; -- a range is 0 to 9
begin
a := 10;
put(a);
end Hello;
with Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Text_IO;
with Ada.Integer_Text_IO;
procedure Hello is
type unsigned_int_type is mod 100; -- user defined unsigned integer (range 0 to 99)
package unsigned_int_IO is new Ada.Text_IO.Modular_IO(unsigned_int_type); -- package for our user defined unsigned integer type unsigned_int_type
use unsigned_int_IO;
a: unsigned_int_type; -- a range is 0 to 99
begin
a := 99;
put(a);
end Hello;
enumeration type
//c
#include <stdio.h>
int main (void)
{
enum DAYS {mon, tue, wed, thu, fri, sat, sun};
enum DAYS d;
d = fri;
printf("day is %d\n",d);
if(d==sat || d==sun)
{
printf("its weekend\n");
}
else
{
printf("its weekday\n");
}
return 0;
}
//ada
with Ada.Text_IO;
use Ada.Text_IO;
procedure Hello is
type Days_type is (mon, tue, wed, thu, fri, sat, sun); -- user defined enumeration type
package Days_IO is new Ada.Text_IO.Enumeration_IO(Days_type); -- package for our user defined enumeration type Days_type
use Days_IO;
a: Days_type; -- a range is mon to sun
begin
a := fri;
put("day is ");put(a);new_line;
if a=sat or a=sun then
put_line("its weekend");
else
put_line("its weekday");
end if;
end Hello;
--floating point datatype:
with Ada.Text_IO;
use Ada.Text_IO;
procedure Hello is
type float_type is digits 4 range 0.0 .. 100.0; -- here digits 4 ndicates minimum numbers of decimal digits in mantissa
package float_type_IO is new Ada.Text_IO.Float_IO(float_type);
use float_type_IO;
f : float_type;
begin
f := 150.0;
Put("f= ");put(f);
end Hello;
ordinary fixed point type:
with Ada.Text_IO;
use Ada.Text_IO;
procedure Hello is
type ordinary_fixed_type is delta 0.0001 range 0.0 .. 5.0;
-- here delta 0.0001 is distance between model numbers and maximum absolute error is half of delta 0.0001
package ordinary_fixed_type_IO is new Ada.Text_IO.Fixed_IO(ordinary_fixed_type);
use ordinary_fixed_type_IO;
f : ordinary_fixed_type;
begin
f := 4.565566;
Put("f= ");put(f);
end Hello;
//decimal fixed point
with Ada.Text_IO;
use Ada.Text_IO;
procedure Hello is
type decimal_fixed_type is delta 0.001 digits 5 range 0.0 .. 99.0; -- delta 0.001 indicates 3 of 5 digits are on right of decimal point. digits 5 indicates a total of 5 decimal digits
package decimal_fixed_type_IO is new Ada.Text_IO.Decimal_IO(decimal_fixed_type);
use decimal_fixed_type_IO;
f : decimal_fixed_type;
begin
f := 4.56;
Put("f= ");put(f);
end Hello;
subtypes:
subtype Lowercase is Character range ‘a’ .. ‘z’;
subtype Negative is Integer range Integer’First .. -1;
type Day_Type is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);
subtype Weekday is Day_Type range Monday .. Friday;
subtype Weekend is Day_Type range Saturday .. Sunday;
type Pounds is digits 6 range 0.0 .. 1.0E+06;
subtype UPS_Weight is Pounds range 1.0 .. 100.0;
subtype FedEx_Weight is Pounds range 0.1 .. 1.0;
Total : Pounds;
Box : UPS_Weight;
Envelope : FedEx_Weight;
Total := Box + Envelope; -- Adding two different subtypes with same base type.
//arrays
#include <stdio.h>
int main()
{
float a[5];
int i;
a[0] = 10.5;
a[1] = 20.5;
a[2] = 30.5;
a[3] = 40.5;
a[4] = 50.5;
printf("Array elements are: ");
for(i=0;i<5;i++)
{
printf("%f ", a[i]);
}
return 0;
}
ada arrays:
with Ada.Text_IO;
with Ada.Float_Text_IO;
use Ada.Text_IO;
use Ada.Float_Text_IO;
procedure Hello is
type float_array_type is array(Integer range 0 .. 4) of float;
a : float_array_type;
begin
a(0) := 10.5;
a(1) := 20.5;
a(2) := 30.5;
a(3) := 40.5;
a(4) := 50.5;
put("Array elements are: ");
for count in 0 .. 4 loop
put(a(count));
end loop;
end Hello;
array index can start with 1 also in ada, index can start with any number
with Ada.Text_IO;
with Ada.Float_Text_IO;
use Ada.Text_IO;
use Ada.Float_Text_IO;
procedure Hello is
type float_array_type is array(Integer range 1 .. 5) of float;
a : float_array_type;
begin
a(1) := 10.5;
a(2) := 20.5;
a(3) := 30.5;
a(4) := 40.5;
a(5) := 50.5;
put("Array elements are: ");
for count in 1 .. 5 loop
put(a(count));
end loop;
end Hello;
//index can start with any number
with Ada.Text_IO;
with Ada.Float_Text_IO;
use Ada.Text_IO;
use Ada.Float_Text_IO;
procedure Hello is
type float_array_type is array(Integer range 5 .. 9) of float;
a : float_array_type;
begin
a(5) := 10.5;
a(6) := 20.5;
a(7) := 30.5;
a(8) := 40.5;
a(9) := 50.5;
put("Array elements are: ");
for count in 5 .. 9 loop
put(a(count));
end loop;
end Hello;
// array index can be of any discrete type
with Ada.Text_IO;
with Ada.Float_Text_IO;
use Ada.Text_IO;
use Ada.Float_Text_IO;
procedure Hello is
type float_array_type is array(Character range 'a' .. 'f') of float;
a : float_array_type;
begin
a('a') := 10.5;
a('b') := 20.5;
a('c') := 30.5;
a('d') := 40.5;
a('e') := 50.5;
put("Array elements are: ");
for count in Character range 'a' .. 'f' loop
put(a(count));
end loop;
end Hello;
//2D array
#include <stdio.h>
int main()
{
float a[3][2];
int i,j;
a[0][0] = 10.5;
a[0][1] = 20.5;
a[1][0] = 30.5;
a[1][1] = 40.5;
a[2][0] = 50.5;
a[2][1] = 60.5;
printf("Array elements are:\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf("%f ", a[i][j]);
}
printf("\n");
}
return 0;
}
//ada 2D arrays
with Ada.Text_IO;
with Ada.Float_Text_IO;
use Ada.Text_IO;
use Ada.Float_Text_IO;
procedure Hello is
type float_2D_array_type is array(Integer range 0 .. 2, Integer range 0 ..1) of float;
a : float_2D_array_type;
begin
a(0,0) := 10.5;
a(0,1) := 20.5;
a(1,0) := 30.5;
a(1,1) := 40.5;
a(2,0) := 50.5;
a(2,1) := 60.5;
put("Array elements are: ");new_line;
for i in 0 .. 2 loop
for j in 0 .. 1 loop
put(a(i,j));
end loop;
new_line;
end loop;
end Hello;
//array index can start with 1 also
with Ada.Text_IO;
with Ada.Float_Text_IO;
use Ada.Text_IO;
use Ada.Float_Text_IO;
procedure Hello is
type float_2D_array_type is array(Integer range 1 .. 3, Integer range 1 .. 2) of float;
a : float_2D_array_type;
begin
a(1,1) := 10.5;
a(1,2) := 20.5;
a(2,1) := 30.5;
a(2,2) := 40.5;
a(3,1) := 50.5;
a(3,2) := 60.5;
put("Array elements are: ");new_line;
for i in 1 .. 3 loop
for j in 1 .. 2 loop
put(a(i,j));
end loop;
new_line;
end loop;
end Hello;
//strings
#include <stdio.h>
int main()
{
char str[10] = "Eswar";
printf("string is %s\n", str);
return 0;
}
with Ada.Text_IO;
with Ada.Strings.Fixed;
use Ada.Text_IO;
use Ada.Strings.Fixed;
procedure Hello is
str : string := "Eswar";
begin
put("string is ");put_line(str);
end Hello;
//strings using constrained arrays concept
with Ada.Text_IO;
use Ada.Text_IO;
procedure Hello is
type String is array(Positive range <>) of Character;
subtype name_string is String(1..5);
str : name_string;
begin
str := "Eswar";
for i in str'range loop
put(str(i));
end loop;
end Hello;
pointers:
#include <stdio.h>
int main()
{
int a;
int *p;
p = &a;
*p = 42;
printf("%d",*p);
return 0;
}
//ada
with Ada.Text_IO;
with Ada.Integer_Text_IO;
use Ada.Text_IO;
use Ada.Integer_Text_IO;
procedure Hello is
type Integer_Ptr is access Integer; -- A pointer type that designates Integers
N : Integer_Ptr; -- can only designate Integer objects
begin
N := new Integer; -- obtain memory for an integer
N.all := 42; -- Assign 42 to the memory designated by N
put(N.all);
end Hello;
structures/records:
#include <stdio.h>
int main()
{
struct student_record
{
int rno;
float marks;
char grade;
};
struct student_record s;
s.rno = 1;
s.marks = 67.65;
s.grade = 'A';
printf("rno=%d\nmarks=%f\ngrade=%c\n",s.rno, s.marks, s.grade);
return 0;
}
//ada records
with Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.Float_Text_IO;
use Ada.Text_IO;
use Ada.Integer_Text_IO;
use Ada.Float_Text_IO;
procedure Hello is
type student_record is
record
rno: Integer;
marks : Float;
grade : Character;
end record;
s : student_record;
begin
s.rno := 1;
s.marks := 67.65;
s.grade := 'A';
put("rno= ");put(s.rno);new_line;
put("marks= ");put(s.marks);new_line;
put("grade= ");put(s.grade);new_line;
end Hello;