Turn Desktop View Off
by Dinesh Thakur

A three-dimensional array is that array whose elements are two-dimensional arrays. In practice, it may be considered to be an array of matrices. A three-dimensional array with int elements may be declared as below.

 
by Dinesh Thakur

The procedure of passing more than one arrays to a function is similar to the case of passing a single array. This is illustrated in Program.

 
by Dinesh Thakur

Program sorts the integers in ascending order by using insertion method. The process of sorting starts with picking the second element which is arranged with respect to the first in the specified order (ascending or descending order).

 
by Dinesh Thakur

Suppose, we want to sort an array in ascending order. The elements with higher values will move back, while elements with smaller values will move to the front; the smallest element will become the 0th element and the largest will be placed at the end. The mechanism of sorting is explained below.

 
by Dinesh Thakur

Each element of an array is a variable of the type declared for the array. Thus, if an array is declared as int Array [n],all its n elements are of type int, and all the operations that can be done on variables of type int may be carried out on the elements of this array. The same holds for arrays of types float, double, and char. In Program integer array elements are subjected to arithmetic operations.

 
by Dinesh Thakur

When a program containing an array of size n is compiled, the compiler allocates n blocks of memory for the array for storing the values of its elements. The size of each block depends on the data type of the array. For example, for the array AR [ ] defined below, five blocks of memory are allocated and each block is of the size of the memory block for an integer (i.e., 4 bytes on a 32-bit system).



 
by Dinesh Thakur

The maximum or minimum value may be determined by comparing the values of array elements. Let us assume that the maximum value is represented by 'max'. The value of the first element of the array is assigned to max.

 
by Dinesh Thakur

In case of large arrays, the method of comparing every array element with key as described above is rather inefficient. However, if an array is a sorted one, the search process can be considerably shortened by using binary search method. For the application of binary search the array should be first sorted.

 
by Dinesh Thakur

Searching an array for a value is the most common occurrence in programming as well as in real life. Often it is required to search a name in a list of names or search a roll number in a list of roll numbers.

 
by Dinesh Thakur

Any operation on an array has to be carried out element by element. It cannot be performed on the array as a whole. Therefore, in swapping also, an element of one array is swapped with an element of another array. The two may not have the same index value if you are not dealing with vectors and matrices.

 
by Dinesh Thakur

An array is similar to a list in which the objects are of the same type and stored in sequential memory blocks; this is the only relationship between the elements of an array. The input/output of values of elements of an array cannot be done as whole of array, but is carried out element by element. This may be done by accessing each element by its index value. Therefore, for large arrays, either a for loop or a while loop may be used for accessing each element for its input/output. For example, if elements of an array Array [4] having 4 elements are to be processed for input /output by user of a program, the program code may be written as follows:



 
by Dinesh Thakur

As we know, a pointer to type T is analogous to an array of type T.A pointer can be used to represent a vector, as illustrated in Fig. When a pointer is declared, the memory is allocated only for the pointer variable. The memory for the array elements is usually allocated separately using dynamic memory allocation functions, namely, malloc or calloc.



 
by Dinesh Thakur

An array is a powerful and easy-to-use data structure provided in the C language. We know that arrays provide easy access to their elements and entire arrays can be manipulated easily using loops. However, there are some drawbacks/limitations of arrays:

 
by Dinesh Thakur

When we declare a pointer, it does not point to any specific variable. We must initialize it to point to the desired variable. This is achieved by assigning the address of that variable to the pointer variable, as shown below.

 
by Dinesh Thakur

C language permits the use of multidimensional arrays. It is easy to visualize arrays up to three dimensions. However, we may have difficulty in visualizing a four, five or in general, an n-dimensional array.

 
by Dinesh Thakur

A two-dimensional array (commonly called a matrix) consists of elements of the same type arranged in rows and columns. The rows and columns of a matrix are numbered starting from 0. Thus, for an array of size rows x cols, the row subscripts are in the range 0 to rows - 1 and the column subscripts are in the range 0 to cols - 1.

 
by Dinesh Thakur

