Department of Computer Applications: T He Is Lamia Colle Ge of SC Ie NC e & Comme R C E, SR Inagar - J&K
Department of Computer Applications: T He Is Lamia Colle Ge of SC Ie NC e & Comme R C E, SR Inagar - J&K
T h e I s l a m i a C o l l e g e o f S c i e nc e & C o m m e r c e , Sr i n a g a r - J & K
(UGC- Autonomous)
Credits (4 +2)
websites.
Purpose Educational.
• An array is a collection of data items, all of the same type, accessed using a common
name.
• A one-dimensional array is like a list; A two dimensional array is like a table; The C
implementations may.
as matrices, and use the general term arrays when the number of dimensions is
unspecified or unimportant.
Declaring Arrays
• Array variables are declared identically to variables of their data type, except that the
variable name is followed by one pair of square [ ] brackets for each dimension of the
array.
• Uninitialized arrays must have the dimensions of their rows, columns, etc. listed within
constant expressions.
long as the variable has a positive value at the time the array is declared. (
Space is allocated only once, at the time the array is declared. The array does
• Examples:
const int NROWS = 100; // ( Old code would use #define NROWS 100 )
const int NCOLS = 200; // ( Old code would use #define NCOLS 200 )
• C Only Example:
int numElements;
exit( 0 );
Initializing Arrays
• Arrays may be initialized when they are declared, just as any other variables.
• Place the initialization data in curly {} braces following the equals sign. Note the use
• An array may be partially initialized, by providing fewer data items than the size of the
required. The compiler will automatically size the array to fit the initialized data. (
• Examples:
int i = 5, intArray[ 6 ] = { 1, 2, 3, 4, 5, 6 }, k;
Designated Initializers:
• For example:
int numbers[ 100 ] = { 1, 2, 3, [10] = 10, 11, 12, [60] = 50, [42] = 420 };
respectively.
o The next two elements ( 12th and 13th ) are initialized to 11 and 12
respectively.
order. )
o If the size of the array is not given, then the largest initialized position
Using Arrays
• Elements of an array are accessed by specifying the index ( offset ) of the desired
• Array subscripts must be of integer type. ( int, long int, char, etc. )
• VERY IMPORTANT: Array indices start at zero in C, and go to one less than the size
of the array. For example, a five element array will have indices zero through
four. This is because the index in C is actually an offset from the beginning of the
array. ( The first element is at the beginning of the array, and hence has zero offset. )
• Landmine: The most common mistake when working with arrays in C is forgetting that
indices start at zero and stop one less than the array size.
• Arrays are commonly used in conjunction with loops, in order to perform the same
• The first sample program uses loops and arrays to calculate the first twenty Fibonacci
numbers. Fibonacci numbers are used to determine the sample points used in certain
optimization methods.
#include <stdlib.h>
#include <stdio.h>
int i, fibonacci[ 20 ];
fibonacci[ 0 ] = 0;
fibonacci[ 1 ] = 1;
#include <stdlib.h>
#include <stdio.h>
int numbers[ 10 ];
int i, index = 2;
numbers[ i ] = i * 10;
numbers[ 8 ] = 25;
numbers[ 5 ] = numbers[ 9 ] / 3;
numbers[ index ] = 5;
++numbers[ index ];
Multidimensional Arrays
• Multi-dimensional arrays are declared by providing more than one set of square [ ]
• One dimensional arrays do not require the dimension to be given if the array is to be
• For two dimensional arrays, the first dimension is commonly considered to be the
number of rows, and the second dimension the number of columns. We will use this
arrays ). For example, "int numbers[ 5 ][ 6 ]" would refer to a single dimensional
• Another way of looking at this is that C stores two dimensional arrays by rows, with all
elements of a row being stored together as a single unit. Knowing this can sometimes
• It is better programming practice to enclose each row within a separate subset of curly
{} braces, to make the program more readable. This is required if any row other than
the last is to be partially initialized. When subsets of braces are used, the last item
within braces is not followed by a comma, but the subsets are themselves separated by
commas.
qualifying the array name. For example, if "data" has been declared as a three
would refer to a one-dimensional array of floats, and data[ 1 ] would refer to a two-
dimensional array of floats. The reasons for this and the incentive to do this relate to
#include <stdlib.h>
#include <stdio.h>
int b[ 2 ][ 3 ] = { { 1, 2, 3 }, { 3, 2, 1 } };
return 0;