1 / 40

Exercise 6

Exercise 6. Arrays. Arrays. A block of many variables of the same type Array can be declared for any type E.g. int A[10] is an array of 10 integers. Examples: list of students’ marks series of numbers entered by user vectors matrices. Arrays in Memory.

clodia
Download Presentation

Exercise 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exercise 6 Arrays

  2. Arrays • A block of many variables of the same type • Array can be declared for any type • E.g. int A[10] is an array of 10 integers. • Examples: • list of students’ marks • series of numbers entered by user • vectors • matrices

  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) … 0 1 2 3 4 5 6 7 8 9 … S

  4. Example - reverse #include <stdio.h> int main(void) { int i, A[10]; printf("please enter 10 numbers:\n"); for(i=0; i<10; i++) scanf("%d", &A[i]); printf("numbers in reversed order:\n"); for(i=9; i>=0; i--) printf("%d\n", A[i]); return 0; }

  5. 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

  6. Reverse with #define /* get 10 integers from the user and printing them in reversed order*/ #include <stdio.h> #define NUM 10 int main(void) { int i; int A[NUM]; printf(“Please enter %d numbers:\n",NUM); for (i=0; i<NUM; i++) scanf("%d",&A[i]); printf("numbers in reversed order:\n"); for (i=NUM-1; i>=0; i--) printf("%d\n",A[i]);

  7. Initialization • Like in the case of regular variables, we can initialize the array 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

  8. Initialization • if you like, the array size can be inferred from the number of initializers 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. Example Sort.c

  10. A[0] A[1] A[2] Sort – step by step 4 8 2 min_index ---- i j tmp #include <stdio.h> #define SIZE 3 int main(void) { int A[SIZE] = {4,8,2}; int min_index; int i,j,tmp; ---- ---- ----

  11. A[0] A[1] A[2] Sort – step by step 4 8 2 min_index ---- i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 ---- ----

  12. A[0] A[1] A[2] Sort – step by step 4 8 2 min_index 0 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 ---- ----

  13. A[0] A[1] A[2] Sort – step by step 4 8 2 min_index 0 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 1 ----

  14. A[0] A[1] A[2] Sort – step by step 4 8 2 min_index 0 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 1 ----

  15. A[0] A[1] A[2] Sort – step by step 4 8 2 min_index 0 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 2 ----

  16. A[0] A[1] A[2] Sort – step by step 4 8 2 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 2 ----

  17. A[0] A[1] A[2] Sort – step by step 4 8 2 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 3 ----

  18. A[0] A[1] A[2] Sort – step by step 4 8 2 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 3 4

  19. A[0] A[1] A[2] Sort – step by step 2 8 2 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 3 4

  20. A[0] A[1] A[2] Sort – step by step 2 8 4 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 0 3 4

  21. A[0] A[1] A[2] Sort – step by step 2 8 4 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 3 4

  22. A[0] A[1] A[2] Sort – step by step 2 8 4 min_index 1 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 3 4

  23. A[0] A[1] A[2] Sort – step by step 2 8 4 min_index 1 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 2 4

  24. A[0] A[1] A[2] Sort – step by step 2 8 4 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 2 4

  25. A[0] A[1] A[2] Sort – step by step 2 8 4 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 3 4

  26. A[0] A[1] A[2] Sort – step by step 2 8 4 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 3 8

  27. A[0] A[1] A[2] Sort – step by step 2 4 4 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 3 8

  28. A[0] A[1] A[2] Sort – step by step 2 4 8 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 1 3 8

  29. A[0] A[1] A[2] Sort – step by step 2 4 8 min_index 2 i j tmp for(i=0; i<SIZE-1; i++) { min_index=i; for(j=i+1; j<SIZE; j++) min_index=((A[min_index]>A[j] ? j) : min_index); tmp=A[i]; A[i]=A[min_index]; A[min_index]=tmp; } 2 3 8

  30. Exercise Write a program that gets an input line from the user (ends with ‘\n’) and displays the number of times each letter appears in it. Example: For the input line: hello, world! The output should be: d – 1 e – 1 h – 1 l – 3 o – 2 w – 1 r – 1 Assume that the input is all in lower-case.

  31. Solution letter_count.c

  32. 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]

  33. Multi-dimensional arrays • The size of the array can be determined by the compiler: double B[][2] = {{1,2}, {2,3}, {3,4}}; Cannot skip this!!

  34. Example: matrix addition • #include <stdio.h> • #define SIZE 3 • int main() • { • double A[][SIZE] = {{1,2,3}, {4,5,6}, {7,8,9}}; • double B[][SIZE] = {{1,1,1}, {2,2,2}, {3,3,3}}; • double C[SIZE][SIZE]; • int i, j; • for(i=0; i<SIZE; i++) • for(j=0; j<SIZE; j++) • C[i][j]=A[i][j] + B[i][j]; • return 0; • }

  35. Exercise • 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) • Print all the matrices on the screen

  36. Arrays as function arguments • Functions can accept arrays as arguments • Usually the array’s size also needs to be passed (why?)

  37. Arrays as function arguments • For example: int CalcSum(int arr[], int size); • Within the function, arr is accessed in the usual way • Changes in the function affect the original array!!

  38. Example • calc_sum.c

  39. Exercise • Implement a function that accepts two integer arrays and returns 1 if they are equal, 0 otherwise • Write a program that accepts two arrays of integers from the user and checks for equality

  40. Solution • compare_arrays.c

More Related