Welcome Guest   Login   Mar 19, 2024
Tutorials
C
HTML
Java
<?php include("../../config.inc.php"); $section="Tutorials - Programming in C - Chapt 5"; include("../../title.inc.php"); ?>
Chapter-5
Arrays

Single Dimension Array

An array is a group of elements that share a common name, and that are differntiated from one another by their positions within the array. For example, if we have five numbers, all which are named x, they may be listed.

x

58

64

23

09

87
 

The position of each of these elements can be indicated by means of subscript :

x1 = 58

x2 = 64

x3 = 23

x4 = 09

x5 = 87

subscript indicates the position of particular element with respect to the rest of the elements.
In C array is declared like :

int x[5];


Assignment statements can be written like

x[0]=58

x[1]=64

x[2]=23

x[3]=09

x[4]=87
 

There is no element x[5]. The reason for this unusual numbering scheme is that C was designed to model the operation of the computer at a level lower than dealt with by most programming languages. The calculation of the address corresponding to a subscript is simpler when the first array element is numbered 0.
 
main()
{
int x[5];
int ctr,sum;

for(ctr=0;ctr<5;ctr++)
{
printf("\n Enter the value : ");
scanf("%d",&x[ctr]);
}

for(ctr=0;ctr<5;ctr++)
{
printf("\n %d ",x[ctr]);
sum = sum + x[ctr];
}
printf("Sum of all elements is %d",sum);

}


 

array can be initialised at the time of declaration also.

Int arr[5] = { 3 , 4 , 5 , 7, 4 };
 
 

Character strings are the example of arrays itself.
 
main()
{
char str[30]="Syspro International";
int x;

for(x=0;str[x]!=’\0’;x++)
{
printf("\n%c",str[x]);
}
}


 

Double dimension arrays.

Double dimension arrays are having counter which not only increments by row but by column also, logically they can be represented in the following format.

Int arr[3][4];
 
12 34 4 54
23 45 3 45
32 56 76 56

The syntax for assignment of double dimension array is in following format.

arr[0][0]=12;

arr[0][1]=34;

the same can be done at the time of declaration also.
 
main()
{
int arr[3][4]={ {12,34,4,54},
{23,45,3,45},
{32,56,76,56} 
};

int r,c;

for(r=0;r<3;r++)

for(c=0;c<4;c++)
{
printf("%d\t",arr[r][c]);
}
printf("\n");

}
}

There can be even Multi-Dimension Arrays.
 

My New Blog

My Award winning Whitepaper

Contradicting Light
My Personal Blog


Trace Mobile Location



My Book Reviews
 




Tech Jokes Worth Reading
 
Top
Home
www.deepjava.com 1999-2017 (C) Developed and maintained by D-Kay Consultancy