C Arrays

C language provides a capability called ‘array’ that enables the user to design a set of similar data types. Very often, one needs to process collections of related data items, such as the addition of fifty numbers, test scores of students in a university, a set of measurements resulting from an experiment, income tax tables, etc. One way of handling such a situation would be to declare a new variable name for each of these data items. This approach obviously is quite cumbersome, if not altogether impossible.

A better way to solve the problem is to use an array of a corresponding data type. This enables the user to access any number of relative data types using a single name and subscript.

Definition

An ordered finite collection of data items, each of the same type, is called an array, and the individual data items are its elements. Only one name is assigned to an array and specifying subscript references individual elements.

A subscript is also called an index. In C, subscripts start at 0, rather than 1, and cannot be negative. The single group name and the subscript are associated by enclosing the subscript in square brackets to the right of the name.

Consider an example where marks of some students are stored in an array named mark, then mark[0] refers to the marks of first student, mark[1] to the marks of second student, mark[10] to the marks of eleventh student and mark[n-1] to the marks of nth student.

An array has the following properties:

  • The type of an array is the data type of its elements.
  • The location of an array is the location of its first element.
  • The length of an array is the number of data elements in the array.
  • The storage required for an array is the length of the array times the size of an element.

Arrays, whose elements are specified by one subscript, are called one-dimensional arrays. Arrays, whose elements are specified by more than one subscript, are called multi-dimensional arrays.

Program without array having different variables:

main()
{
   int a,b,c,d;
   a=20;
   printf("%d",a);
   b=30;
   printf("\t%d",b);
   c=40;
   printf("\t%d",c);
   d=50;
   printf("\t%d",d);
}

Output is as follows:

20   30   40   50

Program with array having same variable name:

main()
{
    int a[50], i, n;
    printf("\n How many number you want to enter:");
    scanf("%d",&n);
    printf("\n Enter the %d different elements in the array :\n ",n);
    for(i=1;i

Output is as follows:

How many number you want to enter: 5
Enter the 5 different elements in the array:
Enter the 5 different elements in the array:
20   30   40   50

Different array elements are:
20   30   40   50

So array will solve the problem with the use of a lesser number of variables and variables having the same name.

Types of Array

There are broadly two types of arrays, namely:

  • Linear Array
  • Non-Linear Array

Linear Array

This type of array is also called one Dimensional Array. This is also called a list array. Another name of the linear array is a single-dimensional array. These arrays are of ladder type. In the linear array, only one subscript is used. It is written either in row or in column form.

The syntax to define or declare linear array is:

data-type arrary-name[size];

where data types are integer(int), real(float), double and character(char).

For example, some of the valid one dimensional array declaration statements are written as below:

int a[50];
float f[50];
char n[20];

Example Program:

/*Below program showing Liner array...*/
#include <stdio.h>
#include <conio.h>
void main()
{
    int a[5],i,sum=0;
    clrscr();
    printf("\n Enter the Number:");
    for(i=0;i

Output is as follows:

Enter the Number:1
2
3
4
5
  
  After the Inputting Array:

a[0]=1
a[1]=2
a[2]=3
a[3]=4
a[4]=5
Sum=15

Non-Linear Array

Array of having different dimensions or n subscript is in the form of Non-linear array. Non-linear array are further of n different types as:

  • Two Dimensional Array
  • Three Dimensional Array
  • ...
  • N-Dimensional Array

Two-Dimensional Array

These arrays are also called a double dimensional array. Another name of a two-dimensional array is Tabular or Rectangular Array. These array are in row and column form, so these are also called Row-Column array or square array.

The syntax to define or declare two dimensional array is as:

data-type arrary-name[row size][column size];

For example, some of the valid one dimensional array declaration statements are written as below:

int a[10][10];
float f[50][50];
char n[20][20];

Example Program:

/*Below program showing Two Dimensional array...*/
#include <stdio.h>
#include <conio.h>
void main()
{
  int a[2][2],r,c;
  clrscr();
  printf("\n\tTwo Dimensional Array\t\n");
  printf("\n\t======================\t\n");
  printf("\n\tEnter the Rows and Columns:\t\n");
  for(r=0;r

Output is as follows:

Related Post