_ARRAYS_2
_ARRAYS_2
Arrays 1D and 2D
2
Introduction
• Arrays
– Structures of related data items
– Static entity – same size throughout the program
– Dynamic data structures
3
Name of array (Note that all
• Array
– Group of consecutive memory locations
– Same name and type c[0] -45
c[1] 6
• To refer to an element, specify c[2] 0
c[3] 72
– Array name c[4] 1543
– Position number c[5] -89
c[6] 0
• Format: c[7] 62
• c[ 0 ], c[ 1 ]...c[ n – 1 ]
Position number of the
element within array c
4
Arrays
• Array elements are like normal variables
c[ 0 ] = 3;
printf( "%d", c[ 0 ] );
– Perform operations in subscript. If x equals 3
c[ 5 - 2 ] == c[ 3 ] == c[ x ]
5
Declaring Arrays
• When declaring arrays, specify
– Name
– Type of array ( data type )
– Number of elements
arrayType arrayName[ numberOfElements ];
– Examples:
int c[ 10 ];
float myArray[ 3284 ];
• Declaring multiple arrays of same type
– Format similar to regular variables
– Example:
int b[ 100 ], x[ 27 ];
6
Exercise : Histogram
1 /* Fig. 6.8: fig06_08.c
7
2 Histogram printing program */
Outline
3 #include <stdio.h>
4 #define SIZE 10
5
1. Initialize array
6 int main()
7 {
2. Loop
8 int n[ SIZE ] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };
9 int i, j;
10 3. Print
11 printf( "%s%13s%17s\n", "Element", "Value", "Histogram" );
12
13 for ( i = 0; i <= SIZE - 1; i++ ) {
14 printf( "%7d%13d ", i, n[ i ]) ;
15
16 for ( j = 1; j <= n[ i ]; j++ ) /* print one bar */
17 printf( "%c", '*' );
18
19 printf( "\n" );
20 }
21
22 return 0;
23 }
8
Element Value Histogram
Outline
0 19 *******************
1 3 ***
2 15 ***************
Program Output
3 7 *******
4 11 ***********
5 9 *********
6 13 *************
7 5 *****
8 17 *****************
9 1 *
9
3 #include <stdio.h>
Outline
4 #define SIZE 5
9 int main()
10 {
2. Pass array to a
11 int a[ SIZE ] = { 0, 1, 2, 3, 4 }, i; function
12
23
29 modifyElement( a[ 3 ] );
31 return 0;
32 }
33 13
34 void modifyArray( int b[], int size ) Outline
35 {
36 int j; 3.1 Function
37
definitions
38 for ( j = 0; j <= size - 1; j++ )
39 b[ j ] *= 2;
40 }
41
42 void modifyElement( int e )
43 {
44 printf( "Value in modifyElement is %d\n", e *= 2 );
45 }
• Arrays in C (cont’d)
– Compare int *list1 and int list2[5] in C.
Same: list1 and list2 are pointers.
Difference: list2 reserves five locations.
– Notations:
list2 - a pointer to list2[0]
(list2 + i) - a pointer to list2[i] (&list2[i])
*(list2 + i) - list2[i]
16
• int arr[max_rows][max_columns];
• however, It produces the data structure which
looks like following.
19
• Access 2D arrray
– we can store the value stored in any particular cell of a 2D array to some
variable x by using the following syntax.
– int x = a[i][j];
• Initialize
– declare and initialize the 2D array is given as follows.
– int arr[2][2] = {0,1,2,3};
#include <stdio.h>
void main ()
{
printf("\n printing the elements ....\
int arr[3][3],i,j;
n");
for (i=0;i<3;i++)
for(i=0;i<3;i++)
{
{
for (j=0;j<3;j++)
printf("\n");
{
printf("Enter a[%d][%d]: ",i,j);
for (j=0;j<3;j++)
scanf("%d",&arr[i][j]);
{
} printf("%d\t",arr[i][j]);
} }
}
}
20
Mapping 2D array to 1D array
21
Row Major ordering
Advantages
31
Disadvantages
32
Applications of Arrays
• Apart from being widely used in programming,
arrays have additional applications as well: