Examples

This section contains three simple C language example programs that illustrate the use of the C routine APIs (aglxxxx routines):

Array Visualizer Sample programs are installed on your hard disk when you select a Complete installation. You can also copy Samples folders from the Array Visualizer CD-ROM to your hard disk.


Example 1

The first example uses the aglMalloc call to dynamically allocate memory for an array and then view the array using the Array Viewer. This example is a Sample program that can be found in the folder  ...\ArrayVisualizer\Samples\C\malloc2d\:

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
// avdef.h is the header file for the AView library
#include <avdef.h>

void
pause()
{
	printf("press any key to continue\n");
	_getch();
}

int
main(int argc, char *argv[])
{
	int dims[] = {40, 50};
	int rc, i, j;
	float x, y, z, rval, *fmat;
	const float fPi = 3.14159f;

	// Use aglMalloc to allocate memory for the array
	fmat = (float *)aglMalloc(dims[0]*dims[1]*sizeof(float));
	if (fmat == NULL) {
		printf("aglMalloc failed\n");
		return -1;
	}

// Initialize the data
	for (j=0; j<dims[0]; j++) {
		y = (float)j/(float)dims[0];
		for (i=0; i<dims[1]; i++) {
			x = (float)i/(float)dims[1];
			z = (float)(sin(y*fPi) + cos(x*fPi));
			// Note that we have to do a bit of pointer arithmetic
			// to assign the (j, i) element of the array to z.
			*(fmat + j*dims[1]+ i) = z;
		}
	}

	// Call aglReshape to let the AView library know about the
	// array dimensions and type.
	rc = aglReshape(fmat, 2, dims, AGL_FLOAT);
	if (rc != 0) {
		printf("aglReshape failed!\n");
		return -1;
	}

	// Set the title bar on ArrayViewer
	aglName(fmat, "sin(x) + cos(y)");

	printf("Starting Array Viewer\n");
	// aglShow will bring up Array Viewer with a view of our array.
	rc = aglShow(fmat);
	if (rc != 0) {
		printf("aglShow failed\n");
		return -1;
	}
	pause();

	// Add some psudeo-random fluctuations
	for (j=0; j<dims[0]; j++) {
		y = (float)j/(float)dims[0];
		for (i=0; i<dims[1]; i++) {
			// produce a random number between 0 and 1.
			rval = (float)(rand() % 1000) * 0.001f;
			// Scale and shift to range: -0.1 to 0.1
			rval = rval * 0.2f - 0.1f;
			// Add to array element
			*(fmat + j*dims[1]+ i) += rval;
		}
	}

	// Inform the viewer that the array data has been changed.
	printf("Updating data\n");
	aglUpdate(fmat);

	// Change the title to reflect the changes in the data set.
	aglName(fmat, "sin(x) + cos(y) + noise");

	// Wait for a key press
	pause();

	printf("Closing down the viewer\n");

 	// Close the Viewer
	rc = aglClose(fmat);
	if (rc != 0) {
		printf("aglClose failed\n");
		return -1;
	}

	pause();

	// Deallocate memory
	aglFree(fmat);

	return 0;
}


Example 2

The second example uses the aglAlloc call to allocate memory for the array. This Sample can be found in the folder  ...\ArrayVisualizer\Samples\C\alloc2d\:

#include <windows.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <math.h>
// avdef.h is the header file for the AView library
#include <avdef.h>

void
pause()
{
	printf("press any key to continue\n");
	_getch();
}

