C tutorial - Introduction of Array in C programming language

Programming languages or concepts

Intro. of array in C Language:-

  in this tutorial, we will learn about array we will learn to declaration, initialization and access elements of an array.


What is array:-

    An array is a collection of data items, all of the same type, accessed using a common name.
    Array is a variable that can store multiple values.
    Array is a collection of items stored at contiguous memory location, they are used to store similar type of elements as in the data type.
   
Representation of an array:-
Consider array name a and it's size is 5.
Notes: array index starting from 0th position
         int a[5];
   
                           a[0].   a[1].   a[2].     a[3].      a[4]





How to declare an array:-
 
 dataType arrayName [arraySize];

Ex,
       int mark[20];

Here, we declared an array, mark of integer point type, And its size of 20 meaning it can hold 20 integer point values it's important to note that the size and type cannot be changed once it is declared.

Initialization of Array:-

 It is possible to initialize an array during declaration.
Ex.
       int a[10]={8,2,0,8,6,8,2,2,6,6};

Here, the compiler known it's size is 10 as we are initializing it with 10 elements.
   initially elements are stored in 0 position.
a[0]=8
a[1]=2
a[2]=0
a[3]=8
a[4]=6
a[5]=8
a[6]=2
a[7]=2
a[8]=6
a[9]=6

Advantages of an array in C/C++:-

1). Easy access to all the elements.
2). Random access of elements using array index.
3). Traversal through the array becomes easy using a single loop.

Disadvantages of an array in C/C++:-

The elements of array is storing in consecutive memory location so insertion and deletion is defficult.
  We must know in advance that how many elements are to be stored in an array.
  Array is static structure it means that array is of fixed size the memory which is allocated to array can not be increased or reduce.

Types of Array:-

 There are two types array in c
1). One dimensional Array.
2). Multi dimensional Array.

1). One dimensional array:-
        In one dimensional array as a row , where elements are stored one after another (in sequence).

Syntax:-
   
dataType arrayName [size];

dataType:-
    It denotes the type of the elements in the array.

arrayName:-
     Name of the array. It must be a valid identifier.

size:-
    Number of elements an array can hold.

Ex.     int number[20];
           float stud [50];
            char ch[50];

Number is an array of type int, which can only store 20 elements of the int .
  Stud is an array 50 element of type float, which can only store 50 element of type float.
   ch is an array of type char, which can only store 50 elements of type char.

Program:


Consider:-  
 a - for storing of data
 x - element to be searched
 n - no of elements in the array
 i - scanning of the array


Searching element in array 

#include
#include
void main()
{
 int a[30],x,n,i;

 printf("\n Enter no of elements :");
 scanf("%d",&n);

 /* Reading values into Array */

 printf("\n Enter the values :");
 for(i=0;i ⟨ n;i++)
  scanf("%d",&a[i]);

 /* read the element to be searched */

 printf("\n Enter the elements to be searched");
 scanf("%d",&x);

 /* search the element */

 i=0;       
 while(i ⟨ n && x!=a[i])
 i++;

 if(i ⟨ n) /* Element is found */
         printf("found at the location =%d",i+1);
 else
         printf("\n not found");

 getch();
}



Two Dimensional Array :-


   In two dimensional array elements are stored in the formate of the table. i.e. in the form of row and column.
    There are two index:-
     1). Row Index.
     2). Column Index.

 1) Row Index:- this index indicates the no.of rows.
 Column Index:- it indicates the no. of Columns.

Declaration of two dimensional Array:-

Syntax:-
   
  dataType arrayName [row size] [Column size];

Eg.
          int a[4][4];

Initialization of Two Dimensional Array :-

 dataType arrayName [row size] [ Column size]={ { r1 C1 Value, r1 C2 Value,........}, { r2 c1 value,r2 c2value....}........};

Eg.
       int mat_A[3]2]={ {1,2},{3,4},{5,6} };


2D array using c program:

Program for accept and print 2×2 matrix from user:

   Consider:

   i : For Counting Rows
   j : For Counting Columns


#include<stdio.h>

int main() {
   int i, j, a[3][3];

   for (i = 0; i < 3; i++) {
      for (j = 0; j < 3; j++) {
         printf("\nEnter the a[%d][%d] = ", i, j);
         scanf("%d", &a[i][j]);
      }
   }

   //Print array elements
   for (i = 0; i < 3; i++) {
      for (j = 0; j < 3; j++) {
         printf("%d\t", a[i][j]);
      }
      printf("\n");
   }

   return (0);
}





For more details
close