Tuesday, 5 March 2019

Single-Dimensional Array in C (Part 1)

What is an array?
  • An array is the collection of elements of single/similar type.
  • These similar elements could be of all ints, all floats, all chars etc.
  • The elements of an array are referred by a single name.
  • All elements of an array must be of same type.

Why do we need arrays?
Suppose we want to store marks of 40 students of a class. There are two options:-
  1. Declare 40 variables to hold marks of 40 students. Or
  2. Declare an array of size 40 to hold marks of 40 students. 

Option 2 is better compared to first one. Because it would be much easier to handle single variable than managing 40 variables.

Types of arrays
  1. Single Dimensional (1 D) array
  2. Multi-Dimensional (n D) array where n=2,3,4…..

Single Dimensional (1 D) Array
Definition
·         Conceptually you can think of a one-dimensional array as a row, where elements are stored one after another.
·         Memory representation of 1-D array:
Declaring 1 D array:
  • Like a variable, we must declare an array before using it.
  • Declaring an array tells the compiler the kind of values an array will hold and its size.
  • The general syntax for declaring an array is as follows
               data_type   array_name[size_of_array];


   Examples:
  • int marks[10];               // array marks holds 10 integer values
  • float salary[5];             //array salary holds 5 real numbers
  • char name[4];           //character array name can hold 4 characters


Initialiazing 1-D array
·         To initialize the elements of an array, you just specify the list of initial values between braces and separate them by commas in the declaration.
Syntax
·         array_name[size]={value1, value 2, value 3,…..};
Example:
  • marks[10]={67,87,89,90,98,65,79,86,45,33};
  • salary[5]={,887.0,689.0,970.0,968.0,695.0};
  •  name[]={‘D’,’E’,’E’,’P’};

Declaration and initiazation
·         Declaration and initialization can be grouped together.
Syntax
·         Data_type array_name[size]={value1, value 2, value 3,…..};
Examples:
·         int marks[10]={67,87,89,90,98,65,79,86,45,33};
·         float salary[5]={,887.0,689.0,970.0,968.0,695.0};
·         char name[]={‘D’,’E’,’E’,’P’};

Accessing array elements:
The elements of an array can be accessed by specifying array name followed by subscript or index inside square brackets (i.e []).
For example, accessing elements of above marks array,
first element=marks[0];
second element=marks[1];
element at ith index=marks[i];
Notes:
  1. Array subscript or index starts at 0.
  2. If the size of an array is n then the first element is at index 0, while the last element is at index n-1.
  3. The first valid subscript (i.e 0) is known as the lower bound, while last valid subscript is known as the upper bound.
  4. The C language doesn’t check bounds of the array. It is the responsibility of the programmer to check array bounds whenever required.
  5. Elements of an array are stored in continuous memory locations.
  6. An array is also known as a subscripted variable.
  7. Before using an array its type and dimension must be declared.
  8. If the array is initialized where it is declared, mentioning the dimension of the array is optional



Please comment if you find anything incorrect, or you want to improve the topic discussed above. 

No comments:

Post a Comment