Arrays in C

                                   ARRAYS (C Programming)

Definition:

  1. An array is a collection of similar data type items stored at contiguous(Continuous) memory locations.
  2. The array is the simplest data structure where each data element can be accessed by using its index number.
  3. if you want to store the marks of a student in 6 subjects, then traditionally we need to define different variables for the marks in the different subjects. But Instead of that, we can define an array that can store the marks in each subject at the contiguous memory locations.


Declaration:

  1. In C programming Array can be created as 
  2. data_type array_name[array_size]
  3. Here data_type is to declare whether we want to store integers or characters or floating-point numbers etc.
  4. Array_name is some user-defined name given to your array to be created.
  5. Array_size is the size of the array to be created, as the array must be created with a predefined size


Example:

int shsarray[10];

The array with the name shsarray will be created with size 10.


Initialization:

Initialization of the array can be done along with the declaration.

The index is the number by which we can access the elements of an array.

Index starts from 0 to (size of the array)-1

The array can be initialized in two ways 

1)At the time of the creation 

Example:

int shsarray[10]={1,2,3,4,5,56,7,8,8,5};

Or can also do without declaring the size of the array in this case like below example.

Int shsarray[]={1,2,3,4,5,5,6,67};

2)Individually using indices

int shsarray1[3];

shsarray[0]=34;

shsarray[1]=65;

shsarray[2]=66;


    34

    65

      66

      0                     1               2

Can be accessed by using the same indices like shsarray[1]

Will give output as 65.


Advantages of Using Arrays :

  • Can store and access data items with fewer lines of code

  • Can search for an element easily using indices.

  • Can perform sorting operation easily.


Disadvantages :

  • We can’t change the size of the array dynamically while running if we want to increase or decrease the size of the array because array size should be fixed, at the time of declaration.


Sample program-1:

Output:

1

4

6

8

Sample Program-2:

Output:

101

23

10

9

7

Note: To do in ascending order just change as a[j]<a[i]


Sample program 3:

Output:

Printing Largest element of the array…

10



2 Dimensional Array

  1. The two-dimensional array can be defined as an array of arrays. 
  2. The 2D array is organized as matrices which can be represented as the collection of rows and columns. 


Declaration:

Syntax:

data_type array_name[rows][columns];

Here we have to represent a number of rows and a number of columns in the representation.

Example:

Int twodarray[2][3];

Here a two-dimensional array of name twodarray of 2 rows and 3 columns has been created.


Initialization:

Initialization can be done in two ways same as in the 1D array

1)At the time of the creation 

Example:

int twoDarray[2][3]={{1,2,3},{4,5,56}};

If you initialize without declaring the size of the array, like in the 1D Array

this will not work with 2D arrays. We will have to define at least the second dimension of the array

example.

int twoDarray[][3]={{1,2,3},{4,5,56}};

2)Individually using indices

int twoDarray[2][3];

twoDarray[0][0]=56;

twoDarray[0][1]=2;

twoDarray[0][2]=5;

twoDarray[1][0]=1;

twoDarray[1][1]=15;

twoDarray[1][2]=4;


        00                01              02

      56

      2                  

    5

      1

    15

    4

      10                 11               12       

Can be accessed by using the indices like twoDarray[1][0];

Will give output as 1.


Sample Program 1:

Input:

A[0][0]: 12

A[0][1]: 23

A[1][0]: 34

A[1][1]: 45

Output:

12    23

34    45




Note: More examples will be covered in Problems on C page

Arrays using structures and Arrays using pointers will be covered in the structures and pointers concept respectively

Similarly you can also learn the structures, unions and pointers concept in C programming Language.

If you have any doubts or you want us to create some more concepts or if you can't solve any problems please give it in the form of your comments.


If you like the content we had given and understood it well then give your valuable rating for this page and comment your experience in the comment Box .





Categories:

2 comments:

  1. Replies
    1. happy to here that from you please be following our Blog you can gain a lot of knowledge

      Delete