Friday, September 30, 2011

Array Searching Algorithms in C

1.Linear Search
Description:
An iterator traverses sequentially the array from left to right. When it encounters an element which is equal to the key the search stops and the index of that element is returned. If the key is not present in the array the size of the array will be returned.
Usage:
It's mostly used when one has no information about the array.
Complexity:
O(n)
Example:
/*
 * Description:
 *  Returns the index of key in array
 * Parameters:
 *  key - the element who is searched for
 *  array - the array who is searched
 *  arraySize - the size of array
 * Returns:
 *  The index of key if key exists in the array.
 *  Otherwise returns arraySize.
 */
int ArraySearch_Linear(int key, int array[], int arraySize)
{
   int i = 0;
   for(i=0;i<arraySize && array[i]!=key; i++);
   return i;
}
2.Sentinel Linear Search
Description:
The algorithm is very similar to the linear search with the exception that the loop invariant is reduced by assigning the value of the key to the last element of the array.
Usage:
It's mostly used when one has no information about the array.
Complexity:
O(n)
/*
 * Description:
 *  Returns the index of key in array
 * Parameters:
 *  key - the element who is searched for
 *  array - the array who is searched
 *  arraySize - the size of array
 * Returns:
 *  The index of key if key exists in the array.
 *  Otherwise returns arraySize.
 */
int ArraySearch_Sentinel(int key, int array[], int arraySize)
{
   int i = 0;
   array[arraySize] = key;
   for(i=0; array[i]!=key; i++);
   return i;
}
3.Binary Search
Description:
The array is divided successively into two parts at each iteration according to the value of the key and the value of the pivot (which is initialized to half of the length of the array).
Usage:
It can only be used when the array is already sorted.
Complexity:
O(log2n)
/*
 * Preconditions:
 *  The array must be sorted
 * Description:
 *  Returns the index of key in array
 * Parameters:
 *  key - the element who is searched for
 *  array - the array who is searched
 *  arraySize - the size of array
 * Returns:
 *  The index of key if key exists in the array.
 *  Otherwise returns arraySize.
 */
int ArraySearch_Binary(int key, int array[], int arraySize)
{
   int left=0;
   int right=arraySize-1;
   int middle = (left+right)/2;
   while(array[middle]!=key && left<=right)
   {
      (key>array[middle])?(left=middle+1):(right=middle-1);
      middle = (left+right)/2;
   }
   return (array[middle]!=key)?(arraySize):(middle);
}
4.Improved Binary Search
Description:
This algorithm brings an improvement over the binary search by removing a condition from the loop invariant (there is no longer needed to check if the pivot is equal to the key the pivot will be calculated differently).
Usage:
It can only be used when the array is already sorted.
Complexity:
O(log2n)
/*
 * Preconditions:
 *  The array must be sorted
 * Description:
 *  Returns the index of key in array
 * Parameters:
 *  key - the element who is searched for
 *  array - the array who is searched
 *  arraySize - the size of array
 * Returns:
 *  The index of key if key exists in the array.
 *  Otherwise returns arraySize.
 */
int ArraySearch_ImprovedBinary(int key, int array[], int arraySize)
{
   int left=0;
   int right=arraySize;
   int middle = (left+right)/2;
   while(left<right)
   {
      (key>array[middle])?(left=middle+1):(right=middle);
      middle = (left + right)/2;
   }
   return ((right>=arraySize)||(right<arraySize && array[right]!=key))
          ?arraySize
          :right;
}
5.Interpolation Search
Description:
This algorithm brings another improvement over the binary search by changing the way the pivot is calculated.
Usage:
It can only be used when the array is already sorted and the differences between elements are somewhat constant.
Complexity:
O(log2n)
/*
 * Preconditions:
 *  The array must be sorted
 * Description:
 *  Returns the index of key in array
 * Parameters:
 *  key - the element who is searched for
 *  array - the array who is searched
 *  arraySize - the size of array
 * Returns:
 *  The index of key if key exists in the array.
 *  Otherwise returns arraySize.
 */
