What Is an Array?  It is the collection of the same type of data. The array is derived data type because the declaration is predefine and...

Arrays

/
0 Comments
What Is an Array? 
  • It is the collection of the same type of data. The array is derived data type because the declaration is predefine and definition is user-defined.
  • You now know how to declare a variable with a specified data type, such as char, int, float, or double. In many cases, you have to declare a set of variables that have the same data type. Instead of declaring them individually, C allows you to declare a set of variables of the same data type collectively as an array.
  • An array is a collection of variables that are of the same data type. Each item in an array is called an element. All elements in an array are referenced by the name of the array and are stored in a set of consecutive memory slots.
  • Declaring Arrays
         The following is the general form to declare an array:
         data-type Array-Name[Array-Size];

  •  Here data-type is the type specifier that indicates what data type the declared array will be. Array-Name is the name of the declared array. Array-Size defines how many elements the array can contain. Note that the brackets [ length] are required in declaring an array.
  • For example, an array of integers is declared in the following statement, 
  • int array_int[8];
  • where int specifies the data type of the array whose name is array_int. The size of the array is 8, which means that the array can store eight elements (that is, integers in this case).


      Indexing Arrays 
  • After you declare an array, you can access each of the elements in the array separately.
  • For instance, the following declaration declares an array of characters: 
          char day[7];
  • You can access the elements in the array of day one after another.
  • The important thing to remember is that all arrays in C are indexed starting at 0. In other words, the index to the first element in an array is 0, not 1. Therefore, the first element in the array of the day is day[0]. Because there are 7 elements in the day array, the last element is day[6], not day[7].
  • In the array, we can store only one type of data.   For example, int a[5];// array has a length five and data type is int so cannot store char or any other data type in this array.
  • The array is nothing but it is kind of variable which has multiple variables. We can access array variable by it position [1].This is example


  • Initializing Arrays 
  • With the help of the array element references, you can initialize each element in an array.
  • For instance, you can initialize the first element in the array of day, which was declared in the last section, like this: 
  • day[0] = `S';
  • Here the numeric value of S is assigned to the first element of day, day[0].
  • Likewise, the statement day[1] = `M'; assigns `M' to the second element, day[1], in the array.
  • The second way to initialize an array is to initialize all elements in the array together. For instance, the following statement initializes an integer array, integer:
  •  int arInteger[5] = {100, 8, 3, 365, 16};

 The array is of 3 types:- 
  • Multidimensional Arrays 
  • So far, all the arrays you've seen have been one-dimensional arrays, in which the dimension sizes are placed within a pair of brackets [ and ].
  • In addition to one-dimensional arrays, the C language also supports multidimensional arrays. You can declare arrays with as many dimensions as your compiler allows.
  • The general form of declaring an N-dimensional array is
  • data-type Array-Name[Array-Size1][Array-Size2]. . . [Array-SizeN];
  • where N can be any positive integer.
  
  •  Two-dimensional array
  •  The two-dimensional array, which is widely used, is the simplest form of the multidimensional array, let's focus on two-dimensional arrays in this section. Anything you learn from this section can be applied to arrays of more than two dimensions, however.
  • For example, the following statement declares a two-dimensional integer array:
  • int array_int[2][3];
  • Here there are two pairs of brackets that represent two dimensions with a size of 2 and 3 integer elements, respectively.
  • You can initialize the two-dimensional array array_int in the following way:
            array_int[0][0] = 1; 
            array_int[0][1] = 2;
            array_int[0][2] = 3;
            array_int[1][0] = 4;
           array_int[1][1] = 5;
           array_int[1][2] = 6; 
  • Note that array_int[0][0] is the first element in the two-dimensional array array_int; array_int[0][1] is the second element in the array; array_int[0][2] is the third element; array_int[1][0] is the fourth element; array_int[1][1] is the fifth element; and array_int[1][2] is the sixth element in the array


  • Program:
  #include<stdio.h>

  #include<conio.h>
  void main()
 {
 int arr[10];//arr is array name and 10 is length name
 int i, b;
 printf("Enter size of array: ");
 scanf("%d", &b);
 printf("Enter %d elements in the array : ", b);
 for(i=0; i<b; i++)
 {
 scanf("%d", &arr[i]);// asssigning value in array with repect 
 }
 printf("\nElements in array are: ");
 for(i=0; i<b; i++)
 {
 printf("%d ", arr[i]);
 }
 getch();
}
  •   Output:

No comments:

Cybercry 2018. Powered by Blogger.

Popular Posts