0% found this document useful (0 votes)
13 views32 pages

Chapter 9 Arrays

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views32 pages

Chapter 9 Arrays

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Arrays

Chapter 9
Introduction (1/2)
 Many applications require the processing of multiple data
items that have common characteristics (e.g., a set of
numerical data, represented by x1, x2, . . . ,xn).
 In such situations it is often convenient to place the data
items into an array, where they will all share the same
name (e.g., x).
 However, they must all be of the same type and the same
storage class.
 Each array element (i.e., each individual data item) is
referred to by specifying the array name followed by one or
more subscripts, with each subscript enclosed in square
brackets.
 Each subscript must be expressed as a nonnegative integer.
In an n-element array, the array elements are X[0], X[1] ,
X[2], . . . ,X[n – 1] , as illustrated in Fig. 9.1.
 The value of each subscript can be expressed as an integer
constant, an integer variable or a more complex integer
expression.
Introduction (2/2)
The number of subscripts determines the
dimensionality of the array. For example, x [ i ]
refers to an element in the one-dimensional array
x. Similarly, y[ i ][ j ] refers to an element in the
two-dimensional array y.
Higher-dimensional arrays can be also be formed,
by adding additional subscripts in the same
manner (e.g., z [ i ] [j] [ k ] )
DEFINING AN ARRAY
In general terms, a one-dimensional array
definition may be expressed as storage -class
data- type array[ expression] ;
It is sometimes convenient to define an array size
in terms of a symbolic constant rather than a
fixed integer quantity.
The initial values must appear in the order in
which they will be assigned to the individual array
elements, enclosed in braces and separated by
commas. The general form is
storage- class data- type array[ expression] =
{ value 1, value 2, . . . , value n) ;
DEFINING AN ARRAY
DEFINING AN ARRAY
All individual array elements that are not
assigned explicit initial values will automatically
be set to zero.
This includes the remaining elements of an array
in which some elements have been assigned
nonzero values.
DEFINING AN ARRAY
The array size need not be specified explicitly
when initial values are included as a part of an
array definition.
With a numerical array, the array size will
automatically be set equal to the number of initial
values included within the definition.
In particular, when a string constant is assigned
to an external or a static character array as a part
of the array definition, the array size specification
is usually omitted.
The proper array size will be assigned
automatically. This will include a provision for the
null character \O, which is automatically added at
the end of every string
PROCESSING AN ARRAY
Single operations which involve entire arrays are not
permitted in C. Thus, if a and b are similar arrays
(i.e., same data type, same dimensionality and same
size), assignment operations, comparison operations,
etc. must be carried out on an element-by-element
basis.
This is usually accomplished within a loop, where
each pass through the loop is used to process one
array element. The number of passes through the
loop will therefore equal the number of array
elements to be processed.
Let us define list to be a 100-element, floating-point
array. However, we need not make use of all 100
elements. Rather, we shall specify the actual number
of elements by entering a positive integer quantity
PASSING ARRAYS TO
FUNCTIONS
An entire array can be passed to a function as an
argument.
To pass an array to a function, the array name
must appear by itself, without brackets or
subscripts, as an actual argument within the
function call.
When declaring a one dimensional array as a
formal argument, the array name is written with a
pair of empty square brackets.
The size of the array is not specified within the
formal argument declaration.
PASSING ARRAYS TO
FUNCTIONS
We have already discussed the fact that arguments are
passed to a function by value when the arguments are
ordinary variables
When an array is passed to a function, however, the
values of the array elements are not passed to the
function.
Rather, the array name is interpreted as the address
of the first array element (i.e., the address of the
memory location containing the first array element).
This address is assigned to the corresponding formal
argument when the function is called. The formal
argument therefore becomes a pointer to the first
array element (more about this in the next chapter).
Arguments that are passed in this manner are said to
be passed by reference rather than by value.
PASSING ARRAYS TO
FUNCTIONS

Moreover, if an array element is altered


within the function, the alteration will be
recognized in the calling portion of the
program (actually, throughout the entire scope
of the array).
The ability to alter an array globally within a
function provides a convenient mechanism for
moving multiple data items back and forth
between the function and the calling portion of
the program.
Contd…
ARRAYS
Multidimensional arrays are defined in much the
same manner as one-dimensional arrays, except
that a separate pair of square brackets is
required for each subscript.
storage-class data- type array[ expression
1 ] [ expression 2] . . . [ expression n] ;
Several typical multidimensional array
definitions are shown below-
float table[50][50];
char page[24][80];
static double records[100][66][255];
static double records[L][M][N];
Multidimensional Arrays
Multidimensional Arrays
Three Dimensional Array
Multidimensional Array
Multidimensional arrays are processed in the
same manner as one-dimensional arrays, on
an element-by element basis. However,
some care is required when passing
multidimensional arrays to a function.
In particular, the formal argument
declarations within the function definition
must include explicit size specifications in all
of the subscript positions except the first.
These size specifications must be consistent
with the corresponding size specifications in the
calling program.
ARRAYS AND STRINGS
We have already seen that a string can be
represented as a one-dimensional character-type
array.
Each character within the string will be stored
within one element of the array.
 Some problems require that the characters within
a string be processed individually.
However, there are many other problems which
require that strings be processed as complete
entities. Such problems can be simplified
considerably through the use special string-
oriented library functions.
ARRAYS AND STRINGS
strcmp and strcpy:
The strcmp function accepts two strings as
arguments and returns an integer value,
depending upon the relative order of the two
strings, as follows:
1. A negative value if the first string precedes the
second string alphabetically.
2. A value of zero if the first string and the second
string are identical (disregarding case).
3. A positive value if the second string precedes the
first string alphabetically.
Input and Output
Thank You!!!

You might also like