8.1 – Arrays Introduction

by subbu on October 22, 2013

Any variable belongs to any type can store only a single value, assigning another value results, overwriting the new value on the old value.

#include<stdio.h>
int main()
{
int x=10;
x=45;
printf("%d",x);
return 0;
}

Output:
45

In the above example x is initialized with 10, in the next statement x is assigned with 45. Here the new value 45 is overwritten on 10.
The moral of this example is, only a single value can be stored in a variable at a time.

Need of an array:

Some times, we need to handle multiple values through the program. Say for example, need to accept sales of a product for 12 months and find months of maximum and minimum sales.

One way to accept sales of 12 months is declaration of 12 variables, accepting the data into individual variables and finding maximum and minimum using if-else selection statement. It would be a 100 line program.

Another way of handling this problem is declaration of a single variable which can store 12 values of similar types. Is it not simple to use a single variable rather using 12 variables to store 12 values?

Array is a variable that can store multiple values of similar type.

How an array is declared?

We specify the name of variable, size in subscript ([]) that is the number of values needs to be stored in and the type of values need to be stored.
Say for example, if we want to store 5 int type of values in a variable called a then the statement must be written as

int a[5];

Simple array in C language
If we want to store 7 floating point values in a variable called x then the statement must be written as

float x[7];

simple float array

How elements are accessed?

When an array is declared, multiple memory locations of specified number and type are selected in adjacent locations. Each memory location can hold a single value called element.

Every element in an array is identified with an index. The index is written with in the subscript []. In C language index of the first element is always 0 and last element is always size-1.

Say for example, if we have an array of size 5 with the name x then the index of first element is 0 and last element is 4. The first element is accessed as x[0] and the last element is accessed as x[4].

int x[5];

Elements of an arrayHere subscript operator is used in two cases, one is to specify the size while declaration and another is to specify the index while accessing element of array.

int x[5];     /* allocates 5 int type memory allocations */
x[0]=10;      /* assigns to 1st element */
x[1]=25;      /* assigns to 2nd element */
x[4]=45;      /* assigns to last element */

Array elements as normal variables:

Elements of array behaves like normal variables, participate in all operations like arithmetic, relational and logical operations.

Operations on array elements

#include<stdio.h>
int main()
{
int x[5];                        /* allocates 5 elements */
x[0]=10;                         /* assigns 10 to x[0]  */
x[1]=5;                          /* assigns 5 to x[1] */
x[2]=x[0]+x[1];                  /* assigns 10+5 - 15 to x[2] */
x[3]=x[0]>x[1];                  /* assigns 10>5 - 1 to x[3] */
x[4]=!x[3];                      /* assigns  !1 - 0 to x[4] */
printf("x[0]\t%d",x[0]);
printf("\nx[1]\t%d",x[1]);
printf("\nx[2]\t%d",x[2]);
printf("\nx[3]\t%d",x[3]);
printf("\nx[4]\t%d",x[4]);
return 0;
}

Output:
x[0]    10
x[1]    5
x[2]    15
x[3]    1
x[4]    0

Specification: Accept two numbers and print the sum, subtraction and product.

Here we can declare 5 int type variables to accept two numbers and calculate sum, subtraction and product like

int x,y,sum,sub,pro;

Rather declaring individual variables, we can declare an int type array of size 5 to handle this problem.

int a[5];

Here the array has 5 elements, a[0] and a[1] are used to store two accepted numbers and a[2], a[3] and a[4] are used to store sum, subtraction and product.

We can accept the data from the keyboard and store into array elements using scanf() statement like

scanf("%d%d",&a[0],&a[1]);

Program:

#include<stdio.h>
int main()
{
int a[5];
printf("Enter two numbers:\n");
scanf("%d%d",&a[0],&a[1]);     /* Accepting data from the keyboard */
a[2]=a[0]+a[1];
a[3]=a[0]-a[1];
a[4]=a[0]*a[1];
printf("Sum %d\nSubtraction %d\nProduct %d",a[2],a[3],a[4]);
return 0;
}

Execution:
Enter two numbers:
23      78
Sum 101
Subtraction -55
Product 1794

Example explained:

Arithmetic operations on elements

  • Address of (&) symbol is used to accept the data from the keyboard and store into the specified elements.
  • scanf(“%d%d”,&a[0],&a[1]); accepts two numbers from the keyboard and stores into a[0] and a[1].
  • Addition, subtraction and product of these elements are stored into a[2], a[3] and a[4]
  • Finally the values of  a[2], a[3] and a[4] are printed as output.

Previous post:

Next post: