Reading:  

Getting started with C Language


Arrays in C

 

ARRAYS

An array in C language is a group of related data types which have a common name or in other words  an array is a collection of common data types referenced under same name and stored in consecutive memory locations. The main use of arrays are that  we can use a group of data items as the ordinary variable. For instance if we want to store the marks of a subject in a class having 50 students we can use an array for storing all the students marks using a single variable name. As declaring 50 different variables are impracticable

How to declare an array

The declaration of array needs the type of data items to be stored in the array (which is data types ) it may be any data type from example int , char, float , etc.. The array name which is the reference name of that array. Like other variable names the rule for giving the array name is same as the ordinary variable, an array name can contain only letter, digit and underscore characters. Array names cannot begin with a digit character. and number of elements stored in the array. The size of array, the size should be a constant. This number is the maximum number which can hold in that array we may or may not use any number of elements less than or equal to this maximum number, but cannot store a single elements than the maximum number of elements.

One dimensional array

A list of elements using a single reference name and a single subscript ( size of the array ) is called one dimensional array.

 

Syntax :

Data-type  array-name[array-size];

Example :

int marks[5];

Where ‘marks’ is the array name of data type integer and we can store 5 elements in this array, which is the size of the array. When executing the declaration the computer will reserve the consecutive locations as per the array size. Here in this example it is 5 and the reservation will be 5 integer locations as shown below.

 

        

mark[0]

 

mark[1]

 

mark[2]

 

mark[3]

 

mark[4]

                  

Here the subscript 0-4 refers as index of the array. To refer to a particular location or element in the array, we refer the name of the array and the index of that element in the array ,which means  the location of the element in the array. The array index starts from zero. The maximum index value will be size of the array minus one. As here we refer 5 elements from 0 to 4, the lower limit will be always 0 and upper limit is size minus one.

Here the subscript 0-4 refers as index of the array. To refer to a particular location or element in the array, we refer the name of the array and the index of that element in the array ,which means  the location of the element in the array. The array index starts from zero. The maximum index value will be size of the array minus one. As here we refer 5 elements from 0 to 4, the lower limit will be always 0 and upper limit is size minus one.

 

Array elements can be assigned as follows

mark[0] =40

mark[1] =44

mark[2] =34

mark[3] =29

mark[4] =33

 

Then the elements are arranged in arrays as

40

mark[0]

44

mark[1]

34

mark[2]

29

mark[3]

 33

mark[4]

 

Initialization of one-dimensional array:

Initialization can be done either as static or dyanamically.Arrays can be initialized at declaration time that is  along the program source code it called as static initialization :

int mark[5]={40,44,34,29,33};          

When using this static initialization method it is not necessary to define the size of arrays during initialization.we can initialize the array without size as

int mark[]={40,44,34,29,33};

Here in this case, the compiler determines the size of array by calculating the number of elements of an array. When assign values as static the array elements  remains constant.

Dynamic initialization of arrays           

It means the array elements are assigned at the run time. Thus  whenever we runs the program the elements will be different. For this dynamic initialization we use a loop.

Program to access ‘n’ array elements to an array

#include<stdio.h>
void main()
{
int ar[10],i,n;
printf(“Enter the size of the array “);
scanf(“%d”,&n);
printf(“Enter the array elements“);
for(i=0;i<n;i++)
{
   scanf(“%d”,&ar[i]);
}
}

Thus using this program we can determine how much must be the size of the array and what are the array elements. When we run this program first compiler will ask the size of that array it must b any integer less than 10 because we allocate 10 spaces for the array ‘ar’. Then we have to enter ‘n’ array elements to the array.

Array ‘ar’

 

 

 

 

 

 

 

 

 

 

 

If we determine 3 is the size then the three elements will be entered in to first 3 consecutive locations and rest of the spaces will be vacant. If we enter three elements 1, 2, 3 into the array then it will be assigned as follows

When enter 1       

ar               

1

 

 

 

 

 

 

 

 

 

 

When enter 2       

ar

1

2

 

 

 

 

 

 

 

 

 

When enter 3

ar

1

2

3

 

 

 

 

 

 

 

 

Here from the location 0-2 the values are assigned. All the other remains free.

Program to access ‘n’ array elements to an array and display the array

#include<stdio.h>
void main()
{
int ar[10],i,n;
printf(“Enter the size of the array “);
scanf(“%d”,&n);
printf(“Enter the array elements“);
for(i=0;i<n;i++)
{
scanf(“%d”,&ar[i]);
}
printf(“The array elements are“);
for(i=0;i<n;i++)
{
printf(“%d ”,ar[i] “\n”);
}
}

Program to access ‘n’ array elements to an array and print its sum.

#include<stdio.h>
void main()
{
int ar[10],i,n,sum=0;
printf(“Enter the size of the array “);
scanf(“%d”,&n);
printf(“Enter the array elements“);
for(i=0;i<n;i++)
{
scanf(“%d”,&ar[i]);
sum= sum + a[i];
}
printf(“The array elements are“);
for(i=0;i<n;i++)
{
printf(“%d ”,ar[i] “\n”);
}
printf(“The sum is %d”,sum);
}

 

Operations on single dimensional arrays

The major operations on one dimensional arrays are

  • Searching
  • Sorting
  • Merging

Searching

Searching means to find whether the element is present in the given array. If the element is present then we can say the search is successful or if not found we can say the search is unsuccessful. There are many methods for searching they are linear search, binary search etc.

Sorting

Arranging the array elements in any order is called sorting. If it is in the increasing order it is called ascending or else it is called descending. Some sorting names are selection sort, bubble sort, etc.

Merging

Combining two or more arrays as a single array is called merging.

                  

 

 

 

 

 

 

 

 

 

 

Description

This tutorial introduces you to C language. Many tutorials has been written. We are writing this tutorial to be brief and to the point, so that you concentrate on what matter, rather than finding yourself in a mess. We want you to get a start and build form knowledge you will gain from this tutorial



Environment

We will cover environment needs as we progress through this tutorial. At the very least you will need a PC with windows or Linus operating system.

Audience

Programmers new to C Language

Learning Objectives

Learn basics of C language and build from that knowledge.

Author: Subject Coach
Added on: 3rd Jan 2015

You must be logged in as Student to ask a Question.

None just yet!