0% found this document useful (0 votes)
24 views25 pages

4.1 Array

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views25 pages

4.1 Array

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

UNIT IV

Arrays
1. Rules for arrays
2. Multiple subscripts in arrays
3. Multi-dimensional arrays
4. For loop with arrays
5. Simple programs
ARRAY
 An array is a group of related data items that share a common
name.
 An array is defined as the collection of similar type of data
items stored at contiguous memory locations.
 Arrays are the derived data type in C programming language
which can store the primitive type of data such as int, char,
double, float, etc.
 It also has the capability to store the collection of derived data
types, such as pointers, structure, etc.
 The array is the simplest data structure where each data
element can be randomly accessed by using its index number.
PROPERTIES OF ARRAY
 The array contains the following properties.
 Each element of an array is of same data type and carries
the same size, i.e., int = 4 bytes.
 Elements of the array are stored at contiguous memory
locations where the first element is stored at the smallest
memory location.
 Elements of the array can be randomly accessed since we
can calculate the address of each element of the array
with the given base address and the size of the data
element.
Advantage of C Array
1) Code Optimization: Less code to the access the data.
2) Ease of traversing: By using the for loop, we can retrieve the
elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we need a
few lines of code only.
4) Random Access: We can access any element randomly using
the array.

Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of
declaration of the array, we can't exceed the limit.
Declaration of C Array
 We can declare an array in the c language in the following
way.
Syntax:
data_type array_name[array_size];
 Example for declare the array.

int marks[5];
 Here, int is the data_type, marks are the array_name, and 5 is
the array_size.
Initialization of C Array
 The simplest way to initialize an array is by using the index of
each element. We can initialize each element of the array by
using the index. Consider the following example.
 marks[0]=80;//initialization of array
 marks[1]=60;
 marks[2]=70;
 marks[3]=85;
 marks[4]=75;
RULES FOR ARRAY DECLARATION

 The data type can be any valid C data types including structure
and union.
 The array name has to follow the rule of variable and the size
of array has to be a positive constant integer.
 We can access array elements via indexes array_name[index].
 Indexes of array starts from 0 not 1 so the highest elements of
an array is array_name[size-1].
C array example
#include<stdio.h>
void main()
Output
{
80
int i=0;
60
int marks[5];//declaration of array
70
marks[0]=80;//initialization of array
85
marks[1]=60; 75
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++)
{
printf("%d \n",marks[i]);
}//end of for loop
getch();
}
C Array: Declaration with Initialization
 We can initialize the c array at the time of
declaration. Let's see the code.
 int marks[5]={20,30,40,50,60};
 In such case, there is no requirement to define the
size. So it may also be written as the following code.
 int marks[]={20,30,40,50,60};
Example
#include<stdio.h>
void main(){
int i=0;
int marks[5]={20,30,40,50,60};
//declaration and initialization of array
//traversal of array Output
for(i=0;i<5;i++) 20
{ 30
printf("%d \n",marks[i]); 40
} 50
60
getch();
}
MULTIPLE SUBSCRIPTS IN ARRAYS
Arrays in C can have multiple subscripts.
• A common use of multiple-subscripted arrays (also called
multidimensional arrays) is to represent tables of values consisting of
information arranged in rows and columns.
• To identify a particular table element, we must specify two subscripts:
The first (by convention) identifies the element’s row and the second
(by convention) identifies the element’s column.
• Tables or arrays that require two subscripts to identify a particular
element are called double- subscripted arrays.
Multiple-subscripted arrays can have more than two subscripts.
• The array contains three rows and four columns, so it’s said to be a 3-
by-4 array.
 • In general, an array with m rows and n columns is called an
m-by-n array
 Arrays with 2 subscripts are often used to represent tables of
values.
 To identify a particular element in the table, we specify:
 its row (by convention, the 1st subscript), and
 its column (by convention, the 2nd subscript). // Draw a
