8.3 – Array with loops

by subbu on October 22, 2013

Array is a derived type used to store multiple values of similar type in adjacent memory allocations as a single variable. It is used in places where multiple values of same type need be processed.

Initializing while declaration: (Assigning while declaration)

We can assign a set of values of similar type to an array while its declaration. Assigning while declaration is called initializing. Elements of same type separated with comma (,) and placed in a pair of parenthesis {} can be directly assigned using assigning operator (=) to the array declaration statement. Values are assigned to the array in the same order as in the order of set.

int a[6]={10,20,40,45,30,12};

It is optional to specify the size of array while its initialization. Hence, the same statement can also be written as

int a[]={10,20,40,45,30,12};   /* size is optional */

Initialization of an array

Here elements are assigned to the array in the same order as in the order of set. Make sure that, set of elements can be only assigned while declaration but, can’t be assigned to an array which is already declared.

int a[6];
a={10,20,40,45,30,12};   /* It results error */

Accessing elements of an array using loop:

int a[6]={10,20,40,45,30,12};

Initialization of an array

To print the elements of array, we can write the code as

printf("%d\t",a[0]);
printf("%d\t",a[1]);
    ------------
    ------------
printf("%d\t",a[4]);
printf("%d\t",a[5]);

Here the same code is being repeated but, only the index is being changed from 0 to 5. Rather writing these many statements repeatedly, we can write a single printf() statement and index from 0 to 5 can be changed using a loop

for(i=0;i<6;i++)
    printf("%d\t",a[i]);

When “i” is 0 then a[0] that is 10 is accessed, when “i” is 1 then a[1] that is 20 is accessed and when “i” is 2 then a[2] that is 40 is accessed.
The index variable “i” could access elements of array one by one as the loop progresses.

The common loop to access elements of array from first to last is

loop to access elements

#include<stdio.h>
int main()
{
int a[]={10,20,40,45,30,12};
int i;
printf("Elements of array:\n");
for(i=0;i<6;i++)
    printf("%d\t",a[i]);
return 0;
}

Output:
Elements of array:
10      20      40      45      30      12

Storing the data into an array:

int a[5];

empty array

If we want to access the elements from the keyboard and store into the array elements then we must write the code like

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

Here the same code is being repeated but, only the index is being changed from 0 to 4. Rather writing these many statements repeatedly, we can write a single scanf() statement and index from 0 to 4 can be changed using a loop

for(i=0;i<5;i++)
   scanf("%d",&a[i]);

When “i” is 0 then an integer is accepted from the keyboard and stored into a[0], when i is 1 then another number is accepted from the keyboard and stored into a[1] and when i is 2 then one more number is accepted from the keyboard and stored into a[2].
Numbers are accessed from the keyboard and stored into array as loop progresses.

The common loop to access elements from the keyboard and store into array is

loop to store elements into an array

Specification: Accept 5 integer values as input to the program and print the elements in accepted order.

We declare an array of int type of 5 elements

int a[5];

We read 5 integers from the keyboard and store into the array using a loop

printf("Enter 5 integers:\n");
for(i=0;i<5;i++)
    scanf("%d",&a[i]);

Storing elements into an array
We access 5 elements from the array and print using a loop

printf("The elements are:\n");
for(i=0;i<5;i++)
   printf("%d\t",a[i]);

printing elements
Putting altogether

#include<stdio.h>
int main()
{
int a[5];
int i;
printf("Enter 5 integers:\n");
for(i=0;i<5;i++)
   scanf("%d",&a[i]);
printf("The elements are:\n");
for(i=0;i<5;i++)
   printf("%d\t",a[i]);
return 0;
}

Execution:
Enter 5 integers:
20      25      30      32      76

The elements are:
20      25      30      32      76

Previous post:

Next post: