Professional Documents
Culture Documents
sional arrays,
on, accessing elements,
PRESENTED BY
M.YUGANDHAR
ation CSE DEPARTMENT
AITAM
Definition
Types of Array
Declaration of 1D Array
•To declare an array in C, a programmer specifies the type of the elements and the
number of elements required by an array .
Syntax
data type arrayname [Size ];
• The arraySize must be an integer constant greater than zero and type can be any data
type.
Example
int sohel [10];
1)static: You can initialize array either one by one or using a single statement as follows:
Syntax
datatype arrayname [Size ] = {List of elements};
Example
int a[4]={10,20,30,40};
1.Static Initializing 1D-Arrays
•The number of values between braces { } cannot be larger than the size that we declare
for the array between square brackets [ ].
Example:
a[0]={10}
a[1]={20}
a[2]={30}
a[3]={40}
1.Static Initializing 1D-Arrays
•If you omit the size of the array.
int a[ ] = {1,2,4,8,16};
•All arrays have 0 as the index of their first element which is also called base index.
•Following is the pictorial representation of the same array (contiguous memory locations)
2.Dynamic Initializing 1D-Arrays
Syntax
for(i=0;i<size;i++)
{
Scanf(“format specifier”, & arrayname [i] )
} Example
For(i=0;i<5;i++)
Example {
int sohel [5]; Scanf(“%i”, & sohel [i] )
}
How to initialize?
Way 1 Way2
Way 3 Way4
int a[5]; int a[5];
a[0]=2; a[1]= 4; a[2]=6; a[3]=8; a[4] = 5; for(int i=0; i<5; i++)
scanf(“%d”, &a[i]);
Accessing 1D-Array Elements
•An element is accessed by index of the array .
•This is done by placing the index of the element within square brackets after the name of
the array.
Example
int b = a[4];
•The above statement will take 5th element from the array and assign the value to b.
#include <stdio.h>
int main () Output:
{ Enter 5 values
int hemanth[ 5 ]; /* hemanth is an array of 5 integers */ 10 11 12 13 14
int i, j; Element[0] = 10
Element[1] = 11
printf (“\n Enter 5 values”); Element[2] = 12
for ( i = 0; i < 5; i++ ) Element[3] = 13
{ Element[4] = 14
scanf(" %i ", &hemanth[i] ); /* initialize elements */
}
for (j = 0; j < 5; j++ )
{
printf("Element[%d] = %d\n", j, hemanth[j] ); /* accessing each array element
*/
}
}
2D Array
•The simplest form of the multidimensional array is the two-dimensional array.
•A two-dimensional array is a list of one-dimensional arrays.
•A two-dimensional array can be like as a table which will have x number of rows and y
number of columns.
•To declare 2D-array in C, a programmer specifies the type of the elements and the
number of rows and columns required by an array .
Syntax
Data type array name [row size ][col size];
Declaring 2D Array
• A 2D- array , which contains three rows and four columns can be shown as below:
Example
int sohel[3][4];
• Now sohel is array variable which holds up-to 12 ( 3x4=12) integer numbers.
•Thus, every element in array is identified by an element name of the form sohel[ i ][ j ].
•where a is the name of the array, and i and j are uniquely identify each element .
Initializing 2D-Arrays
You can initialize array in C by using 2 methods:
1)static 2)dynamic
1)static: You can initialize array in C either one by one or using a single statement as
follows:
Way-I:
Syntax
Datatype arrayname [row Size ][col size] = {List of elements};
Example
int a[3][3]={10,20,30,40};
Initializing 2D-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.
Way 2: int a[3][4] = { {0, 1, 2, 3} , {4, 5, 6, 7} , {8, 9, 10, 11} };
2)Dynamic:
for(int i=0 ; i<row size ; i++)
for(int i=0; i<3; i++)
{
{
for(int j=0; j<col size; j++)
for(int j=0; j<4; j++)
{ {
scanf(“format Specifier” , &array name scanf(“%d”,&a[i][j]);
[i][j] ); }
} }
Accessing Two-Dimensional Array Elements
•An element in 2-dimensional array is accessed by using the subscripts, i.e., row index
and column index of the array.
•Let us check below program where we have used nested loop to handle a two
dimensional array:
Accessing Two-Dimensional Array Elements