Title: Agenda
1Agenda
- Arrays
- Definition
- Memory
- Examples
- Passing arrays to functions
- Multi dimensional arrays
2Arrays
- Array sequential block of memory that holds
variables of the same type - Array can be declared for any type
- Example int A10 is an array of 10 integers.
- Examples
- list of students marks
- series of numbers entered by user
- vectors
- matrices
3Arrays in Memory
- Sequence of variables of specified type
- The array variable itself holds the address in
memory of beginning of sequence - Example
- double s10
- The k-th element of array A is specified by
Ak-1 (0 based)
0
1
2
3
4
5
6
7
8
9
s
4Arrays in Memory
- Access arrays content
- Change arrays content
5Example Find Minimum
- int i, min, array10
- printf("please enter 10 numbers\n")
- for(i 0 i lt 10 i)
- scanf("d", arrayi)
- min array0
- for(i 1 i lt 10 i)
- if (arrayi lt min)
- min arrayi
-
- printf("the minimum is d\n", min)
6Define
- Magic Numbers (like 10 in the last example) in
the program convey little information to the
reader - Hard to change in a systematic way
- define defines a symbolic name
- During preprocessing phase, symbolic names are
replaced by the replacement text
7minimum with define
- include ltstdio.hgt
- define ARRAY_SIZE 10
- int main(void)
-
- int i, min, arrayARRAY_SIZE
- printf("please enter d numbers\n",
ARRAY_SIZE) - for(i 0 i lt ARRAY_SIZE i)
- scanf("d", arrayi)
- min array0
- for(i 1 i lt ARRAY_SIZE i)
- if (arrayi lt min)
- min arrayi
-
- printf("the minimum is d\n", min)
return 0
8Initialization
- Can be initialized during declaration.
- the number of initializers cannot be more than
the number of elements in the array - but it can be less
- in which case, the remaining elements are
initialized to 0 - if you like, the array size can be inferred from
the number of initializers (not recommended) - by leaving the square brackets empty
- so these are identical declarations
- int array1 8 2, 4, 6, 8, 10, 12, 14, 16
- int array2 2, 4, 6, 8, 10, 12, 14,
16
9Exercise
- Write a program that gets 10 numbers from the
user. - It then accepts another number and checks to see
if - that number was one of the previous ones.
Example 1 Please enter 10 numbers 1 2 3 4 5 6 7
8 9 10 Please enter a number to search for 8 I
found it! Example 2 Please enter 10 numbers 1
2 3 4 5 6 7 8 9 10 Please enter a number to
search for 30 Sorry, its not there.
10Solution (simple_search.c)
- include ltstdio.hgt
- define ARRAY_SIZE 10
- int main(void)
-
- int arrayARRAY_SIZE, i, num
- printf("Please enter d numbers\n",
ARRAY_SIZE) - for (i 0 i lt ARRAY_SIZE i)
- scanf("d", arrayi)
- printf("Please enter a number to search
for\n") - scanf("d", num)
- for (i 0 i lt ARRAY_SIZE i)
- if (arrayi num)
- printf("I found it!\n")
- return 0
11Some Notes on Array
- Arrays versus basic variables
- Out of range
- Equality do not use the operator on arrays
(until we learn otherwise). Copy instead - Size is always a constant (until we learn
otherwise)
12Agenda
- Arrays
- Definition
- Memory
- Examples
- Passing arrays to functions
- Multi dimensional arrays
13Arrays as function arguments
- Functions can accept arrays as arguments
- The arrays size also needs to be passed (why?)
14Arrays as function arguments
- For example int calc_sum(int arr, int size)
- Within the function, arr is accessed in the usual
way - Changes to the array in the function change the
original array! (why?)
15Example (mult_all.c)
- void mult_all(int arr, int size, int
multiplier) -
- int i 0
- for (i 0 i lt size i)
- arri multiplier
-
- int main()
-
- int i,array1,2,3,4,5,6,7,8,9,10
- mult_all(array,10,5)
-
- printf(array is now\n)
- for (i0 ilt10 i)
- printf(d ,arrayi)
- printf(\n)
- return 0
16Example - Sort
- We would like to sort the elements in an array in
an ascending order.
sort
17Example of Sort (Bubble Sort)
(done)
18Bubble Sort
- void sort(int a, int size)
-
- int i, j, temp
- for (i size - 1 i gt 0 --i) / counting
down / -
- for (j 0 j lt i j) / bubbling
up / -
- if (aj gt aj1) / if out of
order... / -
- / ... then swap /
- temp aj
- aj aj1
- aj1 temp
-
-
-
19Using sort
- include ltstdio.hgt
- define ARRAY_SIZE 5
- void sort(int a, int size)
- int main()
-
- int arrayARRAY_SIZE 7, 2, 8, 5, 4
- int i 0
- sort(array, ARRAY_SIZE)
- / print the sorted array /
- for (i 0 i lt ARRAY_SIZE i)
- printf("d ", arrayi)
- return 0
20Exercise (_at_ home?)
- Implement a function that accepts two integer
arrays and returns 1 if they are equal, 0
otherwise.The arrays are of the same size - Write a program that accepts two arrays of
integers from the user and checks for equality
21Solution (compare_arrays.c)
- int compare_arrays(int arr1, int arr2, int
size) -
- int i 0
- / compare the elements one at a time /
- for (i 0 i lt size i)
- if (arr1i ! arr2i)
- return 0
- / if we got here, both arrays are identical
/ - return 1
22Agenda
- Arrays
- Definition
- Memory
- Examples
- Passing arrays to functions
- Multi dimensional arrays
23Multi-dimensional arrays
- Array of arrays
- int A23 1, 2, 3,
- 4, 5, 6
- Means an array of 2 integer arrays, each of
length 3. - Access j-th element of the i-array is
- Aij
24Multi-dimensional arrays
- The size of the array can be determined by the
compiler (not recommended) - int B2 1,2, 2,3, 3,4
Cannot skip this!!
25Example matrix addition
include ltstdio.hgt define SIZE 3 int main()
int ASIZESIZE 1,2,3, 4,5,6,
7,8,9 int BSIZESIZE 1,1,1, 2,2,2,
3,3,3 int CSIZESIZE int i 0, j
0 for (i 0 i lt SIZE i) for (j 0 j
lt SIZE j) Cij Aij
Bij return 0
262D arrays as function arguments
- The second subscript must be specified and it
must be constant
- void print_matrix(int mat33)
-
- int i, j
- for (i 0 i lt 3 i)
-
- for (j 0 j lt 3 j)
- printf("\td ", matij)
- printf("\n")
-
-
27Exercise _at_ home
- Write a program that defines 3 matrices A,B,C of
size 3x3 with float elements initialize the
first two matrices (A and B) - Compute the matrix multiplication of A and B and
store it in C (i.e. C AB) - Matrix Multiplication
- Print all the matrices on the screen
28Solution
mat_mul.c
29Debugger (if time allows)
Use the debugger on one of the above examples,
see that you can follow execution step by step
and see the variables values at each stage
30More time (no chance)?
Talk about the loops-related questions from 2
weeks ago