Arrays

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Arrays:

A group of homogeneous elements of a specific data type is known as an array, one of


the simplest data structures. Arrays hold a sequence of data elements, usually of the same size
and same data type placed in contiguous memory locations that can be individually referenced.
Hence arrays are essentially a way to store many values under the same name. Individual
elements are accessed by their position in the array. The position is given by an index, which
is also called a subscript. The index usually uses a consecutive range of integers. Some arrays
are multi-dimensional, but generally, one- and two-dimensional arrays are the most common.

1. Select an array constant from Functions>>Programming>>Arrays palette; place the


array shell on the block diagram. The array shell appears, with an index display on the
left, an empty element display on the right, and an optional label.
2. Place a constant in the array shell. For example, a numeric constant in the array shell.
3. The array shell automatically resizes to accommodate the object you place in the array
shell. When an object is placed in the array shell, the data type of the array constant is
defined.
4. An alternative method of creating array constant is to copy or drag an existing array on
the front panel to the block diagram to create a constant of the same data type.

A two-dimensional array is analogous to a spreadsheet or table. A two-dimensional array stores


elements in a grid. It requires a column index and a row index to locate an element, both of which are
zero-based. Two-dimensional arrays are very commonly used in data acquisition applications. For
example, when waveforms from several channels are read from a data acquisition (DAQ) board, the
data is stored in a two-dimensional array where each column in the array corresponds to data from one
channel. To create a two-dimensional array on the front panel, right-click the index display of the array
and select Add Dimension from the shortcut menu. You also can use the Positioning tool to resize the
index display to have one more dimension. Figure 5.3 shows a two-dimensional array.
CREATING MULTIDIMENSIONAL ARRAYS

INITIALIZING ARRAYS

You can initialize an array, or leave it uninitialized. When an array is initialized, you can
define the number of elements in each dimension and the contents of each element. An uninitialized
array has a dimension but no elements. Figure 5.5(a) shows an uninitialized two-dimensional array
control with all the elements are dimmed indicating that the array is uninitialized. Figure 5.5(b)
shows an array of two rows and two columns.
ARRAY FUNCTIONS
Array functions are used to create and manipulate arrays. You can perform common array
operations such as extracting individual data elements from an array, inserting, deleting, or
replacing data elements in an array or splitting arrays using array functions.
Array functions including Index Array, Replace Array Subset, Insert Into Array, Delete From Array,
and Array Subset automatically resize to match the dimensions of the input array you wire.
For example, if you wire a one-dimensional array to one of these functions, the function
shows a single index input. If you wire a two-dimensional array to the same function, it shows two
index inputs—one for the row index and one for the column index.

1.

2.
AUTO INDEXING
For Loops and While Loops can index and accumulate arrays at their boundaries. This
is known as auto-indexing. If you wire an array to a For Loop or While Loop input tunnel, you
can read and process every element in that array by enabling auto-indexing. When you auto-
index an array output tunnel, the output array receives a new element from every iteration of
the loop. The wire from the output tunnel to the array indicator becomes thicker as it changes
to an array at the loop border, and the output tunnel contains square brackets representing an
array.

CREATING TWO-DIMENSIONAL ARRAYS USING LOOPS


3.
4.

5. Create a one-dimensional (1D) numeric array using the Build Array function
which gets array elements from numeric controls.

6. Create a 1D numeric array from loops (For and While) using random numbers and obtain the reverse of
the array.
7. Build a VI that generates a 1D array and then multiplies pairs of elements together and outputs the
resulting array. For example, the input array with values 1, 23, 10, 5, 7, 11 will result in the output array
23, 50, 77.
8.Build a VI to find the sum and product of array elements.

You might also like