Example of translation

 

 Z algorithm : Binary search in a table

   
LET
    Arr : ARRAY ( 10 ) ;
    Bi , Bs , M : INTEGERS ;
    Found : BOOLEAN ;
    V : INTEGER ;
BEGIN
    INIT_VECT ( Arr , [ 34 , 56 , 87 , 123 , 134 , 187 , 230 , 422 , 760 , 1034 ] ) ;
    V := 123 ;
    Bi := 1 ;
    Bs := 10 ;
    Found := FALSE ;
    WHILE ( Bi <= Bs ) AND NOT Found
        M := ( Bi + Bs ) / 2 ;
        IF ELEMENT ( Arr [ M ] ) = V
            Found := TRUE
        ELSE
            IF V < ELEMENT ( Arr [ M ] )
                Bs := M - 1
            ELSE
                Bi := M + 1
            ENDIF
        ENDIF
    ENDWHILE ;
    WRITE ( Found )
END
 

 

 the c equivalent program

 

 /**--------------------------------------------------------**/
/**           T r a n s l a t i o n Z to C (Standard)               **/
/**                           By Pr. D.E ZEGOUR                        **/
/**                                E S I - Algier                              **/
/**                             Copywrite 2014                           **/
/**---------------------------------------------------------**/

#include <stdio.h>
#include <stdlib.h>

typedef int bool ;

#define True 1
#define False 0

/** -Implementation- **\: ARRAY OF INTEGERS**/

/** Arrays **/

typedef int Typeelem_V10i ;
typedef Typeelem_V10i * Typevect_V10i ;

Typeelem_V10i Element_V10i ( Typevect_V10i V , int I1 )
{
return *(V + (I1-1) ) ;
}

void Ass_element_V10i ( Typevect_V10i V , int I1 , Typeelem_V10i Val )
{
*(V + (I1-1) ) = Val ;
}

/** For temporary variables **/
typedef Typeelem_V10i Typetab_V10i[10];


/** Variables of main program **/
Typevect_V10i Arr;
int Bi;
int Bs;
int M;
bool Found;
int V;
Typetab_V10i T_Arr;

/** High level operations **/


/** Initializing an array **/
void Init_vect_V10i ( Typevect_V10i T , Typetab_V10i Tab , int N)
{
int K=-1;
int I1 ;
for (I1=0; I1< 10; ++I1)
{
K++;
*(T + I1 ) = Tab [K] ;
}
}

    INIT_VECT ( Arr , [ 34 , 56 , 87 , 123 , 134 , 187 , 230 , 422 , 760 , 1034 ] ) ;
    V := 123 ;


int main(int argc, char *argv[])
{
Arr = malloc(10 * sizeof(int));
T_Arr [ 0 ] = 34 ;
T_Arr [ 1 ] = 56 ;
T_Arr [ 2 ] = 87 ;
T_Arr [ 3 ] = 123 ;
T_Arr [ 4 ] = 134 ;
T_Arr [ 5 ] = 187 ;
T_Arr [ 6 ] = 230 ;
T_Arr [ 7 ] = 422 ;
T_Arr [ 8 ] = 760 ;
T_Arr [ 9 ] = 1034 ;
Init_vect_V10i ( Arr , T_Arr , 10 ) ;
V = 123 ;
Bi = 1 ;
Bs = 10 ;
Found = False ;
while( ( Bi <= Bs ) && ! Found) {
M = ( Bi + Bs ) / 2 ;
if( Element_V10i ( Arr , M ) == V) {
Found = True; }
else
{
if( V < Element_V10i ( Arr , M )) {
Bs = M - 1; }
else
{
Bi = M + 1;
}
}
} ;
printf ( " %d", Found );

system("PAUSE");
return 0;
}