Download presentation
Presentation is loading. Please wait.
Published byRegina Jones Modified over 8 years ago
1
1 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays
2
2 Arrays Array: sequential block of memory that holds variables of the same type Array can be declared for any type Example: int A[10] is an array of 10 integers. Examples: list of students’ marks series of numbers entered by user vectors matrices
3
3 Arrays in Memory Sequence of variables of specified type The array variable itself holds the address in memory of beginning of sequence Example: double s[10]; The k-th element of array A is specified by A[k-1] (0 based) 0123456789 s ……
4
4 Arrays in Memory Access array’s content Change array’s content
5
5 Example – Find Minimum int i, min, array[10]; printf("please enter 10 numbers:\n"); for(i = 0; i < 10; ++i) scanf("%d", &array[i]); min = array[0]; for(i = 1; i < 10; ++i) { if (array[i] < min) min = array[i]; } printf("the minimum is: %d\n", min);
6
6 Define 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
7
7 minimum with #define #include #define ARRAY_SIZE 10 int main(void) { int i, min, array[ARRAY_SIZE]; printf("please enter %d numbers:\n", ARRAY_SIZE); for(i = 0; i < ARRAY_SIZE; ++i) scanf("%d", &array[i]); min = array[0]; for(i = 1; i < ARRAY_SIZE; ++i) { if (array[i] < min) min = array[i]; } printf("the minimum is: %d\n", min); return 0; } Use capital letters
8
8 Initialization 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};
9
9 Exercise 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, it’s not there.
10
10 Solution (simple_search.c) #include #define ARRAY_SIZE 10 int main(void) { int array[ARRAY_SIZE], i, num; printf("Please enter %d numbers:\n", ARRAY_SIZE); for (i = 0; i < ARRAY_SIZE; ++i) scanf("%d", &array[i]); printf("Please enter a number to search for:\n"); scanf("%d", &num); for (i = 0; i < ARRAY_SIZE; ++i) { if (array[i] == num) { printf("I found it!\n"); return 0; } printf("Sorry, it's not there\n"); return 0; }
11
11 Some 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…)
12
12 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays
13
13 Arrays as function arguments Functions can accept arrays as arguments The array’s size also needs to be passed (why?)
14
14 Arrays 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?)
15
15 Example (mult_all.c) void mult_all(int arr[], int size, int multiplier) { int i = 0; for (i = 0; i < size; ++i) arr[i] *= multiplier; } int main() { int i,array[]={1,2,3,4,5,6,7,8,9,10}; mult_all(array,10,5); printf(“array is now:\n”); for (i=0; i<10; i++) printf(“%d “,array[i]); printf(“\n”); return 0; }
16
16 Example - Sort We would like to sort the elements in an array in an ascending order. 7 2854 2 4578 sort
17
17 Example of Sort (Bubble Sort) 7 2854 2 7854 2 7854 2 7584 2 7548 2 7548 2 5748 2 5478 2 7548 2 5478 2 4578 2 5478 2 4578 2 4578 (done)
18
18 Bubble Sort void sort(int a[], int size) { int i, j, temp; for (i = size - 1; i >= 0; --i) /* counting down */ { for (j = 0; j < i; ++j) /* bubbling up */ { if (a[j] > a[j+1]) /* if out of order... */ { /*... then swap */ temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; }
19
19 Using sort #include #define ARRAY_SIZE 5 void sort(int a[], int size); int main() { int array[ARRAY_SIZE] = {7, 2, 8, 5, 4}; int i = 0; sort(array, ARRAY_SIZE); /* print the sorted array */ for (i = 0; i < ARRAY_SIZE; ++i) printf("%d ", array[i]); return 0; }
20
20 Exercise (@ 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
21
21 Solution (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 < size; ++i) { if (arr1[i] != arr2[i]) return 0; } /* if we got here, both arrays are identical */ return 1; }
22
22 Agenda Arrays: Definition Memory Examples Passing arrays to functions Multi dimensional arrays
23
23 Multi-dimensional arrays Array of arrays: int A[2][3] = { {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 A[i][j] 123 456
24
24 Multi-dimensional arrays The size of the array can be determined by the compiler (not recommended): int B[][2] = {{1,2}, {2,3}, {3,4}}; Cannot skip this!!
25
25 Example: matrix addition #include #define SIZE 3 int main() { int A[SIZE][SIZE] = {{1,2,3}, {4,5,6}, {7,8,9}}; int B[SIZE][SIZE] = {{1,1,1}, {2,2,2}, {3,3,3}}; int C[SIZE][SIZE]; int i = 0, j = 0; for (i = 0; i < SIZE; ++i) for (j = 0; j < SIZE; ++j) C[i][j] = A[i][j] + B[i][j]; return 0; }
26
26 2D arrays as function arguments void print_matrix(int mat[3][3]) { int i, j; for (i = 0; i < 3; ++i) { for (j = 0; j < 3; ++j) printf("\t%d ", mat[i][j]); printf("\n"); } The second subscript must be specified and it must be constant
27
27 Exercise @ 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 = A*B) Matrix Multiplication: Print all the matrices on the screen
28
28 Solution mat_mul.c
29
29 Debugger (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
30
30 More time (no chance)? Talk about the loops-related questions from 2 weeks ago
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.