Monday, 2 April 2018


The C language provides a capability that enables the user to define a set of ordered data items as an array.

Suppose we had a set of grades that we wished to read into the computer and suppose we wished to perform some operations on these grades, we will quickly realize that we cannot perform such an operation until each and every grade has been entered since it would be quite a tedious task to declare each and every student grade as a variable especially since there may be a very large number.
    In C we can define variable called grades, which represents not a single value of grade but a entire set of grades. Each element of the set can then be referenced by means of a member called as index number of subscript.

Declaration of Arrays
  Like any other variable arrays must be declared before they are used. The general form of declaration is :
   type variables-name[50];
 The type specifies the type of the elements that will be contained in the arrays, such as int, float or char and the size indicates the maximum number of elements that cane stored inside the array. For example :
   float height[50];
 Declares the height to be an array containing 50 real elements. Any subscripts  0 to 49 are valid. In C the array elements index or subscript begins with number zero.
  So height [0] refers to the first element of the array. (For this reason, it is easier to think of it as referring to element number zero, rather than as referring to the first element).

Initialization of Arrays
  We can initialize the elements in the array in the same way as the ordinary variables when they are declared. The general form of initialization of arrays is :
   type array_name[size] = {list of values};
The values in the list care separated by commas, for example the statement
  int number[3] = {0,0,0};
Will declare the array size as a array of size 3 and will assign zero to each element.
   How to access the array element can be understood by the following program :
/* Program to count the number of positive and negative numbers*/

void main( )
int a[50], n, count_neg=0, count_pos=0,I;
printf("Enter the size of the arrays\n");
printf("Enter the elements of the array\n");
if(a[I] < 0)
printf("There are %d negative numbers in the array\n",count_neg);
printf("There are %d positive numbers in the array\n",count_pos);

Multidimensional Arrays
  Often there is a need to store and manipulate two dimensional data structure such as matrices and tables. Here the array has two subscripts. One subscripts denotes the row and the other the column.
    The declaration of two dimension arrays is as follows :
       data_type array_name[row_size][column_size];
        int m[10][20];
Here m is declared as a matrix having 10 rows (numbered from 0 to 9) and 20 columns (numbered 0 through 19). The first element of the matrix is m[0][0] and the last row last column is m[9][19].

Elements of Multidimension Arrays
  A 2 dimensional array marks[4][3] is shown below. The first element is given by marks[0][0] contains 35.5 and second element is marks[0][1] and contains 40.5 and so on.

Initialization of Multidimensional Arrays

 Like the one dimension arrays, 2 dimension arrays may be initialized by following their declaration with a list of initial values enclosed in braces.

Example :
   int table[2][3] = {0,0,01,1,1};
Initializes the elements of first row to zero and second row to 1. The initialization is done row by row. The above statement can be equivalently written as
  int table[2][3] = { {0,0,0},{1,1,1}};
/* Example : Program to add two matrices & store the results in the  3rd matrix */

void main( )
int a[10][10], b[10][10], c[10][10], i, j, m, n, p, q;
clrscr( )
printf("enter the order of the matrix\n");
scanf("%d%d",&p, &q);
if(m= =p && n = = q)
printf("Matrix can be added\n");
printf("enter the elements of the matrix a");
printf("enter the elements of the matrix b");
printf("The matrix c is ");
getch( );

1 comment:

Popular Posts


Android (21) AngularJS (1) Books (3) C (75) C++ (81) Data Strucures (4) Engineering (13) FPL (17) HTML&CSS (38) IS (25) Java (85) PHP (20) Python (94) R (69) Selenium Webdriver (2) Software (13) SQL (27)