int ArraySearch_Interpolation(int key, int array[], int arraySize)
{
   int left=0;
   int right=arraySize-1;
   int pivot=arraySize;
   if(key<=array[right] && key>=array[left])
   {
      do
      {
         pivot=left+(key-array[left])*(right-left)/(array[right]-array[left]);
        (key>array[pivot])?(left=pivot+1):(right=pivot-1);
      }while(array[pivot]!=key && left<right && array[left]!=array[right]
             && key>=array[left] && key<=array[right]);
   }
   return pivot;
}

Useful links
Array Searching Visualisation Applet

Thursday, September 29, 2011

Radix Conversion in C

In the article Formatted Strings in C I've given a few examples on how to convert between hexadecimal, octal and decimal.

Still, by using the methods described in that article you cannot convert directly to binary or a more "exotic" radix.

1.Converting to radix

Description:
A solution lays in the function char* itoa(int inputValue, char *result, int radix). This function allows you to take a number in decimal, octal or hexadecimal and convert it to a string containing the number's representation in a radix of your choosing.

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

int main(void)
{
   int number = 16;
   char radix2[10], radix3[10], radix12[10];
   itoa(number,radix2,2);
   itoa(number,radix3,3);
   itoa(number,radix12,12);
   printf("\nBinary  : %s"
          "\nRadix 3 : %s"
          "\nRadix 12: %s",radix2, radix3,radix12);
   return 0;
}
/*Output:
Binary  : 10000
Radix 3 : 121
Radix 12: 14
 */
Pitfall:
The function is not ANSI, so it will not available on all compilers. Still, you can implement it by yourself. Check the useful links at the end of the article.

Observation:
This function was present in most of the old compilers (ex. Borland C) and some regard it as deprecated.

2.Converting from radix

Description:
Another option for radix conversion is the ANSI function long int strtol ( const char * str, char * endptr, int radix). The function takes as arguments:
str - a string containg numbers in the radix radix
endptr - a pointer who shall memorize the position of last character after the conversion
radix - the radix of the number who will be converted to radix 10

Simple example:
#include<stdio.h>
#include<stdlib.h>

int main(void)
{
   long x = strtol("zzzz",NULL,36);
   printf("%ld\n",x);
   return 0;
}
/*Output:
1679615
 */
In the example above, we convert the number zzzz (represented in radix 36) to a number in radix 10.

Advanced example:
#include<stdio.h>
#include<stdlib.h>

int main(void)
{
   char buffer[] = "2050 40a0b0 -110111 zzzz";
   char * ptr_end = NULL;
   long int d1, d2, d3, d4;
   d1 = strtol (buffer,&ptr_end,10);
   d2 = strtol (ptr_end,&ptr_end,16);
   d3 = strtol (ptr_end,&ptr_end,2);
   d4 = strtol (ptr_end,NULL,36);
   printf ("%ld, %ld, %ld, %ld\n", d1, d2, d3, d4);
   return 0;
}
/*Output:
2050, 4235440, -55, 1679615
 */
In this example we take full advantage of the function's capabilities. The function will use ptr_end to return a pointer after a conversion to the end of the substring containing the number who was converted.

3.Implementing a radix to radix conversion function
/*
 * Description:
 *  Performs a radix conversion
 * Parameters:
 *  oldNumber - a string containing the number who will be converted
 *  oldradix - the base of number
 *  newNumber - a string who shall contain the value of number in
 *              the radix newBase
 *  newRadix - the radix to which number will be converted
 * Preconditions:
 *  The parameters base and newBase must be between 1 and 36
 *  The number arguments can only contain '0' - '9' and 'A'-'Z' chars
 *  The number string must represent correctly the number in base "base"
 * Postcondtions
 *  The function returns:
 *      1 - Success
 *      0 - Failure because bases were not correct
 *      -1 - Failure because newNumber incorrect format
 *
 */
