Arrays in C | Introduction to 1-D Arrays | User-Defined Data Types in C programming



In the previous tutorials, we were manipulating only 1 single data entry and that was even easy for us. But imagine what if we are to manage a large amount of data each having the same specifications, example, to create a program for managing the roll numbers of a class consisting of 50 students. If we use go on with our old static approach then we need to declare about 50+ variables. That is why, the concept of Array comes into play.

*****Theoretical Definition of Array*****


Array is a structured data type in C. It is defined as finite ordered collection of homogeneous data providing random access of memory

  • It is finite because data size must be defined.
  • It is ordered because all the elements need to be stored in memory at contiguous locations.
  • It is homogeneous because all the elements must be of same type.

Arrays can have as many dimensions as possible like 1-Dimensional arrays, 2-D dimensional arrays... N-dimensional arrays etc.

So instead of using different variables we can use arrays for storing roll numbers, only condition is that, they must be of a same type.


As we know the size of a integer variable is 4 bits, supposing first value is at address 1000 then next will be after 4 bits, i.e, 1004. These are not actual memory locations and are supposed by us for our convenience. In actual case, the memory addresses of locations vary from system to system. But in arrays the same set of rules is followed by them.

*****Declaring Arrays In C***** 


Like any other data type, Arrays also need to be declared before using. To declare an array in C, we have to provide its data type, name and size;

type array_name[size];

This is 1D array just as the RAM of the computer. The size must be an integer expression having value greater than zero. For creating an integer array named 'arr' of size 50,

int arr[50];

*****Initialization of an Array***** 


After declaring the array we need to initialize it with values otherwise garbage values ( any random piece of memory ) will be displayed if we try to access the elements of the array. Initialization is done as follows :

type array_name[size] = { value1 , value2 , ... , valueN };

example:

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

* If we provide more values than the size of the array, then a compile time error will occur or it will ignore the extra values, this actually depends on the compiler, what action would be taken in such case.
We can also omit the size in case of compile time initialization and the compiler will calculate the size automatically based on the number of values.

int arr[] = { 9,11,23,43,0 };     //automatically size is taken 5

*****************************************************

* Indexing in case of arrays is done from 0th index. That is, in the above example value 9 is stored at arr[0] where its index is 0,then 11 is at arr[1] and so on. At last the value 0 is stored at arr[4]. Hence, the last index of the array is always SIZE-1.

A loop is required to access each element of the array, so that we can iterate through each index and access the values stored at each index.

Also we can initialize each array element one by one. Like arr[0]=1; arr[1]=32; and so on.

Even a Loop can also be used to enter values in the array:

      for ( int i=0 ; i<SIZE ; i++ )
           scanf("%d",&arr[i]);

*****************************************************

Using Arrays in Programs:

Program 1.

#include <stdio.h>
int main()
{
int i;
int age[5];
age[0]= 34;
age[1]= 43;
age[2]= 45;
age[3]= 23;
age[4]= 54;  
for(i=0 ; i<5 ; i++) 
        {
          printf("%d\t", age[i]);
}
    return 0;
}


Program 2.

#include <stdio.h>
int main()
{
 int i;
 int age[] = {1,2,3,1,4,5};     //array of size 6 will be created     for(i=0 ; i<10 ; i++)          //automatically
  {
    printf("%d\t", age[i]);
  }
return 0;
}


Program 3.

#include <stdio.h>
int main()
{
int age[10], i;
printf("Enter the age of 10 persons :");
for(i=0; i<10; i++)
  scanf("%d", &age[i]);     
for(i=0; i<10; i++) 
  printf("%d\t", age[i]);
return 0;
}


Ritish

Just a novice blogger

Post a Comment (0)
Previous Post Next Post