Slide1 : ARRAYS INTRODUCTION : An array is a group of related data items that share a common name. For instance, we can define an array name Salary to represent a set of salaries of a group of employees. A particular value is indicated by writing a number called
index number or subscript in brackets after the array name. For example,
Salary [10] represents the salary of the 10th employee while the complete set of values is referred to as an array, the individual values are called elements. Arrays can be of any variable type. One -Dimensional Arrays : A list of items can be given one variable name using
only one subscript and such a variable is called Single-subscripted variable or a
One-Dimensional array. In mathematics we often deal with variables that are
single-subscripted. For instance,
we use the equation A = Xi
n=0
calculate the average of n values of x. the subscripted variable Xi refers to the ith
element of X. In C, single-subscripted variable Xi can be expressed as
X[1], X[2], X[3],...............X[n]
Slide2 : The subscript can begin with number 0.that is allowed. For example, if we want to represent a set of five numbers, say(35,40,20,57,19) by an array variable number, then we may declare the variable number as follows
int number [5];
and the computer reserves five storage locations as shown below.
Number[0] Number[1] Number[2]
Number[3] Number[4]
values to the array elements can be assigned as follows:
number[0]=35; number[1]=40; number[2]=20; number[3]=57; number[4]=19;
this would cause the array number to store the values as shown below:
num ber[0] 35
number[1] 40
number[2] 20
number[3] 57
number[4] 19
these elements may be used in programs just like any other C variable. For example, the following are valid statements:
a= number[0] +10; number[4]= number[0] + number[2];
number[2]= X[5] + Y[10]; value[6]= number[i]* 3; subscript of an array can be integer constant, integer variables like i , or expressions that yield integer. C performs no bounds checking and therefore, care should be exercised to ensure that the array indices are within the declared limits.
Slide3 : Declaration of Arrays
Like any other variables, arrays must be declared before they are used. The general form of array declaration is
type variable-name [size];
The type specifies the type of elements that will be contained in the array, such as int,float or char and the size indicates the maximum number of elements that can be stored inside the array.
For example: float height[50];
declares the height to be an array containing 50 real elements.
Any subscript 0 to 49 are valid. Similarly, int group[10]; declares the group to be an array to contain maximum of 10 integer constants. Remember, any reference to the array outside the declared limits would not necessarily cause an error. Rather, it might result in unpredictable program results. The C language treats character string simply as array of characters. The size in a character string represents the maximum number of characters that the string can hold. For instance,
char name[10];
declares the name as a character array(string) variable that can hold a maximum of 10 characters. Suppose we declare the following string constant into the string variable name.
WELL DONE
Slide4 : W
E
L
L
D
O
N
E
\0
When the compiler sees a character string, it terminates it with an additional null character.
Thus, the element name[9] holds the null character \0 at the end. When declaring character array, we must always allow one extra element space for the null terminator. each character of the string is treated as an element of the array name and is stored in the memory as follows:
Slide5 : Initialization of Arrays
We can initialize the elements of the array in the same way as the ordinary variables when they are declared. The general form of initialization of array is:
static type array-name [size] = { list of variables};
the values in the list are separated by commas. For example, the statement
static int number [3] = {0,0,0}
will declare the variable number as an array of size 3 and will assign zero to each element. If the number of values in the list is less than the number of elements, then only that many elements will be initialized the remaining elements will be set to zero automatically. For instance,
static float total[5] ={ 0.0, 15.75, -10};
will initialize the first three elements to 0.0, 15.75, -10 and the remaining two elements to zero. Note that we have used the word static before type declaration. This declares the variable as a static variable. The size may be omitted. In such cases the compiler allocates enough space for all initialized elements. For example, the statement
static int counter []={ 1,1,1,1}; will declare the counter array to contain four elements with initial values 1. This approach works fine as long as we initialize every element in the array.
Slide6 : Character arrays may be initialized in a similar manner. Thus, the statement
static char name[] = {J, o ,h , n};
declares the name to be an array of characters, initialized with the string John. Initialization of arrays in C suffers two drawbacks.
1.There is no convenient way to initialize only selected elements.
2.There is no shortcut method for initializing a large number of array elements
Program showing One-Dimensional Array:
main()
{ int i;
float X[10],value,total;
print f (“Enter 10 real numbers \n”);
for (i=0;i<10;i++) {
scan f(“%f” ,&value);
X[i]=value; }
total=0.0;
for(i=0;i<10;i++) total =total + X[i] * X[i];
print f (“\n”);
for (i=0;i<10;i++) print f(“ X[%2d]=%5.2f\n”, X[i]);
print f(“\n total =%2.f \n” , total);
}
Slide7 : OUTPUT:
Enter 10 real numbers
1.2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9 10.10
X[1]=1.10 X[2]=2.20 X[3]=3.30 X[4]=4.40 X[5]=5.50
X[6]=6.60 X[7]=7.70 X[8]=8.80 X[9]=9.90 X[10]=10.10
total= 446.86
consider an array of size ,say 100. All the 100 elements have to be explicitly initialize. There is no way to specify a repeat count. In such situations, it would be better to use a for loop to initialize the elements. Consider the following segment of a C program:
for(i=0;i<100;i=i+1) { if(i<50) sum[i]=0.0; else sum[i]=1.0; }
the first 50 elements of the array sum are initialized to zero while the remaining 50 elements are initialized to 1.0
Slide8 : Two-Dimentional Arrays
So far we have discussed the array variable that can store a list of values . There will be situation where a table of values will have to be stored.
Consider the following data table, which shows the value of the sales of three items by four sales mens:
item 1 item 2 item 3
salesmen 1 310 275 365
salesmen 2 210 190 325
salesmen 3 405 325 240
salesmen 4 260 300 290
the table contains a total of 12 values, three in each line.We think of this table as a matrix consisting of four rows and three columns. Each row represents the values of sales by a particular salesmen and each column represents the values of sales of a particular item. In mathematics, we represent a particular value in a matrix by using two sub scripts such as Vij. Here V denotes the entire matrix and Vij refers to the value in the ith row and jth column. For example, in the above table V23 refers to the value 325. C allows us to define such tables of items by using two-dimensional arrays. The table discussed above can be defined in c as v[2][3].
Slide9 : Two-Dimensional arrays are declared as follows:
type array-name [row-size][column-size];
note that unlike most other languages which we use one pair of parenthesis with commas to separate array sizes. C places each size in the own set of brackets. Two dimensional arrays are stored in memory as follows: [0][0] [0][1] [0][2]
310 275 365
[1][0] [1][1] [1][2]
210 190 325
[2][0] [2][1] [2][2]
405 325 240
[3][0] [3][1] [3][2]
260 300 290