You are on page 1of 10

Kaliachak Government Polytechnic

Kalikapur Bandh, P.O: Bahadurpur,


P.S: Kaliachak , Malda- 732201

DEPARTMENT OF COMPUTER SCIENCE &


TECHNOLOGY
2nd Year-3rd semester
E-CONTENTS : C-Programming
language
UNIT : C arrays
Developed By :
Shahnawaz Shams, Lecturer in CST
Kaliachak Government Polytechnic
Kalikapur Bandh, P.O:Bahadurpur,
P.S:Kaliachak, Malda-732201
Contents :
1. Definition of Array And Types
2. Advantages of variables/ arrays
3. Declaration and initialization of one
dimensional, two dimensional and character
arrays
4. Accessing array element
1 Definition of Array And Types

• Array: An array is a collection of homogenious data


elements(i.e same data type) described by a single
name.Every array elements represented by an integer
value is called subscript or array index so it is also called
as subscripted variable.
• Type of Array: There are three types of array –
• (i)1-Dimentional Array
• (ii)2-Dimentional Array
• (iii)Multi-Dimentional Array
2.Advantages of variables/ arrays

• Arrays represent multiple data items of the same type


using a single name.
• In arrays, the elements can be accessed randomly by
using the index number.
• Arrays allocate memory in contiguous memory locations
for all its elements.
• Using arrays, other data structures like linked lists,
stacks, queues, trees, graphs etc can be implemented.
• Two-dimensional arrays are used to represent matrices.
3.1 Declaration and initialization of one
dimensional arrays

Syntax of declaration of 1-D array: data_type array_name[size];

Example:int a[10];
Compile time initialization:
process(1):int a[10]={10,21,3,20,65,42,51,58,35,11};
process(2):int a[]={10,21,3,20,65,42,51,58,35,11};

Memory Representation:
3.2 Declaration and initialization of two
dimensional arrays
• Multidimensional arrays may be initialized by specifying
bracketed values for each row.
• Following is an array with 3 rows and each row has 4
columns.
int a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1
*/
{8, 9, 10, 11} /* initializers for row indexed by 2
*/
};
OR
• int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
3.3 Declaration and initialization of
charecter arrays

• char name[13] = "StudyTonight"; // valid character array


initialization
• har name[10] = {'L','e','s','s','o','n','s','\0'}; // valid
initialization
3.4 Accessing array element
4. Accessing array element
#include <stdio.h>
int main ()
{
/* an array with 5 rows and 2 columns*/
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;
/* output each array element's value */
for ( i = 0; i < 5; i++ )
{
for ( j = 0; j < 2; j++ )
{
printf("a[%d][%d] = %d\n", i,j, a[i][j] );
}
}
return 0;
}

When the above code is compiled and executed, it produces the following result −

a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8
QUESTIONS OR
ASSIGNMENTS

You might also like