int DecimalRadixConvert(const char* oldNumber, int oldRadix,
                        char* newNumber, int newRadix)
{
    long data = 0L;
    char digit = 0;
    int i = 0;
    int size=strlen(oldNumber);
    char* reverseNew = NULL;
    /*Checks if base if within bounds*/
    if((oldRadix<=1 || oldRadix>36)||(newRadix<=1 || newRadix>36))
    {
        return 0;
    }
    /*Convert to base 10 from old base*/
    for(i=0;i<size;i++)
    {
        if(oldNumber[i]>='0' && oldNumber[i]<='9')
        {
            data+=(oldNumber[i]-'0')*pow(oldRadix,size-i-1);
        }
        else if(oldNumber[i]>='A' && oldNumber[i]<='Z')
        {
            data+=(oldNumber[i]-'A' + 10)*pow(oldRadix,size-i-1);
        }
        else
        {
            return -1;
        }
    }
    i=0;
    /*Convert from base 10 to new base*/
    while(data>0)
    {
        digit = data % newRadix;
        (digit<10)?(newNumber[i] = digit + '0')
                  :(newNumber[i] = digit + 'A' -10);
        data=data/newRadix;
        i++;
    }
    newNumber[i]='\0';
    /*Reverses newNumber*/
    reverseNew = (char*)(malloc(sizeof(char)*strlen(newNumber)));
    size = strlen(newNumber);
    for(i=0; i<size; i++)
    {
        reverseNew[i] = newNumber[size-1-i];
    }
    reverseNew[size]='\0';
    newNumber = reverseNew;
    return 1;
}
The function above converts number to radix 10 and then converts the result the the specified newRadix radix.

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

int DecimalRadixConvert(const char* oldNumber, int oldRadix,
                           char* newNumber, int newRadix);

int main(void)
{
   char data[15]="1233";
   char data2[15];
   /*Converts from base 5 to base 3*/
   DecimalRadixConvert(data,5,data2,3);
   printf("\nRadix 3: %s\nRadix 5: %s",data2,data);
   return 0;
}
/*Output:
Radix3: 20222
Radix5: 1233
 */
Useful links:
itoa implementation
Radix Conversion Applet

Creating a Menu-Based Text User Interface in ANSI C

The easiest interface one can build (and the most used by console apps) is the Text User Interface. A Text User Interface is usually based on a menu which provides the user with various options.
Below, you have an example of how to implement such a interface using ANSI C:
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

int main(void)
{
    //Variable used for reading the user input
    char option;
    //Variable used for controlling the while loop
    bool isRunning = true;

    while(isRunning==true)
    {
        //Clears the screen
        system("clear");        //For UNIX-based OSes

        //Clears the keyboard buffer
        fflush(stdin);
        //Outputs the options to console
        puts("\n[1]Option1"
             "\n[2]Option2"
             "\n[3]Option3"
             "\n[4]Option4"
             "\n.........."
             "\n[x]Exit");
        //Reads the user's option
        option = getchar();
        //Selects the course of action specified by the option
        switch(option)
        {
            case '1':
                     //TO DO CODE
                     break;
            case '2':
                     //TO DO CODE
                     break;
            case '3':
                     //TO DO CODE
                     break;
            case '4':
                     //TO DO CODE
                     break;
            //...
            case 'x':
                     //Exits the system
                     isRunning = false;
                     return 0;
            default :
                     //User enters wrong input
                     //TO DO CODE
                     break;
        }
    }
    return 0;
}
As you probably observed, this is a general template for a TUI, but you can easily modify it and adapt it to use in your programs.

TIPI used for clearing the console the system("clear") command which works for Unix-based operating systems. If you want to adapt the program for a Windows OS, you should replace the command with system("cls").

Question: In which situations do you prefer creating a text user interface instead of graphical one?

References:
About the TUI

Tuesday, September 27, 2011

Formatted Strings in ANSI C

A formatted element can be defined as:

%[flags][width][.precision][length]specifier

The elements between between square brackets are optional.

