Arrays of C program

Hello friends...!!!  Welcome back.  We studied loops, data types, operators in previous post. Now we will study a new topic, which is array. First we will discuss on data types.
Introduction:-
In C language, Data types are divided into three different categories.
  1.  Fundamental data types
  2. Derived data types.
  3. User defined data types.

 Fundamental Data Types:-
            Character, Integer, Float, Double etc.
Derived Data Types:-
            Arrays, Function, Pointers.
User-defined Data Types:-
            Structure, Union, Enumeration .
Definition:-
Array is collection of variables of same data type. It is used to represent a list of names and list of numbers and it also provides a convenient structure for representing a data.It is representedas shown below:-
                                    Int a[3];
Array is divided in three types:-
·                                           One Dimensional Array
·                                          Two Dimensional Array
·                                         Multi Dimensional Array

One Dimensional Array:-

The syntax is:          data type array name[size];
Example                         int a[3];

            Data type is the types of array element. Array name is name of variable which represent an array. [] symbol is defined the size of the array. So in example, int is a data type. a is an array variable and 3 is an array size. Array index or element starts from 0.
Means a[3]= a[0],a[1] a[2].  a[0] is first element and a[2] is last element. We can store any value in each of these elements. Size will be counted from 0. If array size is 5 that means elements is 0 to 4.
Initialization of one dimensional array:-

                        Syntax is:      data type array [size] = {values};
Example:       int name[3]={1,2,3};    <-- These are values not an elements.

                                            When we initialize value in the array, we use this method. In the brace value of array must be written. It is also initialized like:
                                    Int name[]={1,2,3};
                                          
It is also valid. If we dont mention values and size while initializing an array, computer can understand size automatically.
When we use character data type, it should be initialized as mentioned below:
            char name[18]= New Age Informers;
  
When we write value of each element of array and size is not equal to values so all characters stored in each elements and the left out elements will be filled with the NULL character (\0).
char name[5]={ j, a, y,\0, \0 }
char name[]={jay};                            It is also valid.
     
/*Write a program to sum and average of n numbers*/


Two Dimensional Array:-

            Syntax:            data type name[size1][size2];
                                  data type name[row][column];

It is used for solving matrix and mathematical questions.
Method is same as that in one dimensional array. But in different styles like:

            Example:            int table[3] [2] = {1,1,1,0,0,0};
                                    int table[3][2] = { {1,1,1}, {0,0,0} };
                                    int table[3][2] = {
                                                                        {1,1,1},
                                                                        {0,0,0}
};

If the values are missing in during initialization, they are automatically set to zero. Example
            int table[2] [3] = {
                                                {2,1},
                                                {4}       
};

It will initialize the first two elements of the first row to one, the first element of the second row to two, and all other elements to zero.
/* Write a program of 3x3 matrix*/

 Multi Dimensional Array:-

Syntax: data type name[s1][s2][s3]..........[sn];

It also same as one dimensional and two dimensional but here more than one sizes are included.
I hope your basic concept of this topic is clear. If there is any doubt, please feel free to mention it in the comment box. If you are interested you may join our facebook group New Age Informers. My next post will be on pointers.

Until then BYE BYE!!!!



Comments