As in case of scalar variables, we can also use external or global arrays in a program, i. e., the arrays which are defined outside any function. These arrays have global scope.

 
by Dinesh Thakur

In the HSC examination, there are six subjects each having a maximum of 100 marks. To pass the examination, a student has to score at least 35 marks in each subject. If a student passes, total marks and percentage marks are calculated and a class is awarded based on the percentage marks (perce)as follows:

 
by Dinesh Thakur

For a list of numbers x whose elements are referred to as x0, x1,…..., xn-1, the mean bar x and the standard deviation a are defined as

 
by Dinesh Thakur

For two vectors a and b having n elements each, the addition operation yields a vector (say c) of size n. The ith element of the result vector is obtained by adding the corresponding vector elements, i.e., ci =ai+ bi. The algorithm to perform the desired addition is given below.

 
by Dinesh Thakur

In this program, an array num of type int is used to store the given integer numbers and variable nelem to store their count. First, the value of nelem is read. Then a for loop is used to read the given numbers and store them in array num. Finally, another for loop is used to print the given numbers in reverse order.

 
by Dinesh Thakur

This feature can be used with arrays as well. A static array has the following characteristics:

 
by Dinesh Thakur

If we desire that the elements of a vector should not be modified, we can declare that vector as a const vector. However, we must initialize this vector when it is declared, as it is not possible to modify it subsequently. Thus, a const vector can be declared and initialized as shown below.

 
by Dinesh Thakur

We can pass an array (one-, two- or multidimensional) as an argument to a function. The C language allows us to define functions that have one or more arrays as parameters. These parameters can be of different types and sizes. Moreover, we can mix scalar and array parameters. A function that uses a single one-dimensional array as a parameter can be defined as shown below.

 
by Dinesh Thakur

Array Declaration

Every array is given a name. The rules for naming arrays are the same as those used for variable names. As with scalar variables, an array must be declared before it is used. The declaration of a one-dimensional array takes the following form:

 
by Dinesh Thakur

Declaration of arrays, Initialization of arrays, Multi dimensional Arrays, Elements of multi dimension arrays and Initialization of multidimensional arrays. The C language provides a capability that enables the user to define a set of ordered data items known as an array.

 
by Dinesh Thakur

Often, we have to deal with groups of objects of same type such as names of persons, instrument readings in an experiment, roll numbers of students, and so on. These groups can be conveniently represented as elements of arrays. An array is defined as a sequence of objects of the same data type. All the elements of an array are either of type int (whole numbers), or all of them are of type char, or all of them are of floating decimal point type, etc. An array cannot have a mixture of different data types as its elements. Also, array elements cannot be functions; however, they may be pointers to functions. In computer memory, array elements are stored in a sequence of adjacent memory blocks. Since all the elements of an array are of same data type, the memory blocks allocated to elements of an array are also of same size. Each element of an array occupies one block of memory. The size of memory blocks allocated depends on the data type and it is same as for different data types. 



 
by Dinesh Thakur

Declaring Arrays : Arrays are declared with the bracket punctuators [ ], as shown in the following syntax :

 
by Dinesh Thakur

The amount of memory an array can consume depends on the data type of an array. In DOS environment, the amount of memory an array can consume depends on the current memory model (i.e. Tiny, Small, Large, Huge, etc.).



 
by Dinesh Thakur

1) An array holds elements that have the same data type.

2) Array elements are stored in subsequent memory locations.



 
by Dinesh Thakur

Pointers and Arrays : Data objects in an array can be referenced through pointers instead of using array subscripts. The data type of such a pointer is referred to as "pointer to array of type". The array name itself behaves like a pointer, so there are several alternative methods to accessing array elements. For example :



 
by Dinesh Thakur

Initializing Arrays : Arrays are initialized with a brace-enclosed list of constant expressions. A list of initializers for an incomplete array declaration completes the array's type and completely defines the array size.

 
by Dinesh Thakur

A major difference is: string will have static storage duration, whereas as a character array will not, unless it is explicitly specified by using the static keyword. Actually, a string is a character array with following properties :

 
by Dinesh Thakur

An array is an array of anything. A string is a specific kind of an array with a well-known convention to determine its length.