CSE Lab Report, Array, in C Programming
CSE Lab Report, Array, in C Programming
EXPERIMENT NO: 05
OBJECTIVE(S):
THEORY:
Arrays:
An array is a collection of variables of the same type that are referenced by a common name. In C,
all arrays consist of contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element. Arrays may have from one to several
dimensions. A specific element in an array is accessed by an index.
type variable-name[size];
Here, type declares the base type of the array, size defines how many elements the array will
hold. For example, the following declares as integer array named sample that is ten elements
long:
int sample[10];
In C, all arrays have zero as the index of their first element. This declares an integer array that
has ten elements, sample[0] through sample[9].
Operations
1. Insertion
2. Deletion
3. Searching
4. Sorting
5. Merging
CSE 1202: Computer Programming Sessional
sum = 0;
for (i=0; i<10; i++)
sum = sum + sample[i];
}
Two-dimensional arrays:
To declare two-dimensional integer array num of size (3,4), we write: int num[3][4]; Left Index
determines row and right index determines column. Two dimensional arrays are stored in a row-
column matrix where the first index indicates the row and the second indicates the column. This
means that the right most index changes faster than the leftmost when accessing the elements in
the array in the order in which they are actually stored in memory.
We notice that to move along any row, we have to change right index keeping the left index
unchanged and to move along any column, we have to change left index keeping the right index
unchanged.
Example:
main ()
{
int t, i, k=1, num [3][4];
for (t=0; t <3; t ++) // controlling left index
for (i=0; i<4; ++i) // controlling right index
CSE 1202: Computer Programming Sessional
Output:
1 4 7 10
2 5 8 11
3 6 9 12
Carefully notice the difference between the two codes and their corresponding outputs.
CSE 1202: Computer Programming Sessional
Array Initialization:
Example 1:
10 element integer array is initialized with the numbers 1 through 10 as:
Example 2:
PRACTICE PROBLEMS