Necessity for array

C language has different data types and variables to hold rutime parameter. One variable is to store one runtime parameter. Now if there are multiple values to store the we can take multiple variables. Here we are storing roll no. of students in integer type variables.

Variable Driven Approach

int roll1, roll2, roll3;
printf ("Enter roll no. of student 1#");
scanf ("%d", &roll1);
printf ("Enter roll no. of student 2#");
scanf ("%d", &roll2);
printf ("Enter roll no. of student 1#");
scanf ("%d", &roll3);

We have taken three variables to store roll no. of three students. This approach is not scalable and variables are also scattered and can be accessed by their individual names. If there is any facility to group these variables inside one entity and individual elements can be accessed via an index then this situation would be more manageable. C language already has a feature called array to deal with this situation. Array can hold any number of elements and it is accessed through a single variable name.

Definition

Array is a collection or sequence of a same data type objects inside a logical variable name where individual objects can be accessed by an index.

Array is a grouping of same type of variables of either C language defined types or an user defined type. Array is very useful when dealing with same type of variables. Each individual entity of an array is called element. Elements are placed sequentially next to next in the memory. Thus total size of the entire array is the sum total of size of all elements.

Array Driven Approach

#define MAX_STUDENTS 3
int i;
int roll[MAX_STUDENTS];
for (= 0; i < MAX_STUDENTS; i++)
{
  printf ("Enter roll no. of student %d#", i);
  scanf ("%d", &roll[i]);
}

Array Elements in memory

Array is a sequence of variables called elements placed in memory in accending order. It start with index = 0 which is element 1 and ends with index = n-1 with is element N.

ARRAY

An array of integer with N elements

There should be a defined number of elements in an array thus it is mandatory to give the element count while defining an array. This element count can be a macro of a positive integer or a constant variable defined earlier. Now we will discuss more on this with examples.

/* Simple Declaration N=100 */
int roll [100];

/* using macro to hold the count */
#define MAX_STUDENTS 100
int roll [MAX_STUDENTS];

/* using a constant variable for count */
const int max_students = 100;
int roll [max_students];

/* Below Declaration not valid */
int max_students = 100;
int roll [max_students];

Size of Array

Array is a sequence of elements in memory. Thus it will give the size which is equal to the size of individual element times of the number of elements. Same way we can get the element count if we divide the size of array by the size of each element.

/* Calculate array size */
array_size = sizeof (roll);
/* Calculate element count */
array_elements = sizeof (roll) / sizeof (roll[0]);

Array initialization

Array can be declared in global,Local and static scope and the rule for initial value is same as it is applicable to normal variables. This is reason array elements are initialized before they are accessed in runtime.


/* Array initialization at declaration */
int roll [5] = { 1, 2, 3, 4, 5 };

/* No element count: We may skip the element count if we are defining an array and the same time initializing elements. */

int roll [] = { 1, 2, 3, 4, 5 };

/* Compiler calculates element count with the help of the elements given at initialization. */

/* Some Elements not initialized: Array initializer has a special property of setting additional uninitialized elements to zero. This is true for both global and local arrays. */

int roll [10] = { 1, 2, 3, 4, 5 };
/*same as*/
int roll [10] = { 1, 2, 3, 4, 5, 0, 0, 0, 0, 0 };

/*Initializing more elements than count:*/
int roll [4] = { 1, 2, 3, 4, 5 }; /* This is an error */

/* Array Initialization at run-time */
int i;
for (= 0; i < MAX_STUDENTS, i++)
{
  /* Valid Roll no. will be entered when new student get enrolled */
  roll[i] = 0;  /* Default 0 */
}

Array elements access for writing

We are assigning roll no. to the student's record as they have enrolled.

int i;
int roll;
while (< MAX_STUDENTS)
{
  printf ("Enter roll no. of student %d#", i);
  scanf ("%d", &roll[i]);
  i++;
}

Array elements access for reading

We are showing student roll no. as they are enrolled.

int i;
for (= 0; i < MAX_STUDENTS, i++)
{
  printf ("Student %d has roll no#", i, roll[i]);
}

Here array has element range from 0 – (n-1) range and it is linear in a single Dimension. Now there are possibilities to have further groupings inside each element or in simple words each can have another dimension. Thus there comes multidimensional array. We will discuss more in our next section.

About our authors: Team EQA

You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.

#