background preloader

Arrays

Facebook Twitter

Matrix. 4.6 Multidimensional Arrays. C: create a pointer to two-dimensional array. Variable size multi-dimensional arrays. Next: The CAM graphics class Up: Scientific programming in C Previous: Complex numbers Multi-dimensional arrays crop up in a wide variety of different applications in scientific programming. Moreover, it is very common for the sizes of the arrays employed in a scientific code to vary from run to run, depending on the particular values of the input parameters.

For instance, in a standard fluid code the array sizes depend on the number of grid points, which, in turn, depends on the requested accuracy. Thus, a crucial test of the suitability of a programming language for scientific purposes is its ability to deal with variable size, multi-dimensional arrays in a convenient manner.

Unfortunately, C fails this test rather badly, since variable size matrix declarations of the form void function(a, m, n) { int m, n; double a[m][n]; . . . are illegal (unlike in FORTRAN 77, where variable size matrix declarations are perfectly acceptable). Typical output from the program looks like. C++ Notes: Array Initialization. An array can be initialized in the declaration by writing a comma-separated list of values enclosed in braces following an equal sign. int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; Altho this looks like an assignment, assignment statements with arrays are not allowed, and this syntax is legal only in a declaration.

Size can be set from initial value list If the size is omitted, the compiler uses the number of values. For example, // is the same as the statement below: int days[] = {31,28,31,30,31,30,31,31,30,31,30,31}; No default value If an array has no initialization, the values are undefined. float pressure[10]; // Initial values of pressure undefined.

Missing initialization values use zero If an explicit array size is specified, but an shorter initiliazation list is specified, the unspecified elements are set to zero. float pressure[10] = {2.101, 2.32, 1.44}; This not only initializes the first three values, but all remaining elements are set to 0.0. Initialization of character arrays. Initialization - initialize a const array in a class initializer in C++ C++ Notes: Dynamic Allocation of Arrays. The problems with fixed size arrays Declaring an array with a fixed size like int a[100000]; has two typical problems: Exceeding maximum. These problems can be avoided by dynamically allocating an array of the right size, or reallocating an array when it needs to expand.

This is exactly what is vector does, but let's see how it's done with an array. Declare array as a pointer, allocate with new To create a variable that will point to a dynamically allocated array, declare it as a pointer to the element type. Int* a = NULL; // pointer to an int, intiallly to nothing. A dynamically allocated array is declared as a pointer, and must not use the fixed array size declaration. Allocate an array with code>new When the desired size of an array is known, allocate memory for it with the new operator and save the address of that memory in the pointer. Freeing memory with delete When you are finished with dynamically allocated memory, free it with the delete operator. Use [] when deleting arrays Examples.