picture
 Arrays in Java can have more than 2 subscripts.
 In Java, multiple-subscripted arrays are implemented as arrays
of arrays:
 A 2-dimensional array of int is implemented as an array of
an array of int.
 There really are only single-subscripted arrays, but an
array's elements can be anything, including arrays.
 This simplifies the language.
 A good illustration of this uniform treatment is the inner
loop of the printArray method: Clearly, element a[i] is itself
an array; it has a length attribute.
MULTI-DIMENSIONAL ARRAYS
 C programming language allows multidimensional arrays. Here is
the general form of a multidimensional array declaration −
type name[size1][size2]...[sizeN];
 For example, the following declaration creates a three dimensional
integer array −
int threedim[5][10][4];
 Similarly, you can declare a three-dimensional (3d) array. For
example,
float y[2][4][3];
 Here, the array y can hold 24 elements
Two-dimensional Arrays
 The simplest form of multidimensional array is the two-
dimensional array. A two-dimensional array is, in essence, a
list of one-dimensional arrays.
 To declare a two-dimensional integer array of size [x][y], you
would write something as follows −
type arrayName [ x ][ y ];
 Where type can be any valid C data type and arrayName will
be a valid C identifier.
 A two-dimensional array can be considered as a table which will
have x number of rows and y number of columns.
 A two-dimensional array a, which contains three rows and four
columns can be shown as follows −

 Thus, every element in the array a is identified by an element name


of the form a[ i ][ j ], where 'a' is the name of the array, and 'i' and 'j'
are the subscripts that uniquely identify each element in 'a'.
Initializing 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 */
};
 The nested braces, which indicate the intended row, are
optional. The following initialization is equivalent to the
previous example −
int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
Accessing Two-Dimensional Array Elements

 An element in a two-dimensional array is accessed by using


the subscripts, i.e., row index and column index of the array.
For example −
int val = a[2][3];
 The above statement will take the 4th element from the 3rd
row of the array. You can verify it in the above figure. Let us
check the following program where we have used a nested
loop to handle a two-dimensional array –
Example:
#include <stdio.h>
void 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] );
}
}
getch();
}
 Output:
 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
FOR LOOP WITH ARRAYS

 When you declare an array, the computer allocates a block


of memory for it, but the block contains garbage (random
values). Therefore, before using an array, you should
initialise it. It is usually a good idea to set all elements in the
array to zero.
 The easiest way to initialise an array is with a for loop. The
following example loops through every element in the
array my_array and sets each to zero.
 Remember, because arrays in C are zero-based, the indices
of the array my_array in the example below run 0 through 9,
rather than 1 through 10. The effect is the same, however:
an array of ARRAY_SIZE (that is, 10) elements.
Example:
#include <stdio.h>
#define ARRAY_SIZE 10

int main ()
{
int index, my_array[ARRAY_SIZE];

for (index = 0; index < ARRAY_SIZE; index++)


{
my_array[index] = 0;
printf ("my_array[%d] = %d\n", index, my_array[index]);
}
printf("\n");

return 0;
}
Output:
my_array[0] = 0
my_array[1] = 0
my_array[2] = 0
my_array[3] = 0
my_array[4] = 0
my_array[5] = 0
my_array[6] = 0
my_array[7] = 0
my_array[8] = 0
my_array[9] = 0
 You can use similar code to fill the array with different values.
The following code example is nearly identical to the one
above, but the line my_array[index] = index; fills each element
of the array with its own index:
Example:
#include <stdio.h>
#define ARRAY_SIZE 5
Output:
int main ()
my_array[0] = 0
{
int index, my_array[ARRAY_SIZE];
my_array[1] = 1
my_array[2] = 2
for (index = 0; index < ARRAY_SIZE; index++) my_array[3] = 3
{ my_array[4] = 4
my_array[index] = index;
printf ("my_array[%d] = %d\n", index, my_array[index]);
}
printf("\n");

return 0;
}

You might also like