Usage of Arrays

An array is said to be a collection of some data items, those all are of the same type and all can be accessed using one common name. We can say that an array is a group of multiple variables having the same data type, those differ from their index of the array.

If one wants to use 100 int data type variables then he can use variables var0, var1, var2, var3, var4, …,var99 having int data type. So instead of making different 100 variables, one can use an array var[100] of int data type.

For Example, One food restaurant has 50 food items. So to save the price of each food, it needs 50 int variables. It can be very hard to take the price of food items from the chef and display it. So we can make an array for 50 food items, like int food_item[50].

It can be of any data type like int, char, float, double.

Initialization

The syntax to initialize the array is as given below.

<data type>    <array name> [<index>];

Here index is the number of variables in an array. Suppose we need 5 variables of type char then we should write int arr[5]. Here index 5 means 5 variables of data type int. This index starts from 0 so it can not be negative. To access any element of an array, we use an index. As the index starts from 0, to access the 1st element of an array, we use arr[0] .To use the 2nd element of an array, we use arr[1] and so on.

Size of an array:   size of data type * index

The size of a whole array is a multiplication of the size of data type which we used to declare the array and index which we wrote while initializing the array. The index will always start from 0. Array elements will have names from <array name>[0]  to <array name>[index-1].

Here in the diagram below, we are initializing an array of 6 indexes. So in the below example, we get 6 variables: arr[0], arr[1], arr[2], arr[3], arr[4], arr[5] of int data type.

int arr[6];                      //hare size of arr will be 6*2 = 12 bytes

Some valid and invalid array initializations are as given below. We can not use any variable as an index of the array. This is shown by invalid initialization as listed below. But instead of a constant index, we can write macro. Macro is a constant, which can be declared using #define at the starting of the program.

Valid and invalid initialization

We can use any constant as an index, multiplication of a constant with macro, summation of multiple macros, pre or post-increment/decrement with macro as an index of an array. Below is the table for valid declaration of array.

arr[3] arr[2*i]
 arr[i] arr[j++]  //post increment  :   j=j+1 , arr[j]
arr[i+j] arr[i]++  // post increment :  arr[i] = arr[i] + 1

Methods to give value to an array

We can give value to an array at compile time or run time. These two methods are listed below.

 Method to give value at compile time

In this method, we give value to an array at the time of declaring the array. So that is why this method is called “method to give value compile time”. In the below example, ‘a’ is an array of 5 elements and we are assigning 5 values:10,20,30,40,50 to array elements one by one.

int a[5]={10,20,30,40,50};       // which means   :     a[0] = 10  , a[1] = 20 , a[2] = 30 , a[3] = 40 ,  a[4] = 50
Method to give value at run time

In this method, we ask a user to give value to an array at the time of running the code. This method is used when we want a user to give whatever value he wants for an array. The below example is also of array ‘a’ which is of 5 bits.

int a[5];
for(int i=0;i<5;i++)
  scanf(“%d”,&a[i]);
Complete example of taking 5 subject marks from user and display them

In the below example code, i is an array of 5 elements. In the below program we are using “Method to give value at run time” to take values of array elements. The first for loop is taking values of array elements from the user by using this method. So to show the index number, we are using int i which will change from value 0 to 4. The second for loop is printing all elements from 0 to 4 of array i. For this we are using the same variable i to show the index number while printing.

#include<stdio.h>
#include<conio.h>
void main()
{

  int sub[5],i;
  clrscr();

  // this for loop takes value of array elements
  for( i=0 ; i<5 ; i++ )  
  {
    printf(“enter value of sub[%d] : ”,i);
    scanf(“%d”,&sub[i]);
  }
  printf(“ displaying marks of all 5 subjects \n “);

  // this for loop prints value of array elements
  for( i=0 ; i<5 ; i++ ) 
  {
    printf(“\n value of sub[%d] : %d  ”,i,sub[i]);
  }
  getch();
}

Learning from this blog:

  1. Syntax of 1-D array
  2. How to access array elements using index?
  3. Valid – Invalid array declaration
  4. Program of how to get the value of array and display it
  5. Variation in declaring index
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments