Ads block

Banner 728x90px

One-Dimensional Array

This chapter deals with the initialization of the arrays, implementation of array in memory and also discussed how to pass an array to particular function.

Initializing One-Dimensional Array


A one-dimensional array is one in which only one subscript specification is needed to specify a particular element of the array.
One-dimensional array can be declared as follows:


data_type var_name [Expression];

After declaration, the elements of a local array have garbage value while the elements of global and static arrays are automatically initialized to zero. We can initialize arrays at the time of declaration. Here, marks is an integer type array, whose size is 10 and all the constant values are separated by commas and there is a semicolon after the ending braces.


For example,

int marks [10] = { 10,11,12,13,14,15,16,17,18,19 };

The values of the array elements after this initialization are:


marks [0] = 10
marks [1] = 11
marks [2] = 12
marks [3] = 13
marks [4] = 14
marks [5] = 15
marks [6] = 16
marks [7] = 17
marks [8] = 18
marks [9] = 19

Note that in C, the first element is always placed in the 0th place. It means that the array index start from 0 to n-1, where n is the maximum size of the array declared by the programmer.


In such a case, the array elements are not assigned explicitly, then the initial values will be automatically set to zero.

This example is based on it.

Example

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

The result of the array elements will be-


num [0] = 1
num [1] = 2
num [2] = 3
num [3] = 0
num [4] = 0

12300
num[0]num[1]num[2]num[3]num[4]


Any integer expression can be used as a subscript in the array by the help of loop.

We know an array is a set of infinite collection of elements with a single variable name. We would use loops to read or write arrays, because loop makes it simple and easy.


//Program to read and write the elements of array.
#include<stdio.h>
void main()
{
    int a[5];
    int i;
    printf("Enter the elements\n");
    for (i=0; i<=4; i++)   //loop:1 to read elements
      {
       scanf("%d", &a[i]);
      }
    printf("Contents of the array\n");
    for (i=0; i<=4; i++)   //loop:2 to write elements
      {
       printf("%d\t",a[i]);
      }
    printf("After some operation\n");
    for (i=0; i<=4; i++)   //loop:3
      {
       a[i] = a[i]+1;
       printf("%d\t",a[i]);
      }
}


Enter the elements
10  20  30  40  50
Contents of the array
10  20  30  40  50
After some operation
11  21  31  41  51


Implementation Of Array In Memory

The Address of particular element in a one-dimensional array is given by the relation:

Address of element a[k] = B + w * k

Where,

B is the base of the array

W is the size of each element of array

k is the index of element of the array

Example

Suppose, base address(B) of the first element of the array is 100, and each element of the array occupies four bytes in the memory, then address of fifth element of a one-dimensional array a[10] will be given as:

Address of element a[5] = 100 + 4 * 5
                                           = 100 + 20
                                           = 120.


Passing Array To Function


Arrays like other simple variables can be passed to function. To pass, its name is written inside the argument list in the call statement. Arrays are default passed to function by call by reference method because array name is itself a pointer to the first memory location of the array.
However, we can pass individual array elements through call by value method also.


//Program to pass an array to a function.
#include<stdio.h>
#include<conio.h>
int integer(int[], int);
void main()
 {
    int a[10], i, sum = 0;
    clrscr();
    printf("Enter the array");
    for (i=0; i<=10; i++)
     {
       scanf(“%d”, &a[i]);
     }
    sum = integer(a, 9);
    printf(“The sum of array elements is %d”, sum);
    getch();
}
    int integer(int p[], int n)
{
       int s = 0, i;
       for (i=0; i<=0; i++)
       {
         s = s + p[i];
       }
       return(s);
}


Enter the array
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
The sum of the array elements is 55