1. Flags

-   Uses left justification instead of right justification.
Example:
#include<stdio.h>

int main(void)
{
   printf("Right justification: %10d\n",42);
   printf("Left justification: %-10d\n",42);
   return 0;
}
/*Output:
Right justification:         42
Left justification: 42
*/
 Forces the display of the number's sign (normally, the plus sign is not displayed, only the minus sign being displayed by default).
Example:
#include<stdio.h>

int main(void)
{
   printf("%+d\n",10);
   printf("%d\n", 10);
   return 0;
}
/*Output
+10
10
*/
blank (space) - If the number is positive, there will be a space placed before the number is formatted
Example:
#include<stdio.h>

int main(void)
{
   printf("% d",10);
   return 0;
}
/*Output
 10
*/
# -If used with o,x or X the result will be preceded by 0, 0x or 0X. If used with e, E,f, g,G the result contain a decimal point, even if there are no digits after him.
Example:
#include<stdio.h>

int main(void)
{
   printf("Octal                   : %#o\n",10);
   printf("Hexadecimal             : %#x\n",10);
   printf("Hexadecimal             : %#X\n",10);
   printf("Float                   : %#f\n",10.0f);
   printf("Scientific lowercase    : %#e\n",10.0f);
   printf("Scientific uppercase    : %#E\n",10.0f);
   printf("Shortest form lowercase : %#g\n",10.0f);
   printf("Shortest form uppercase : %#g\n",10.0f);
   return 0;
}
/*Output
Octal                   : 012
Hexadecimal             : 0xa
Hexadecimal             : 0XA
Float                   : 10.000000
Scientific lowercase    : 1.000000e+01
Scientific uppercase    : 1.000000E+01
Shortest form lowercase : 10.0000
Shortest form uppercase : 10.0000
*/
0 - Left-pads the number with zeroes (0) instead of spaces when padding it specified.
Example:
#include<stdio.h>

int main(void)
{
   printf("%010d",10);
   return 0;
}
/*Output
0000000010
*/
2. Width
The width represents the minimum number of characters. If the formatted element's size is smaller than the specified width, the element is left-padded with blanks (spaces). It's good to know that if the element has its size greater than the specified width, he will not be truncated.
Example:
#include<stdio.h>

int main(void)
{
   printf("%1d will not be truncated\n",10000);
   printf("%15d\n",10000);
   return 0;
}
/*Output
10000 will not be truncated
          10000
*/

If the width is not specified in the format string (".*"), an additional integer value is needed as an argument before.
Example:
#include<stdio.h>

int main(void)
{
   printf("The meaning of life is %*d", 5, 42);
   return 0;
}
/*Output
The meaning of life is     42
*/
3. Precision
For decimal data types specifiers (d,i,o,u,x,X), precision specifies the minimum number of digits to be written. If the value to be written is shorter when compared to the precision value, the result is padded with zeros. Otherwise (if the value is longer when compared to the precision value), the result is not truncated.

For the character type (c), it has no effect.
Example:
#include<stdio.h>

int main(void)
{
   printf("\n%1.1c will not be affected",'I');
   return 0;
}
/*Output
I will not be affected
*/
For floating point data types specifiers (e,E,f, g, G) the precision represents the number of digits to be printed after the decimal point.
Example:
#include<stdio.h>

int main(void)
{
   printf("\n%1.1f",3.141f);
   printf("\n%1.1e",3.141f);
   printf("\n%1.1E",3.141f);
   printf("\n%1.1g",3.141f);
   printf("\n%1.1G",3.141f);
   return 0;
}
/*Output
3.1
3.1e+00
3.1E+00
3.1
3.1
*/
For strings (s), the precision represents the maximum number of characters to be written.
Example:
#include<stdio.h>

int main(void)
{
   printf("\n%1.3s","The bird is the word");
   return 0;
}
/*Output
The
*/
When the precision is not specified, it's considered 0.

