Breaking News
You are here: Home / Notes / SEM-2 / C / Arrays in C

Arrays in C

Arrays in C

Arrays in C

Arrays in C

 

Arrays:

  • Array is a set of Collection of similar data type.
  • each elements having the same data type and the same name but index is different.
  • Array index is begins from 0.
  • It makes program coding Efficient.
  • Suppose I want to store 100 students name in that case I have to use 100 different variables, instead of taking this you can use declare only one Array for name.
int name[100];
  • In this example there can be store 100 students name  by declaring only one Array
  • Arrays occupy consecutive memory slots in the computer’s memory.
name[0], name[1], name[2], ..., name[99]
  • If an array has n elements, the largest subscript is n-1.
  • Types

One Dimensional Array :

  • A list of items can be given in one variable name using one subscript and such variable is Called a one-dimensional Array.
  • Syntax:
    • <datatype> array_name[size];
  • Eg. Int a[5];
  • Array occupy total number of elements * data size
  • In out example total elements are 5 and size of integer is 2 bytes so that 5*2=10 bytes
  • In Upper Example A is a Array Name in Which Max. 5 Values can be Store.
  • a[0]=12
  • a[1]=10
  • a[2]=20
  • a[3]=12
  • a[4]=34
  • a[5]=45
  • We Can initialize Array Elements at the time of Declarations.
  • Eg. Int a[5]={12,23,45,67,3};

Two Dimensional Array :

  • Here Data is stored two Dimensionally.
  • Eg. Suppose I want to Store the data like Student rollno,name and semester of 50 Students in that case I have to use 3*50=150 Variables but this is very tedious task. Instead of this you can use two dimensional Array.
  • Syntex :
    • <datatype> array_name[Rows][Columns];
      int n[10][20];
  • In Upper Example there is 10 Rows and 20 Columns, so that total size of the Array will be 10*20*2(for integer)=40 bytes.
void main( ) {
               int n[5],i;
               clrscr();
               for(i=0;i<5;i++)
               {
                  printf(“\n Enter a Date”);
                  scanf(“%d”,&n[i]);
               }
               for(i=0;i<5;i++)
               {
                  printf(“\n Value is %d”,n[i]);
               }
        getch();
       }

 

Array Initialization

  • As with other declarations, array declarations can include an optional initialization
  • Scalar variables are initialized with a single value
  • Arrays are initialized with a list of values
  • The list is enclosed in curly braces
int array [8] = {2, 4, 6, 8, 10, 12, 14, 16};
  • The number of initializers cannot be more than the number of elements in the array but it can be less in which case, the remaining elements are initialized to 0.
  • if you like, the array size can be inferred from the number of initializers by leaving the square brackets empty so these are identical declarations:
  • An array of characters ie string can be initialized as follows:
char str[10] = "Hello";

Download Paper Solutions from Below Link

About admin

Scroll To Top