Lecture06 Arrays
Lecture06 Arrays
Declaring,
Problem Solving Using Computers
Referencing
Constants
Returning
Arrays Stephen Scott
Searching & Adapted from Christopher M. Bourke
Sorting
Multi-
dimensional
Arrays
Common
Errors
Fall 2009
Exercises
1 / 30
Chapter 8
CSCE150A
Returning
8.5 Array Arguments
Arrays
8.6 Searching and Sorting an Array
Searching &
Sorting 8.7 Multidimensional Arrays
Multi-
dimensional 8.9 Common Programming Errors
Arrays
Common
Errors
Exercises
2 / 30
Introduction
CSCE150A
Introduction
Declaring,
Referencing
Simple data types use a single memory cell to store a variable
Initialization Collections of data should be logically grouped
Function Args
Example: 75 students in the class; should we declare 75 separate
Constants
variables to hold grades?
Returning
Arrays Grouping related data items together into a single composite data
Searching &
Sorting
structure is done using an array
Multi-
dimensional
Arrays
Common
Errors
Exercises
3 / 30
Declaring Arrays I
CSCE150A
Introduction
Declaring,
Referencing
Multi-
dimensional
Arrays
Common
Errors
Exercises
4 / 30
Declaring Arrays II
CSCE150A
Function Args
The first one instructs C to associate 8 memory cells of type double
Constants
with the name my_first_array
Returning
The second one instructs C to associate 10 memory cells of type int
Arrays with the name students
Searching &
Sorting In all cases, the memory cells will be adjacent to each other in
Multi- memory
dimensional
Arrays
Common
Errors
Exercises
5 / 30
Referencing Array Elements I
CSCE150A
Introduction
Declaring,
Referencing To process the data stored in an array, each individual element is
Initialization associated to a reference value
Function Args
By specifying the array name and identifying the element desired, we
Constants
can access a particular value
Returning
Arrays
The subscripted variable x[0] (read as x sub zero) references the
Searching &
Sorting first element
Multi-
dimensional
Arrays
Common
Errors
Exercises
6 / 30
Referencing Array Elements II
CSCE150A
Common
Errors
Exercises
7 / 30
Referencing Array Elements I
Pitfall
CSCE150A
Take care that you do not reference an index outside the array:
Introduction
Declaring,
1 double grades [75];
Referencing 2 ...
Initialization 3 printf ( " 75 th grade is % f \ n " , grades [74]);
Function Args
4 printf ( " 76 th grade is % f \ n " , grades [75]); ← Illegal
5 printf ( " -1 th grade is % f \ n " , grades [ -1]); ← Illegal
Constants
6
Returning
Arrays 7 int i ;
Searching &
8 for ( i =0; i <76; i ++)
Sorting 9 printf ( " %d - th grade is % f \ n " , ( i +1) , grades [ i ]);
Multi- 10 ↑ Illegal on last iteration
dimensional
Arrays
Common
Errors
Exercises
8 / 30
Array Initialization
CSCE150A
Introduction
You can declare multiple arrays along with regular variables:
Declaring, double cactus[5], needle, pins[7];
Referencing
Initialization
We can initialize a simple variable when we declare it:
Function Args
int sum = 0;
Constants Same with arrays:
Returning
Arrays 1 int array [ SIZE ];
Searching & 2 for ( i =0; i < SIZE ; i ++)
Sorting
Multi-
3 array [ i ] = 0;
dimensional
Arrays
Common
Errors
Exercises
9 / 30
Array Declaration & Initialization
CSCE150A
Introduction
Declaring,
We can declare and initialize an array
Referencing
If we initialize when we declare, we can omit the size
Initialization
Function Args
1 int p r i m e N u m b e r s L e s s T h a n H u n d r e d [] = {
Constants
Returning
2 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 ,
Arrays 3 41 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 ,
Searching &
Sorting
4 89 , 97 };
Multi-
dimensional
Arrays
Common
Errors
Exercises
10 / 30
Using for Loops for Sequential Access
CSCE150A
Exercises
11 / 30
Using Array Elements as Function Arguments
CSCE150A
Introduction
Declaring,
Referencing
You can use scanf with array elements just like with regular variables
Initialization 1 int x [10];
Function Args 2 int i = 0;
Constants
3 scanf ( " % d " , & x [ i ]);
Returning
Arrays 4 printf ( " Hey , I read % d \ n " , x [ i ]);
Searching &
Sorting
Multi-
dimensional
Arrays
Common
Errors
Exercises
12 / 30
Arrays as Arguments
CSCE150A
Declaring,
Passing arrays as arguments to a function means:
Referencing The function can access any value in the array
Initialization The function can change any value in the array
Function Args
Syntax: specify an array as a parameter by using the square brackets:
Constants
int sum(int array[], int size);
Returning
Arrays
Note: what is actually being passed is a pointer to the first element
Searching &
Sorting of the array!
Multi- We could equivalently define:
dimensional
Arrays int sum(int *array, int size);
Common
Errors
Exercises
13 / 30
Full Example
CSCE150A
Common
Errors
Exercises
14 / 30
Formal Array Parameter
CSCE150A
Common
Errors
Exercises
15 / 30
Arrays as Input Arguments
CSCE150A
Returning
declaration: int sum(const int foo[], int size) ...
Arrays
Specifies to the compiler that the array is to be used only as an input
Searching &
Sorting The function does not intend to modify the array
Multi-
dimensional The compiler enforces this: any attempt to change an array element
Arrays
in the function as an error
Common
Errors
Exercises
16 / 30
Returning an Array Result
CSCE150A
Exercises
17 / 30
Searching and Sorting an Array
CSCE150A
Introduction
Declaring,
Referencing
Initialization
Two common problems with array processing:
Function Args
1 Searching - Finding the index of a particular element in an array
Constants
Returning
2 Sorting - rearranging array elements in a particular order
Arrays
Searching &
Sorting
Multi-
dimensional
Arrays
Common
Errors
Exercises
18 / 30
Searching an Array
CSCE150A
Exercises
Algorithm 1: Searching Algorithm
19 / 30
Searching an Array
C code
CSCE150A
Introduction
Declaring,
Referencing 1 foreach index value i = 0, . . . , n − 2 do
Initialization 2 Find the index of the smallest element in the
Function Args subarray a[i, . . . , n − 1]
Constants 3 Swap the smallest element with the element
Returning
stored at index i
Arrays 4 end
Searching &
Sorting Algorithm 2: Selection Sort Algorithm
Multi-
dimensional
Arrays
Common
Errors
Exercises
21 / 30
Sorting an Array - Selection
CSCE150A
Introduction
Declaring, 1 while i ≤ n − 1 do
Referencing
2 while j ≤ n − 1 do
Initialization
3 if a[j] > a[j + 1] then
Function Args 4 Swap a[j] and a[j + 1]
Constants 5 end
Returning
Arrays
6 end
Searching & 7 end
Sorting
Multi-
Algorithm 3: Bubble Sort Algorithm
dimensional
Arrays
Common
Errors
Exercises
23 / 30
Sorting an Array - Bubble Sort
C code
CSCE150A
Introduction
Initialization
Two-dimensional arrays represent tables of data, matrices, and other
Function Args
two-dimensional objects
Constants Declare multidimensional arrays similar to regular arrays:
Returning
Arrays
int myArray[10][20];
Searching & This declares a 10 × 20 sized array
Sorting
Multi-
Interpretation: 10 rows, 20 columns
dimensional
Arrays
Common
Errors
Exercises
25 / 30
Multidimensional Arrays II
CSCE150A
Declaring,
Last row, last column: myArray[9][19] = 29;
Referencing
When iterating over a multidimensional array, use nested for loops
Initialization
Function Args
Constants
1 int a [10][10];
Returning
2 for ( i =0; i <10; i ++)
Arrays 3 for ( j =0; j <10; j ++)
Searching &
Sorting
4 a [ i ][ j ] = 1 + i + j ;
Multi-
dimensional
Arrays
Common
Errors
Exercises
26 / 30
Initialization of Multidimensional Arrays
CSCE150A
Introduction
Searching &
Sorting
This would initialize a 3 × 3 the array with all blank spaces.
Multi-
dimensional
Arrays
Common
Errors
Exercises
27 / 30
Initialization of Multidimensional Arrays
CSCE150A
Introduction
Declaring,
Referencing When declaring and initializing, you must still provide all dimensions
Initialization except the outer-most
Function Args
The compiler is able to deduce the outer-most dimension at compile
Constants
Returning
time
Arrays
Not sophisticated enough to deduce the rest
Searching &
Sorting
Multi-
dimensional
Arrays
Common
Errors
Exercises
28 / 30
Common Programming Errors
CSCE150A
Introduction
Declaring,
Referencing
Initialization
Most common error: out-of-range access error
Function Args
Segmentation fault, Bus error
Constants
Error may not be caught in some situations: unexpected results
Returning Use correct syntax when passing arrays as parameters
Arrays
Searching &
Sorting
Multi-
dimensional
Arrays
Common
Errors
Exercises
29 / 30
Exercises
CSCE150A
Introduction Write the following functions and write a main driver program to test
Declaring, them.
Referencing
Initialization
void printArray(int *array, int size) – prints the elements
Function Args
of an integer array
Constants
Returning
void printMatrix(int **array, int rows, int columns) –
Arrays prints the elements of an integer array
Searching &
Sorting double average(int *array, int size) – computes the
Multi- average of all elements in the array
dimensional
Arrays
Common
Errors
Exercises
30 / 30