Array in C





In programming, one of the frequently arising problem is to handle numerous data of same type.

Consider a situation, you want to store marks of 100 students, for that what will you do? you will make 100 variables like marks1,marks2 etc... to store the value.

Instead of declaring individual variable, you can declare one array variable such as 'marks' of size 100.


Array's Definition :

C provides a data structure called the array, which can store a fixed-size sequential collection of elements of the same type.

Key Points
  • An Array is a collection of similar data items stored in contiguously on memory.
  • Each member of an array is identified by unique index assigned to it.
  • An index is a positive positive integer enclosed in [ ] placed immediately after the array name.
  • Valid index is from 0 to less than the size of array.(0 <= index < size)

Syntax

data_type array_Name[size] ;
for e.g:
int marks[100];
float price[10];
char name[25];

Here with the help of below image you can understand the array representation in memory.
Suppose you create an array with size 10 like.
int marks [10] prowessapps.in

Array initialization :

  • By default, arrays of local scope (for example, those declared within a function) are left uninitialized. This means that none of its elements are set to any particular value; their contents are undetermined at the point the array is declared.

    Example:

    #include<stdio.h>
    int main() {
      int marks[10] ;
      printf("Value at index 0 %d",marks[0]);
      return 0;
    }
    OUTPUT:
    Con't predict it will give
    different value in different
    run.

  • So, the elements in an array can be explicitly initialized to specific values when it is declared, by enclosing those initial values in braces { }.
    Example:
    int marks[5] = {80 ,60, 70, 85, 75};
    The above statement will declare an array with size 5, and can represented like this prowessapps.in
  • You can initialize some element of array, in that condition rest of indexes will initialize with 0 in case of numeric(int, float, double) array and white space in case of char array;
    Example:
    int marks[5] = {80 ,60, 70};
    The above statement will initialize the 3 indices with respective value and rest will be with 0. prowessapps.in
  • You can initialize array without specifying the size as well.In this case the number element you will initialize ,become the size of array.;
    Example:
    int marks[] = {80 ,60, 70};
    The above statement will initialize the 3 indices, so in this case array size will be 3. prowessapps.in

Accessing Array Elements :

  • To access the values of array use the following syntax:
    array_name[index];
    For example ,you want to access the value of 3rd element, you will write
    //since 3rd element will at index 2.
    int x = marks[2];

  • In the same way if you want to assign value to any element you can write:
    array_name[index] = value;
    For example ,you want to assign the value at 3rd element, you will write
    //since 3rd element will at index 2.
    marks[2] = 90;

Programs to Understand Array

Program to print all element of array.
#include<stdio.h>
int main() {
  int ar[5] ={5, 10, 15, 20, 25};
  int i;
  for(i=0; i<5; i++)
   {
     printf("ar[%d] = %d",i,ar[i]);
   }
  return 0;
}

OUTPUT:

ar[0] = 5
ar[1] = 10
ar[2] = 15
ar[3] = 20
ar[4] = 25

Program to initialize the array element by user and print all element of array.
#include<stdio.h>
int main() {
 int ar[5] ;
 int i;
 for(i=0; i<5; i++)
  {
   printf("Enter ar[%d] element :");
   scanf("%d",ar[%d]);
  }
  printf("****************\n");
  printf("Entered elements are\n");
  for(i=0;i<5;i++)
   {
     printf("%d, ",ar[i]);
   }
 return 0;
}

OUTPUT:

Enter ar[0] element :5
Enter ar[1] element :10
Enter ar[2] element :15
Enter ar[3] element :20
Enter ar[4] element :25
****************
Entered elements are
5, 10, 15, 20, 25, 


Types of Array :

  1. Single Dimension Array
    [ ] defines the dimension of an array. If you write one bracket( [ ] ) followed by array name means that array is single dimension array.
    int ar[10];
  2. Multi Dimension Array
    C allows us to create an array with more than one dimension.
    This array is called multi-dimension array.
    In C,Multidimensional arrays can be described as "arrays of arrays".

    Syntax

    
    data_type array_Name[size1][size2]...[sizeN];
                                    
    for e.g:
    int ar[3][4];//two dimensional array
    int arr[2][3][4];//Three dimensional array.

Two-Dimensional Array:

To declare two dimensional array use the following syntax:
data_type array_Name[ m ][ n ];

A two dimensional array can be imagined as table, which will have m number of rows and n number of columns.

For example a two dimensional array a, which contains 3 rows and 4 columns can be represented as:
prowessapps.in

Initialization Two-Dimensional Array:

We can initialize two dimensional array in many ways:

  1.  
    int a[3][4] ={
                   {1,2,3,4},//for row 0
                   {5,6,7,8},//for row 1
                   {9,10,11,12}//for row 3
                  };
                                    

  2. The following initialization is equivalent to above example:
    int a[3][4] ={1,2,3,4,5,6,7,8,9,10,11,12};

The above statement will initialize an array can be represented like this:


Accessing Two-Dimensional Array Elements:

As you access the single dimension array by providing the index number as subscript, in the same way you can access the two-dimensional array's element by providing the subscript as well, i.e., row index and column index.Syntax is:
array_Name[row-index][column-index];
for e.g: You want to access 4th element from the 2nd row of the array.
int x = a[1][3];

Programs to Understand two-dimensional Array

Program to print all element of array.
#include<stdio.h>
int main() {
int a[2][3] = {{1,2,3},{4,5,6}};
int i,j;
for(i=0;i<2;i++)
for(j=0;j<3;j++){
 printf("ar[%d][%d] = %d",i,j,ar[i][j]);
 printf("\n");
}
return 0;
}

OUTPUT:

a[0][0] = 1
a[0][1] = 2
a[0][2] = 3
a[1][0] = 4
a[1][1] = 5
a[1][2] = 6

Program to initialize the array element by user and print all element of array.
#include<stdio.h>
int main(){
int a[2][3];
int i,j;
for(i=0;i<2;i++)
for(j=0;j<3;j++){
 printf("a[%d][%d] element :",i,j);
 scanf("%d",a[i][j]);
}
printf("*****************\n");
printf("Entered elements are:\n");
printf("*****************\n");
for(i=0;i<2;i++)
for(j=0;j<3;j++){
printf("a[%d][%d] = %d",i,j,a[i][j]);
printf("\n");
}
return 0;
}

OUTPUT:

a[0][0] element :1
a[0][1] element :2
a[0][2] element :3
a[1][0] element :4
a[1][1] element :5
a[1][2] element :6
*****************
Entered elements are:
*****************
a[0][0] = 1
a[0][1] = 2
a[0][2] = 3
a[1][0] = 4
a[1][1] = 5
a[1][2] = 6

Next topic is string in C





 












Training For College Campus

We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.

Please mail your requirement at info@prowessapps.in


Projects For Students

Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.

Students can mail requirement at info@prowessapps.in


CONTACT DETAILS

info@prowessapps.in
(8AM to 10PM):

+91-9026070669 , +91-9451396824

© 2016, prowessapps.in, All rights reserved