1
COMP 1402
Winter 2008 Tutorial #5 Arrays
Overview of Tutorial #5
- What is an array?
- Array syntax
- Character arrays
- Passing arrays to functions.
- Processing/traversing arrays
- Higher Dimension arrays (2d and 3d)
COMP 1402 Winter 2008 Tutorial #5 Arrays Overview of Tutorial #5 - - PDF document
COMP 1402 Winter 2008 Tutorial #5 Arrays Overview of Tutorial #5 What is an array? Array syntax Character arrays Passing arrays to functions. Processing/traversing arrays Higher Dimension arrays (2d and 3d) 1 Why
87.0 student1_grade 75.0 student2_grade 91.0 student3_grade 3 Variables A Single Array with 3 Elements grade_array 87.0 75.0 91.0
int a; An array reserves a contiguous block of memory addresses in which we can store values of the array type (int in this case). int a[5]; A regular variable reserves a single address in memory where its value is stored.
Regular Variable An Array
int a[5]; a[0] = 7; a[1] = 13; a[2] = 11; a[3] = 5; a[4] = -8;
13 7 11 5
an_array 1 2 3 4
a[0] a[1] a[2] a[3] a[4]
int a = 15; //Creates a variable and sets its value. //Create and initialize an array in one step. int a[] = { 8, 7, 15, 9} //Note compiler can determine //array size. //Create, and then initialize the elements. int a[4]; //In this case you must provide # elements. a[0] = 8; a[1] = 7; a[2] = 15; a[3] = 9;
char my_string[] = { 'h', 'e', 'l', 'l', 'o', '\0'}; //count the length of my_string int pos = 0; while( my_string[pos] != '\0' ) { pos = pos + 1; } printf( "String: %s : is %d chars long\n", my_string, pos);
#define N 10 int numbers[N]; void func1( int array[N]); //OK void func2( int array[] ); //Also OK void func3( int *array); //Also OK //When you invoke the function you don't use [ ] func1(numbers ); //correct func1(numbers[] ); //wrong - syntax error.
//Count how many elements of array are greater than 100 int array[N]; array[0] = 237; //etc. int i; int count = 0; for( i = 0; i < N; i++) { if( array[i] > 100 ) count = count + 1; }
#define N 5 int array[N]; array[0] = 237; //etc. int i; int c= 0; for( i = 0; i < N; i++) { if( (array[i] > 100 ) c = c + 1; } #define N 5 int array[N]; array[0] = 237; //etc. int i; int count= 0; if( array[0] > 100) count = count +1; if( array[1] > 100) count = count +1; if( array[2] > 100) count = count +1; if( array[3] > 100) count = count +1; if( array[4] > 100) count = count +1; Code is equivalent.
#define END_OF_ARRAY -1.0 //Note that the terminator need not be in the final position. char astring[ ] = { 'h', 'e', 'l', 'l', 'o', '\0', 't', 'h', 'e', 'r', 'e', '\0' }; int i = 0; while( astring[i] != '\0' ) { // prints "hello" putchar( astring[i] ); i++; } double weights[ ] = { 123.0, 203.1, 176.0, 141.0, END_OF_ARRAY } i = 0; while( weights[i] != END_OF_ARRAY ) { //do something. //Note you must increment i or the loop runs forever! }
// A 1-d array int a[10]; //A 2-d array - perhaps used to store a chess/checker board. int b[8][8]; //A 3-d array int c[3][4][5]; //Higher - you could do this if you wanted to ... why? int e[2][3][4][5][6];
// create an array with two // rows and three columns int matrix[2][3];
// To set row 0 column 1 to 7 matrix[0][1] = 7; matrix[0][0] matrix[0][1] matrix[0][2] matrix[1][0] matrix[1][1] matrix[1][2]
func1( int array[2][3] ); //OK func2( int array[ ][3] ); // Also OK func3( int array[ ][ ] ); //Not allowed. int a[2][3]; int b[3][3]; //Passing to functions is the same as for 1d arrays func1( a ); //OK same size. func1( b); //Will compile since b has 3 columns, but will likely //cause problems at run time. func2( a ); //OK same size. func2( b ); //OK because b has 3 columns.
#define ROWS 2 #define COLUMNS 2 int array2d[ROWS][COLUMNS]; int rw, cl; for( rw = 0; rw < ROWS; rw++ ) { for( cl = 0; cl < COLUMNS; cl++ ) { array2d[rw][cl] = 0; } }
// array with 3 levels, rows // and columns int cube[3][3][3];
// Set centre cell to 5 cube[1][1][1] = 5; [0][0][0] [0][0][1] [0][1][1] [0][1][2] [0][2][2] [0][1][0] [0][0][2] [0][2][0] [0][2][1]
[1][0][0] [2][0][0] [1][0][1] [2][0][1] [2][0][2] [1][0][2] [1][1][2] [2][2][2] [2][1][2] [1][2][2]
Array cube
func1( int array[2][3][4] ); //OK func2( int array[ ][3][5] ); // Also OK func3( int array[ ][ ][4] ); //Not allowed. int a[2][3][4]; int b[3][3][5]; //Passing to functions is the same as for 1d arrays func1( a ); //OK same size. func2( b); //OK columns and rows are same. func2( a ); //Syntax error a has 3 rows, 4 columns.
#define LEVELS 2 #define ROWS 2 #define COLUMNS 2 int array3d[LEVELS][ROWS][COLUMNS]; int lv, rw, cl; for( lv = 0; lv < LEVELS; lv++) { for( rw = 0; rw < ROWS; rw++ ) { for( cl = 0; cl < COLUMNS; cl++ ) { array2d[lv][rw][cl] = 0; } } }