C-Tutorial: ARRAYS

Latest

Showing posts with label ARRAYS. Show all posts
Showing posts with label ARRAYS. Show all posts

Sunday, 9 October 2016

Multidimensional arrays in C-language

Sunday, October 09, 2016 0
Multidimensional arrays

            If an array consists of two or more subscripts then it is called "Multidimensional array".  In generally a multidimensional array consists of three subscripts.

Declarationof Multi dimensional arrays:

The general format of a multidimensional array is:
            Syntax:           datatype ArrayName[size1][size2] - - - - -  - [sizen];
            Example:        int k[2][3][4];

            The above example is a three dimensional array.  Here, compiler allocates memory as in terms of tables.
                                   
INITIALIZATION OF MULTIDIMENSIONAL ARRAYS

            Three dimensional arrays can be initialized into two different ways.
            1. Direct initialization
            2. Through program execution

            Direct Initialization:
     The general form of initialization of a multidimensional array is:
Syntax:           datatype ArrayName[size1][size2]- - - -[sizen] = {List of Values};

            Here, List of Values is separated by comma operator.
                        Example:        int k[2][3][2] = {10,20,30,40,50,60,70,80,90,10,11,12};      

Through program execution:

/* PROGRAM TO READ A MULTDIMENSIONAL ARRAY AND PRINT IT */

main()
{
            int m,n,p,i,j,k,x[10][10][10];
            clrscr();
            printf("\nEnter how many Tables:");
            scanf("%d",&m);
            printf("\nEnter how many rows:");
            scanf("%d",&n);
            printf("\nEnter how many columns:");
            scanf("%d",&p);
            printf("\nEnter Array Elements:");
            for(i=0;i<m;i++)
            {
                        for(j=0;j<n;j++)
                        {
                                    for(k=0;k<p;k++)
                                    scanf("%d",&x[i][j][k]);
                        }
            }
            printf("\nArray Elements Are:");
            for(i=0;i<m;i++)
            {
                        printf("\n");
                        for(j=0;j<n;j++)
                        {
                                    printf("\n");
                                    for(k=0;k<p;k++)
                                    printf("%5d",x[i][j][k]);
                        }
            }

}
Read More

Double (or) Two dimensional arrays in C-language

Sunday, October 09, 2016 0
Double (or) Two dimensional arrays

If the Array name followed by two subscripts then it is called two dimensional arrays.      


Declaration of Two dimensional arrays:
The general format of a two dimensional array is:
                        Syntax:           datatype ArrayName[size1][size2];
Where,
  • datatype specifies the type of the elements that will be stored in the array.
  • ArrayName specifies the name of the array that follows rules as a valid identifier.
  • size1specifies row size i.e., number of rows.
  • size2specifies column size i.e., number of columns
Example:        int a[3][4];

For this, memory allocation will be:


INITIALIZATION OF DOUBLE DIMENSIONAL ARRAYS

Two dimensional arrays can be initialized into two different ways. Those are
1. Direct initialization
2. Through program execution

Direct initialization:
1) Like one-dimensional arrays, double dimensional arrays can also be initialized by placing a list of values enclosed within braces as:
Syntax:           datatype ArrayName[size1][size2] = {List of Values};

            Here, List of Values is separated by comma operator.
                        Example:        int a[2][3] = {10,20,30,40,50,60};                 
           

           Through program execution:

/* PROGRAM TO READ A DOUBLE DIMENSIONAL ARRAY AND PRINT IT */

main()
{
            int m,n,i,j,a[10][10];
            clrscr();
            printf("\nEnter the row size:");
            scanf("%d",&m);
            printf("\nEnter the column size:");
            scanf("%d",&n);
            printf("\nEnter the Array Elements:");
            for(i=0;i<m;i++)
            {
                        for(j=0;j<n;j++)
                        {
                        scanf("%d",&a[i][j]);
                        }
            }
            printf("\nArray Elements Are:");
            for(i=0;i<m;i++)
            {
                        printf("\n");
                        for(j=0;j<n;j++)
                        {                     
                        printf("%5d",a[i][j]);
                        }         
            }

}
Read More

Single (or) One dimensional arrays in C-language

Sunday, October 09, 2016 0
Single (or) One dimensional arrays


If an array followed by only one subscript, those arrays are called one dimensional arrays.
The general format of a one dimensional array is:
           
Declaration of an one dimensional array:  
           
Syntax:           datatype ArrayName[size];

Where,
  • datatype specifies the type of the elements will be stored in the array.
  • ArrayName specifies the name of the array. That arrayname should follow the valid identifier rules.
  • size indicates the maximum number of elements that can be stored inside the array.

Example:        int a[6];




INITIALIZATION OF ONE DIMENSIONAL ARRAYS

One dimensional array variables can be initialized into two different ways. Those are
1. Direct initialization
2. Through program execution (scanf() function)

Direct initialization:  We can initialize the elements of the array in the same way as the ordinary variables when they are declared.  The general form of initializing the one dimensional array is:
Syntax:           datatype Arrayname[size] = {List of Values};

            Here, List of Values is separated by comma operator.
                        Example:        int a[4] = {10,20,30,40};                    /* 10 20 30 40 */
                       

Through program execution:

/* EXAMPLE PROGRAM TO READ A ONE DIMENSIONAL ARRAY AND PRINT IT */

main()
{
            int n,i,a[10];
            clrscr();
            printf("\nEnter the no.of elements should be stored in array:");
            scanf("%d",&n);
            for(i=0;i<n;i++)
            {
                            scanf("%d",&a[i]);
            }
            printf("\nThe given array elements are:");
            for(i=0;i<n;i++)
            {
            printf("%5d",a[i]);
            }
            getch();

}
Read More

ARRAYS IN C-LANGUAGE

Sunday, October 09, 2016 0
 ARRAYS


  • Ø  An array is a collection of homogeneous/similar type of data items. The type of data items may be char, int, float etc..  The elements that are stored in successive memory locations. 
  • Ø  The array name followed by one or more subscripts, with each subscript is enclosed in square brackets. 
  • Ø  The number of subscripts determines the dimensionality of the array. 
  • Ø  Depending on the number of subscripts, the arrays can be classified into different types as:


Read More