Your global information security experts

Search for Vulnerabilities via
The National Vulnerability Database

Programming in Multiple Languages


by Chris Hurley - 3/29/00

If you want to be a hacker, you are going to have to learn to program. Programming is rewarding, frustrating, and can be time consuming. Programming is planning, scheduling, or performing a task or event. A computer program is a sequence of steps for a computer to follow or a list of instructions to be performed by a computer. These definitions hold regardless of the programming language that you are using. This article shows how a basic program, one that can be very useful, is essentially the same in three distinct languages, Microsoft's Visual Basic, C++, and Perl.

An algorithm is a sequential procedure for solving a problem. The algorithm that I am going to discuss is one to remove duplicate elements from an array. An array is a structured collection of the same type of components, all with the same name and accessed by an index indicating the component's position in the collection. In our example, we will use an array of integers although the algorithm is the same regardless of the data type. Another important concept to grasp is that this is only ONE solution to this problem. There are thousands of ways to perform most tasks.

First, lets look at the Visual Basic code to accomplish removing duplicate elements from an array.

First we must declare the arrays. Here we declare 2 arrays named array1 and array2. (1 To 1000000) tells the Operating System that 1 million consecutive blocks of memory need to be set aside for this array to use. Finally, As Integer declares the data type of the arrays. In this case, we assume that array2 is a copy of array1

Dim array1(1 To 1000000) As Integer
Dim array2(1 To 1000000) As Integer

Next we declare i, j, and k. A generic declaration 'Dim without a data type leaves the data type open until the element is used. In this case i and j have no data type associated with them while k is an integer. Visual Basic is very flexible here as most languages will require a type declaration.

Dim i
Dim j
Dim k As Integer

Next is our loop. Looping or iteration is fundamental in all languages. We are using a 'FOR' loop. We assume that the variable k has been initialized to the index number of the final element of our array, in other words the size of the array. The For loop initialized i to the integer value 1. 'To k' means that as long as i has a value smaller than k the loop is to continue. Step 1 tells the program how much to increment i each time through the loop.

For i = 1 To k Step 1
For j = i + 1 To k Step 1 'This is an embedded loop.
If i = j Then 'This ensures that the indexes
j = j + 1 'of our two arrays aren't equal
End If
If array1(i) = 0 Then 'This checks to see if the element has
j = k 'been "blanked" out and if so moves on
End If

The next statement compares the element in array1 at index i to the element in array2 at index j.

If array1(i) = array2(j) Then
If these elements have the same value, set element at array2(j) to 0
array2(j) = 0
End If
Next
Next

Now lets look at that all together:

For i = 1 To k Step 1
For j = i + 1 To k Step 1
If i = j Then
j = j + 1
End If
If array1(i) = 0 Then
j = k
End If
If array1(i) = array2(j) Then
array2(j) = 0
End If
Next
Next

Now array2 contains one instance of each element of array1. Depending on what you want to do with the data, you can print it out to the screen or an external file. Or you can use another For loop and copy all of the elements of array2 that are not equal to 0 back into array1 or a 3rd array.

Now lets look at the same algorithm in C++.

First we have our declarations. Again we will assume that k has been initialized to the array size and array2 is a copy of array1

int array1[1000000];
int array2[1000000];
int i,j,k;

for (i=0;i&ltk;i++)
{
 for (j=i+1;j&ltk;j++)
 {
  if (i==j)
  j+=2; //this is C++ shorthand for j=j+2;
  if (array1[i]==0)
  j=k;
  if (array1[i]==array2[j])
  array2[j]=0;
 }
}


As you can see, the same basic algorithm was used in C++ as was used in Visual Basic. The difference was the language, not the logic.

Finally, we will see the same algorithm in Perl. We make the same assumptions that we made in the other languages.

##Declarations##
@array1 = (1,2,1,3,3,4);
@array2 = (1,2,1,3,3,4);

$i=0;
$j=0;
$k=6;
####################

for($i=0;$i<$k;$i++)
{
 for($j=$i+1;$j<$k;$j++)
 {
 if($i==$j){
  $j=$j+1;}
  if ($array1[$i]==0){
  $j=$k;}
  if($array1[$i] == $array2[$j]){
  $array2[$j]=0;}
 }
}

Once again, it is obvious that the algorithm is essentially the same, only slight linguistic modifications needed to be made. The actual code in Perl is very similar to C/C++ code. Furthermore, Perl is a great language for the beginner because it is freely distributed at Perl.com and is relatively easy to learn. I have written some simple example programs that institute each of the above examples. I have only uploaded the executable Remove.exe for the Visual Basic example, created with Microsoft Visual Basic 5.0. Because of the overhead involved in windows applications it was not feasible to upload the entire source.

The C++ version remove.cpp was written using Borland C++ 5.02. It can however be executed using any C++ compiler.

The Perl version remove.pl was written using the Win32 version of ActivePerl implementing Perl 5.6.0.613.

This algorithm is not restricted to these languages. The purpose of these examples was to show that with some simple modifications, an algorithm can be converted easily from one language to another. Please feel free to utilize this code, play with it, modify it, and improve on it. If I can be of any assistance, please email me.