SEMESTER
II
BCA2B02
– Problem Solving
Using C
Unit IV
Arrays and Strings - One dimensional array, two dimensional and multi-
dimensional arrays.
Arrays and Strings - One dimensional array, two dimensional and multi-
dimensional arrays.
Data
Structures in C
In C
language, arrays are referred to as structured data types. An
array is defined as finite ordered collection of homogenous data
(similar data), stored in contiguous memory locations.
Here the words,
finite means data
range must be defined.
ordered means data
must be stored in continuous memory addresses.
homogenous means data
must be of similar data type.
Instead of declaring individual
variables, such as number0, number1, ..., and number99, you declare one array
variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99]
to represent individual variables. A specific element in an array is accessed
by an index.
All arrays consist of
contiguous memory locations. The lowest address corresponds to the first
element and the highest address to the last element.
number[0]
|
number[1]
|
number[3]
|
……………
|
number[98]
|
Example where arrays are used,
·
to store list of Employee or Student
names,
·
to store marks of students,
·
or to store list of numbers or
characters etc.
Since arrays provide an
easy way to represent data, it is classified amongst the data structures in C.
Other data structures in c are structure, Linked lists, Stacks,
Queues and Trees. Array can be used to represent not only simple list
of data but also table of data in two or three dimensions.
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
the square brackets.
- Dimensions
used when declaring arrays in C must be positive integral constants or
constant expressions.
Syntax:
datatype arrayName [ arraySize ];
Examples:
int i, j, intArray[ 10 ], number; /* Single dimensional integer array of size 10*/
float floatArray[ 1000 ]; /* Single dimensional floating point array of size 1000*/
int tableArray[ 3 ][ 5 ]; /* Double dimensional array with 3 rows by 5 columns */
const int NROWS = 100;
const int NCOLS = 200;
float matrix[ NROWS ][ NCOLS ];
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 of commas in the examples below.
- An array
may be partially initialized, by providing fewer data items than the size
of the array. The remaining array elements will be automatically
initialized to zero.
- If an array
is to be completely initialized, the dimension of the array is not
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;
float sum = 0.0f, floatArray[ 3 ] = { 1.0f, 5.0f, 20.0f };
The number of values between braces { }
cannot be larger than the number of elements that we declare for the array
between square brackets [ ].
Following is an example to assign a
single element of the array:
If you omit the size of the array, an
array just big enough to hold the initialization is created. Therefore, if you
write:
double piFractions[ ] = { 3.141592654, 1.570796327, 0.785398163 };
Sample Programs Using 1-D Arrays
- 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.
/* Program to calculate the first 20 Fibonacci numbers. */
#include <stdlib.h>
#include <stdio.h>
int main( void ) {
int i, fibonacci[ 20 ];
fibonacci[ 0 ] = 0;
fibonacci[ 1 ] = 1;
for( i = 2; i < 20; i++ )
fibonacci[ i ] = fibonacci[ i - 2 ] + fibonacci[ i - 1 ];
for( i = 0; i < 20; i++ )
printf( "Fibonacci[ %d ] = %f\n", i, fibonacci[ i ] );
} /* End of sample program to calculate Fibonacci numbers */
- Exercise: What is the
output of the following program:
/* Sample Program Using Arrays */
#include <stdlib.h>
#include <stdio.h>
int main( void ) {
int numbers[ 10 ];
int i, index = 2;
for( i = 0; i < 10; i++ )
numbers[ i ] = i * 10;
numbers[ 8 ] = 25;
numbers[ 5 ] = numbers[ 9 ] / 3;
numbers[ 4 ] += numbers[ 2 ] / numbers[ 1 ];
numbers[ index ] = 5;
++numbers[ index ];
numbers[ numbers[ index++ ] ] = 100;
numbers[ index ] = numbers[ numbers[ index + 1 ] / 7 ]--;
for( index = 0; index < 10; index++ )
printf( "numbers[ %d ] = %d\n" index, numbers[ index ] );
} /* End of second sample program */
Multidimensional Arrays
C programming language allows multidimensional arrays. Here
is the general form of a multidimensional array declaration:
Syntax:
datatype name[size1][size2]...[sizeN];
For example, the following declaration creates a three dimensional
5 . 10 . 4 integer array:
int threedim[5][10][4];
- Multi-dimensional
arrays are declared by providing more than one set of square [ ]
brackets after the variable name in the declaration statement.
- One
dimensional arrays do not require the dimension to be given if the array
is to be completely initialized. By analogy, multi-dimensional
arrays do not require the first dimension to be
given if the array is to be completely initialized. All dimensions
after the first must be given in any case.
- 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 convention when discussing two dimensional
arrays.
- Multidimensional
arrays may be completely initialized by listing all data elements within a
single pair of curly {} braces, as with single dimensional arrays.
Two-Dimensional
Arrays
·
The simplest form of the
multidimensional array is the two-dimensional array. A two-dimensional array
is, in essence, a list of one-dimensional arrays. To declare a two-dimensional
integer array of size x, y you would write something as follows:
Syntax:
datatype arrayName [ x
][ y ];
· Where datatype can be any valid C data
type and arrayName will be a valid C
identifier.
identifier.
· A two-dimensional array can be think as
a table which will have x number of rows
and y number of columns.
and y number of columns.
· A 2-dimentional array a, which contains
three rows and four columns can be shown
as below:
as below:
· Thus, every element in array a is
identified by an element name of the form
a[ i ][ j ], where a is the name of the array, and i and j are the subscripts that
uniquely identify each element in a.
a[ i ][ j ], where a is the name of the array, and i and j are the subscripts that
uniquely identify each element in a.
Initializing
Two-Dimensional Arrays
· Multidimensional arrays may be initialized by specifying bracketed
values for
each row. Following is an array with 3 rows and each row has 4 columns.
int a[3][4] = { {0, 1, 2, 3} , {4, 5, 6, 7} , {8, 9, 10, 11}};
each row. Following is an array with 3 rows and each row has 4 columns.
int a[3][4] = { {0, 1, 2, 3} , {4, 5, 6, 7} , {8, 9, 10, 11}};
· The nested braces, which indicate the intended row, are optional.
The
following initialization is equivalent to previous example:
· int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
following initialization is equivalent to previous example:
· int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
Accessing
Two-Dimensional Array Elements
An element in 2-dimensional array is accessed by using the
subscripts, i.e., row index and column index of the array.
For example:
int val = a[2][3];
The above statement will take 4th element from the 3rd
row of the array. You can verify it in the above diagram. Let us check below
program where we have used nested loop to handle a two dimensional array:
#include <stdio.h>
int main ()
{
/* an array with 5 rows and 2 columns*/
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;
/* output each array element's value */
for ( i = 0; i < 5; i++ )
{
for ( j = 0; j < 2; j++ )
{
printf("a[%d][%d] = %d\n", i,j, a[i][j] );
}
}
return 0;
}
When the above code is compiled and
executed, it produces the following result:
a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8
No comments:
Post a Comment