Wednesday, October 21, 2020

Is Ada truly verbose?

 

People who prefer programming language syntax derived from the C language have often argued that the Ada language is verbose because it uses entire words rather than punctuation.

Meaning

C Syntax

Ada Syntax

Begin a block

{

begin, loop

End a block

}

end, end if, end loop, end subprogram name, end task name, end protected object name, end record

Declare a function taking a single integer parameter and returning an integer value

int foo (int x);

function foo (x : integer) return integer;

Declare a 10 element integer array indexed with the values 0 through 9

int a [10];

a : array (0..9) of integer;

Write a “for” loop to sum all the elements in array a declared above.

for (int I = 0; I < 10; i++)

{

   sum += a [i];

}

for I in a’range loop

   sum := sum + a (i);

end loop;

 

Of course, not everything in Ada takes more lines of code or more typing than the equivalent program in C or C++. For instance, the following C++ program is taken from the CodesCracker website. Its purpose is to convert a user input of a hexadecimal value into the corresponding decimal value.

/* C++ Program - Hexadecimal to Decimal Conversion */            

#include<iostream.h>

#include<stdlib.h>

#include<conio.h>

#include<math.h>

unsigned long convtodecnum(char hex[]);

void main()

{

    clrscr();

    unsigned long decnum;

    char hex[9];     // 8 characters for 32-bit Hexadecimal Number and one for ' '   

    cout<<" Enter 32-bit Hexadecimal Number : ";

    cin>>hex;

    decnum = convtodecnum(hex);

    cout<<"Value in Decimal Number is "<<decnum<<"\n";

    getch();

}

unsigned long convtodecnum(char hex[])

{

    char *hexstr;

    int length = 0;

    const int base = 16;     // Base of Hexadecimal Number

    unsigned long decnum = 0;

    int i;

    // Now Find the length of Hexadecimal Number

    for (hexstr = hex; *hexstr != '\0'; hexstr++)

    {

       length++;

    }

    // Now Find Hexadecimal Number

    hexstr = hex;

    for (i = 0; *hexstr != '\0' && i < length; i++, hexstr++)

    {

       // Compare *hexstr with ASCII values

       if (*hexstr >= 48 && *hexstr <= 57)   // is *hexstr Between 0-9

       {

           decnum += (((int)(*hexstr)) - 48) * pow(base, length - i - 1);

       }

       else if ((*hexstr >= 65 && *hexstr <= 70))   // is *hexstr Between A-F

       {

           decnum += (((int)(*hexstr)) - 55) * pow(base, length - i - 1);

       }

       else if (*hexstr >= 97 && *hexstr <= 102)   // is *hexstr Between a-f

       {

           decnum += (((int)(*hexstr)) - 87) * pow(base, length - i - 1);

       }

       else

       {

           cout<<"Invalid Hexadecimal Number \n";

       }

    }

    return decnum;

}

 

An Ada program handling the same input and results in the same output is:

-- Convert Hexadecimal string to decimal value

with Ada.Text_IO; use Ada.Text_IO;

procedure Main is

   type Unsigned_Long is mod 2**32;

   Decimal : Unsigned_Long;

begin

   Put ("Enter a 32 bit hexadecimal number: ");

   Decimal := Unsigned_Long'Value ("16#" & Get_Line & "#");

   Put_Line (Decimal'Image);

exception

   when Constraint_Error =>

      Put_Line ("Input value is not a valid 32 bit hexadecimal number.");

end Main;

 

Ada has the built-in capability to handle literal numbers represented by base 2 through base 16. The base 16 value FF is represented as 16#FF#. Ada accepts either upper or lower case representation of the hexadecimal values A through F. The program simply appends the 16# to the entered hexadecimal digits and then appends # to the end of the string. The integer’value built in attribute converts a string representation of an integer to its corresponding integer value.

The message created in response to the exception Constraint_Error is raised if the user enters a value more than 2^32 or if one of the digits is not in the range of 0 through 9 or A through F.

In this case the Ada program appears to be far less verbose than the C++ program.

No comments:

Post a Comment

Comparison of three algorithms for summing an array of integers

This article shares a comparison of the time taken to sum an array of integers. This article tests three algorithms for summing the array. •...