int
main(int argc, char *argv[])
{
	int dims[] = {40, 50};
	int rc, i, j;
	float x, y, z, rval, **fMat;
	const float fPi = 3.14159f;

	// Use aglAlloc to allocate memory for the array.
	// This uses slightly more memory than using aglMalloc,
	// but makes it easier to reference the array elements.
	// Note that fMat is of type float **, ie a pointer to
	// a pointer to a float.
	fMat = (float **)aglAlloc(2, dims, AGL_FLOAT);
	if (fMat == NULL) {
		printf("aglAlloc failed\n");
		return -1;
	}

	// Initialize the data
	for (j=0; j<dims[0]; j++) {
		y = (float)j/(float)dims[0];
		for (i=0; i<dims[1]; i++) {
			x = (float)i/(float)dims[1];
			z = (float)(sin(y*fPi) + cos(x*fPi));
			fMat[j][i] = z;
		}
	}
// Set the title bar on ArrayViewer
	aglName(fMat, "sin(x) + cos(y)");

	printf("Starting Array Viewer\n");
	// aglShow will bring up Array Viewer with a view of our array.
	rc = aglShow(fMat);
	if (rc != 0) {
		printf("aglShow failed\n");
		return -1;
	}
	pause();

	// Add some psudeo-random fluctuations
	for (j=0; j<dims[0]; j++) {
		y = (float)j/(float)dims[0];
		for (i=0; i<dims[1]; i++) {
			// produce a random number between 0 and 1.
			rval = (float)(rand() % 1000) * 0.001f;
			// Scale and shift to range: -0.1 to 0.1
			rval = rval * 0.2f - 0.1f;
			// Add to array element
			fMat[j][i] += rval;
		}
	}

	// Inform the viewer that the array data has been changed.
	printf("Updating data\n");
	aglUpdate(fMat);

	// Change the title to reflect the changes in the data set.
	aglName(fMat, "sin(x) + cos(y) + noise");

	// Wait for a key press
	pause();

	printf("Closing down the viewer\n");

	// Close the Viewer
	rc = aglClose(fMat);
	if (rc != 0) {
		printf("aglClose failed\n");
		return -1;
	}

	pause();

	// Deallocate memory
	aglFree(fMat);

	return 0;
}


Example 3

The third example calls aglStartWatch to view an array that has been statically allocated. Calling aglStartWatch results in more memory being used than with aglMalloc or aglAlloc, since the array data needs to be copied to the Array Viewer's process space.

#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
// avdef.h is the header file for the AView library
#include <avdef.h>


#define NCOLS 40
#define NROWS 50

void
pause()
{
	printf("press any key to continue\n");
	_getch();
}

int
main(int argc, char *argv[])
{
	// In this example our array will be statically allocated.
	float fmat[NROWS][NCOLS];
	int dims[] = {NROWS, NCOLS};
	int rc, i, j;
	float x, y, z, rval;
	const float fPi = 3.14159f;

	// Use aglStartWatch for arrays that aren't dynamically allocated
	// with aglMalloc(), or aglAlloc().
	aglStartWatch(fmat, dims[0]*dims[1]*sizeof(float));

	// Initialize the data
	for (j=0; j<dims[0]; j++) {
		y = (float)j/(float)dims[0];
		for (i=0; i<dims[1]; i++) {
			x = (float)i/(float)dims[1];
			z = (float)(sin(y*fPi) + cos(x*fPi));
			fmat[j][i] = z;
		}
	}

	// Call aglReshape to let the AView library know about the
	// array dimensions and type.
	rc = aglReshape((void *)&fmat[0][0], 2, dims, AGL_FLOAT);
	if (rc != 0) {
		printf("aglReshape failed!\n");
		return -1;
	}

	// Set the title bar on ArrayViewer
	aglName(fmat, "sin(x) + cos(y)");

	// Inform the AView library to update its view of the data.
	aglUpdate(fmat);

	printf("Starting Array Viewer\n");
	// aglShow will bring up Array Viewer with a view of our array.
	rc = aglShow(fmat);
	if (rc != 0) {
		printf("aglShow failed\n");
		return -1;
	}
	// Note in Array Viewer the Data\Refresh menu item is disabled since the
	// viewer can't "pull" in data from statically allocated arrays.
	pause();

	// Add some psudeo-random fluctuations
	for (j=0; j<dims[0]; j++) {
		y = (float)j/(float)dims[0];
		for (i=0; i<dims[1]; i++) {
			// produce a random number between 0 and 1.
			rval = (float)(rand() % 1000) * 0.001f;
			// Scale and shift to range: -0.1 to 0.1
			rval = rval * 0.2f - 0.1f;
			// Add to array element
			fmat[j][i] += rval;
		}
	}

	// Inform the viewer that the array data has been changed.
	printf("Updating data\n");
	aglUpdate(fmat);

	// Change the title to reflect the changes in the data set.
	aglName(fmat, "sin(x) + cos(y) + noise");

	// Wait for a key press
	pause();

	printf("Closing down the viewer\n");

	// Close the Viewer
	rc = aglClose(fmat);
	if (rc != 0) {
		printf("aglClose failed\n");
		return -1;
	}

	pause();

	// Call aglEndWatch to free resources allocated by aglStartWatch
	rc = aglEndWatch(fmat);

	return 0;
}