Professional Documents
Culture Documents
2
Objectives
To use the array data structure to represent lists and tables of values.
To define and initialize an array.
To refer to individual elements of an array.
To define symbolic constants.
To manipulate the values of array’s elements.
To use one-dimensional and multidimensional-arrays.
3
Introduction
An array is a group of memory locations related by the fact that
they all have the same name and the same type.
The position number contained within square brackets is more formally called
a subscript (or index).
4
Defining Arrays
Specify the following things for defining array.
The data type of the values stored in the array
The name of the array
The dimensionality of the array: One dimensional array or two-dimensional array.
The size of each dimension.
Examples:
int c[ 5 ]; /* an integer array named c with size 5 */
float a[ 10 ]; /* a float array named a with size 10 */
int marks[ 10 ][ 7 ]; /*a two-dimensional array to store 7 subject marks for 10 students
*/
5
One-Dimensional Arrays
Use one-dimensional arrays to store and access list of data values.
c[ 5 ]
c[ 0 ] 7
c[ 1 ] 9
c[ 2 ] 5
c[ 3 ] 6
c[ 4 ] 2
Examples:
int c[ 5 ]; /* an integer array named c with size 5 */
There are five elements in array c.
All values are named c.
The value of first element is c[ 0 ] = 7.
The second value is c[ 1 ] = 9
And, the value of fifth element, c[ 4 ] is 2.
6
Initializing Arrays
Using for loop to initialize the elements of a 5-element integer array c to zero.
7
Cont;
Using initializer list to initialize the elements of a 5-element integer
array c to zero.
Note:
Arrays are not automatically initialized to zero.
Must at least initialize the first element to zero for the remaining elements to be
automatically zeroed.
8
Cont;
Example-1: Write a program that uses for loop to initialize the elements of a 10-element integer
array n to zeros and print the array in tabular format.
#include <stdio.h>
for( i=0; i<10; i++ )
int main(void)
{
{
printf( "%7d%13d\n", i, n[i] );
/* use initializer list to initialize array n */
} /* End for loop */
int n[10] = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
int i; /* counter */
return 0;
} /* End main */
/* Output contents of array n in tabular format*/
printf( "%s%13s\n", "Element", "Value" );
10
Array Manipulations
Example-3: Write a program that specify an array’s size with a symbolic constant
and initializes the10-element array s to the values 2, 4, 6, …, 20 with calculation
and print the array in tabular format.
for( roll = 0; roll <= 6000; roll++ ) /* roll die 6000 times*/
{
face = 1 + rand( ) % 6;
++frequency[ face ];
}
13
Passion Arrays to Functions
Pass by Reference
C automatically passes (entire) arrays to functions by reference.
The called functions can modify the element values .
The name of array evaluates the address of the first element.
Pass by Value
C passes individual array elements to functions by value.
The called functions can’t modify the element values.
14
Cont;
Pass by Reference
#include<stdio.h> for( i = 0; i< SIZE; i++)
#define SIZE 5
void array( int b[ ], int size ); { printf("%3d", a[ i ] ); }
int main(void) return 0;
{ }
int a[SIZE] = { 0, 1, 2, 3, 4};
void array( int b[ ], int size )
int i;
{
printf("The values of the original array are:\n"); int j;
for( i = 0; i< SIZE; i++)
for( j=0; j <size; j++)
{ printf("%3d", a[ i ] ); }
{ b[ j ] *= 2; }
printf("\n"); }
array( a, SIZE );
15
Cont;
Pass by Value
#include<stdio.h> void array( int e )
#define SIZE 5
void array( int e ); {
int main(void) printf("Modified element value is: %d\n", e *= 2);
{ }
int a[SIZE] = { 0, 1, 2, 3, 4};
int i;
array( a[ 3 ]);
return 0;
}
16
Sorting Arrays
One of the most important computing applications.
Placing the data into a particular order such as ascending or descending.
Bubble Sort or Sinking Sort
For Ascending Order
The smaller values upward to the top of the array like air bubbles rising in water.
The larger values sink to the bottom of the array.
Temporary location is used to swap array elements. (e.g. hold, temp, …)
2 hold
2
1
1 a[ 0 ] > a[ 1 ]
2 2
3 a[ 0 ] a[ 1 ]
1
3
17
Cont;
Sorting the 10-element integer array into ascending order with bubble sort.
#include<stdio.h>
#define SIZE 10 if( a[ i ] > a[ i+1 ] )
int main( void ) {
{ hold = a[ i ];
int a[ SIZE ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
a[ i ] = a[ i+1 ];
int pass; /* passes counter */
a[ i+1 ] = hold;
int i; /* comparisons counter */
} /* end if */
int hold; /* temporary location */
} /* end inner for */
printf("\nData items in original order\n"); } /* end outer for */
for( i = 0; i < SIZE; i++ )
{ printf("%4d", a[ i ]); } printf("\nData items in ascending order\n");
for( i = 0; i < SIZE; i++ )
for( pass = 1; pass < SIZE; pass++) { printf("%4d", a[ i ]); }
{ return 0;
for( i = 0; i < SIZE-1; i++) } /* end main */
{
18
Cont;
Sorting the 10-element integer array into ascending order with bubble sort.
19
Searching Arrays
Determine whether an array contains a value that matches a certain kay value.
Finding a particular element of an array.
20
Cont;
Searching an array with Linear Search.
0 2 4 6 8 10 12 14 16 18
int linearSearch(int a[ ], int key, int size)
{ a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[9]
int n;
for( n = 0; n < size; n++ )
{ key=6
if( a[ n ] == key ) n=0
{ a[0] 0 0 != 6 n++
return n; n=1
} a[1] 2 2 != 6 n++
}
n=2
return -1; 4 != 6 n++
a[2] 4
}
n=3
a[3] 6 6 == 6 return 3
21
Cont;
Searching an array with Linear Search.
#include<stdio.h> if( element != -1) for( n = 0; n < size; n++ )
#define SIZE 100 { {
int linearSearch(int array[ ], int key, int size); printf("Found value in element %d\n",
int main( void ) if( array[ n ] == key )
element);
{ {
}
int a[ SIZE ]; return n;
else
int x;
{ }
int searchKey;
int element; printf("Value not found\n"); }
for( x = 0; x < SIZE; x++ ) } return -1;
{ return 0;
}
a[ x ] = 2 * x; }
}
printf("\nEnter search key: "); int linearSearch(int array[ ], int key, int
scanf("%d", &searchKey); size)
{
element = linearSearch( a, searchKey, SIZE);
int n;
22
Cont;
Searching an array with Binary Search.
#include<stdio.h> result = binarySearch( a, key, 0, SIZE-1 );
#define SIZE 15
int binarySearch( int b[ ], int searchKey, int low, int high); if( result != -1 )
int main( void ) {
{
printf("\n%d found in array element %d\n", key, result);
int a[ SIZE ];
}
int i;
else
int key;
int result; {
printf("\n%d not found\n", key);
for( i = 0; i < SIZE; i++) }
{
a[ i ] = 2 * i; return 0;
} }
24
Cont;
int binarySearch( int b[ ], int searchKey, int low, int high) key=4
{
int middle; 0 2 4 6 8 10 12 14 16 18
while( low <= high ) b[0] b[1] b[2] b[3] b[4] b[5] b[6] b[7] b[8] b[9]
{
middle = ( low + high ) / 2; low=0
high=9
if( searchKey == b[ middle ] )
{ mid=(0+9)/2=4 b[4]=8 4 != 8
return middle;
} 4<8
else if( searchKey < b[ middle ] ) high=4-1=3
{
high = middle - 1; mid=(0+3)/2=1 b[1]=2 4 != 2
}
4<2
else { 4>2
low = middle +1; low=1+1=2
}
} mid=(2+3)/2=2 4 == 4 return 2
b[2]=4
return -1;
} 25
Multiple-Subscripted (Multidimensional) Arrays
To represent the tables of values consisting of information arranged in rows and columns.
To identify a particular table element, two subscripts are specified.
The first identifies the element’s row and the second identifies the element’s column.
Tables or arrays that required two subscripts to identify a particular element are called two
double-subscripted arrays.
The m rows and n column array is called m-by-n array.
For example: int c[ m ][ n ];
int c[ 3 ][ 4 ];
Table or array has 3 rows and 4 columns.
26
Cont;
Initializing Multidimensional Arrays
#include<stdio.h> void printArray( int a[ ][ 3 ] )
void printArray( int a[ ][ 3 ] ); {
int main( void ) int i;
{ int j;
int array1[ 2 ][ 3 ] = { { 1, 2, 3 }, { 4, 5, 6} };
for( i = 0; i <= 1; i++ )
int array2[ 2 ][ 3 ] = { 1, 2, 3, 4, 5, 6 };
{
int array3[ 2 ][ 3 ] = { { 1, 2 }, { 4 } };
for( j = 0; j <= 2; j++ )
printf("\nValue in array1 by row are:\n");
printArray( array1 ); {
printf("\t");
printf("\nValue in array2 by row are:\n"); printf( "%d", a[ i ][ j ] );
printArray( array2 ); }
printf("\n");
printf("\nValue in array3 by row are:\n"); }
printArray( array3 ); }
return 0;
}
27
Cont; Two-dimensional Array Manipulations (Average Grade)
#include<stdio.h>
#define STUDENTS 3
#define EXAMS 4
double average( const int setOfGrade[ ], int tests);
void printArray( const int grades[ ][ EXAMS ], int pupils, int tests);
int main( void )
{
int student;
const int studentGrades[ STUDENTS ][ EXAMS ] = { { 77, 68, 86, 73 }, { 96, 87, 89, 78}, { 70, 90,86, 81} };
void printArray( const int grades[ ][ EXAMS ], int pupils, int tests)
{
int i, j;
printf("%18c[0] [1] [2] [3]", ' '); /* output column head*/
for( i =0; i < pupils; i++ )
{
printf("\nstudentGrades[%d] ", i);
29
References:
Text Book, EcE_22014, Technical Programming II
THANK YOU
30