4. Length
h - the argument is interpreted as short (available for d,i,o,u,x,X)
l - the argument is interpreted as long (for d,i,o,u,x,X). For the specifiers c or s, the character/string will become wide.
L - the argument is interpreted as long (for e,E,f,g,G)
    5. Specifiers
    • Common specifiers
      • c - outputs an character (char)
      • d or i - outputs an signed integer (int)
      • u - outputs an unsigned integer (unsigned int)
      • f - outputs an floating point number (float)
      • s - outputs an string (char[] or char*)
      • p - outputs an pointer address
    • Less common specifiers
      • e - outputs a number in the mantise/exponent form using e (example: 3e023)
      • E - outputs a number in the mantise/exponent form using E (example: 5E-23)
      • g - outputs the shortest form between %f and %e (as in number of characters)
      • G - outputs the shortest form between %f and %E (as in number of characters)
      • n - does not output the argument
    • Base-conversion specifiers
      • o - outputs an unsigned decimal in base-8 (octal base)
      • x - outputs an unsigned decimal in base-16 (hexadecimal base) using the x character
      • X - outputs an unsigned decimal in base-16 (hexadecimal base) using the X character
    Example:
    #include<stdio.h>
    
    int main(void)
    {
       printf("Character              : %c  \n",'a');
       printf("Integer                : %d  \n",32);
       printf("Short integer          : %hd \n",32);
       printf("Long integer           : %ld \n",32L);
       printf("Unsigned integer       : %u  \n",32U);
       printf("Short unsigned integer : %hu \n" 32U);
       printf("Long unsigned integer  : %lu \n",32UL);
       printf("Float                  : %f  \n",32.0f);
       printf("Double                 : %f  \n",32.0);
       printf("Long double            : %Lf \n",32.0L);
       printf("String                 : %s  \n","word");
       return 0;
    }
    /*Output
    Character            : a
    Integer              : 32
    Short integer        : 32
    Long integer         : 32
    Unsigned integer     : 32
    Long unsigned integer: 32
    Float                : 32.000000
    Double               : 32.000000
    Long double          : 32.000000
    String               : word
    */
    6.Special characters
    \n - newline (end current line)
    \t - horizontal tab (inserts tab, leaving blank space)
    \v - vertical tab  (ends current line and continues on the next line at the same position)
    \b - backspace (deletes last character)
    \r - carriage return (returns the cursor at the beginning of the line)
    \f - form-feed (creates a page break for electronic documents. It is seldom used in modern environments)
    \a - alert
    \\ - backslash
    \? - question mark
    \' - single quotation mark
    \" - double quotation mark
    \ooo - octal number
    \xhhh - hexadecimal number
    \0 - null character
    Example:
    #include<stdio.h>
    
    int main(void)
    {
        /*The text who will follow this call will be on a new line*/
        printf("The bird is the word\n");
        /*Inserts a horizontal tab before writing the text*/
        printf("\tThe bird is the word");
        /*Ends the current line and continues writing at the same position*/
        printf("\vThe bird is the word\n");
        /*Deletes the 'r' character from word*/
        printf("The bird is the wor\bd\n");
        /*Returns the cursor the begining of the line*/
        printf("The bird is the word\rBird bird bird!");
        /*Produces an buzz-like sound*/
        printf("\nAlert\a\n");
        /*Backslash, question mark and single/double quotation marks require
        a backslash in order to be interpreted corectly*/
        printf("\\This is a backslash.Do you have any questions\?\n");
        printf("I've heard \"The bird\" is the \'word\'");
        return 0;
    }
    /*Output
    The bird is the word
     The bird is the word
                                The bird is the word
    The bird is the wod
    Bird bird bird! word
    Alert
    \This is a backslash.Do you have any questions?
    I've heard "The bird" is the 'word'
    */

    QuestionWhat tips & tricks do you know related to formatted strings?

    References:
    http://www.codingunit.com/printf-format-specifiers-format-conversions-and-formatted-output

    Sunday, September 25, 2011

    Reading Command Line Arguments in Java

    In console applications, arguments sometimes play an important role. They may be used to set the program's behavior according to external (as in external to the program) variables. These external variables can be sent to the program as strings which the program interprets as a String vector.
    public class CmdLineProgram 
    {
        /*
         * Precondition:
         * The program knows the minimum and maximum number of arguments
         * which he allows 
         */
        public static void main(String[] args)
        {
            int minNrOfArgs = 0;    //Minimum number of arguments
            int maxNrOfArgs = 2;    //Maximum number of arguments
            try
            {
                if(args.length >= minNrOfArgs && args.length <=maxNrOfArgs)
                {
                    if(args.length!=0)
                    {
                        System.out.println("The arguments are: ");
                        for(String s: args)
                            System.out.println(s);
                    }
                    System.out.println("Total number of arguments: " 
                                      + args.length);
                }
                else
                {
                    if(args.length <= minNrOfArgs)
                        throw new Exception("Number of arguments is "
                                            + args.length
                                            + ".\n" 
                                            + "At least "
                                            + minNrOfArgs
                                            + " are required.");
                    else
                        throw new Exception("Number of arguments is "
                                            + args.length
                                            + ".\n" 
                                            +"At most "
                                            + minNrOfArgs
                                            + " are permitted.");
                }
            }
            catch(Exception e)
            {
                System.out.println(e.getMessage());
                System.exit(0);
            }
        }
    }
    
    What the program does:
    Knowing the minimum and maximum number of arguments, the program checks if the length of the argument String vector (args) is between those values. If that is true, it will output the number of arguments and the value of each argument using a foreach loop (still, he will not output the arguments if he didn't receive any and will only show 0).
    If the number of arguments is not correct (too many of too few) it will throw an exception and output that exception.

    Friday, September 23, 2011

    Configuring Gamma Correction in Linux using xgamma

    You can easily change the gamma correction factor in Linux by using the following command
    xgamma -gamma newValue
    The newValue signifies the new gamma correction factor. It should be around 0.5 - 1. You can use higher or lower values than 0.5-1, but you may end up with much too bright or much too dark screen (the minimum value which the script accepts is 0.1 and the maximum value is 10 - I wouldn't recommend trying those).

    The best way to find the ideal gamma value for your monitor is to query first the xgamma value:
    xgamma
    After querying the value, increase the factor with 0.1 if you want your screen brighter or decrease it with 0.1 if you want your screen darker. Do it until you get the value you seek.

    If you want to a fine tunning you can modify the red, green or blue component:
    xgamma -rgamma rVal -ggamma gVal -bgamma bVal
    
    The testing method should remain the same as the one described above. Increasing/Decreasing the values too much can leave with an interesting (as in not so great) view.

    Useful links:
    xgamma man

    Thursday, September 1, 2011

    The Dynamic Multidimensional Arrays Library

    The Dynamic Multidimensional Arrays Library provides functions for the allocation and deallocation of arrays, matrices, cubes and hypercubes.

    In order to pass any kind of data type to the functions, we shall pass the array to the functions using a void pointer. Also, for each allocation function, the size of an array's element must be passed.

    The prototypes for the functions are:
    /*
     * Abstract:
     *  Allocates a 1D array
     * Parameters:
     *  vector - a pointer to the 1D array
     *  xsize - the size of the array
     *  elementSize - the size of one element
     * Returns:
     *  A pointer to the allocated array
     */
    void* NAlloc1D(void* vector, size_t xsize, size_t elementSize);
    /*
     * Abstract:
     *  Allocates a 2D array
     * Parameters:
     *  vector - a pointer to the 2D array
     *  xsize - the x size of the array (real of rows)
     *  ysize - the y size of the array (real of columns)
     *  elementSize - the size of one element
     * Returns:
     *  A pointer to the allocated array
     */
    void** NAlloc2D(void **vector, size_t xsize, size_t ysize, size_t elementSize);
    /*
     * Abstract:
     *  Allocates a 3D array
     * Parameters:
     *  vector - a pointer to the 3D array
     *  xsize - the x size of the array (real of rows)
     *  ysize - the y size of the array (real of columns)
     *  zsize - the z size of the array (real of tables)
     *  elementSize - the size of an array element
     * Returns:
     *  A pointer to the allocated array
     */
    void*** NAlloc3D(void ***vector, size_t xsize, size_t ysize, size_t zsize,
            size_t elementSize);
    /*
     * Abstract:
     *  Allocates a 4D array
     * Parameters:
     *  vector - a pointer to the 4D array
     *  xsize - the x size of the array (real of rows)
     *  ysize - the y size of the array (real of columns)
     *  zsize - the z size of the array (real of tables)
     *  tsize - the t size of the array
     *  elementSize - the size of an array element
     * Returns:
     *  NULL
     */
    void**** NAlloc4D(void ****vector, size_t xsize, size_t ysize, size_t zsize,
            size_t tsize, size_t elementSize);
    
    /*Deallocation functions*/
    /*
     * Abstract:
     *  Deallocates a 1D array
     * Parameters:
     *  vector - a pointer to the 1D array
     * Return:
     *  NULL
     */
    void* NFree1D(void *vector);
    /*
     * Abstract:
     *  Deallocates a 2D array
     * Parameters:
     *  vector - a pointer to the 2D array
     *  xsize - the x size of the array (real of rows)
     * Returns:
     *  NULL
     */
    void** NFree2D(void **vector, size_t xSize);
    /*
     * Abstract:
     *  Deallocates a 3D array
     * Parameters:
     *  vector - a pointer to the 3D array
     *  xsize - the x size of the array (real of rows)
     *  ysize - the y size of the array (real of columns)
     * Returns:
     *  NULL
     */
    void*** NFree3D(void ***vector, size_t xsize, size_t ysize);
    /*
     * Abstract:
     *  Deallocates a 4D array
     * Parameters:
     *  vector - a pointer to the 3D array
     *  xsize - the x size of the array (real of rows)
     *  ysize - the y size of the array (real of columns)
     *  zsize - the z size of the array (real of tables
     * Returns:
     *  NULL
     */
    void**** NFree4D(void ****vector, size_t xsize, size_t ysize, size_t zsize);

    5.Example
    #include<stdio.h>
    #include"dmda.h"
    
    int main()
    {
        int** array = NULL;
        int i,j;
        /*Creating a 10x10 2-dimensional numeric array*/
        array = (int**)NAlloc2D((int**)array,10,10,sizeof(int));
        /*Initializing the array with values*/
        for(i=0; i<10; i++)
        {
            for(j=0; j<10; j++)
            {
                array[i][j] = i-j;
            }
        }
        /*Displaying the 2-dimensional array*/
        for(i=0; i< 10; i++)
        {
            for(j=0; j<10; j++)
            {
                printf("%+d ",array[i][j]);
            }
            putchar('\n');
        }
        /*Freeing the 2-dimensional array*/
        array = (int**)NFree2D((int**)array,10);
        puts("Memory freed");
        return 0;
    }
    /*Output
    +0 -1 -2 -3 -4 -5 -6 -7 -8 -9
    +1 +0 -1 -2 -3 -4 -5 -6 -7 -8
    +2 +1 +0 -1 -2 -3 -4 -5 -6 -7
    +3 +2 +1 +0 -1 -2 -3 -4 -5 -6
    +4 +3 +2 +1 +0 -1 -2 -3 -4 -5
    +5 +4 +3 +2 +1 +0 -1 -2 -3 -4
    +6 +5 +4 +3 +2 +1 +0 -1 -2 -3
    +7 +6 +5 +4 +3 +2 +1 +0 -1 -2
    +8 +7 +6 +5 +4 +3 +2 +1 +0 -1
    +9 +8 +7 +6 +5 +4 +3 +2 +1 +0
    Memory freed
     */
    Related Posts Plugin for WordPress, Blogger...