C_lecture_3

C_lecture_3 - CS 11 C track: lecture 3 n This week: n...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 11 C track: lecture 3 n This week: n Arrays n one-dimensional n multidimensional n Command-line arguments n Assertions
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Arrays n What is an "array"? n A way to collect together data of a single type in a single object n A linear sequence of data objects e.g. n array of int s n array of char s (string)
Background image of page 2
Creating and using arrays n One-dimensional array of three ints: int arr[3]; int sum; arr[0] = 1; arr[1] = 22; arr[2] = -35; sum = arr[0] + arr[1] + arr[2];
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
One-dimensional arrays (1) n Arrays can be n initialized n partially initialized n not initialized n Uninitialized space contains? n "garbage"
Background image of page 4
One-dimensional arrays (2) n Examples: int my_array[10]; /* not initialized */ int my_array[5] = { 1, 2, 3, 4, 5 }; /* initialized */ int my_array[] = { 1, 2, 3, 4, 5 }; /* OK, initialized */ int my_array[4] = { 1, 2, 3, 4, 5 }; /* warning */ int my_array[10] = { 1, 2, 3, 4, 5 }; /* OK, partially initialized */
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
One-dimensional arrays (3) n Note on partial initialization: int my_array[10] = { 1, 2, 3, 4, 5 }; n rest of array initialized to 0 int my_array[10]; n entire array uninitialized n contains garbage
Background image of page 6
One-dimensional arrays (4) n Explicit initialization of arrays: int i; int my_array[10]; for (i = 0; i < 10; i++) { my_array[i] = 2 * i; } n This is the most flexible approach
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
One-dimensional arrays (5) n Some bad things that can happen. .. int my_array[10]; /* What happens here? */ printf("%d\n", my_array[0]); /* What happens here? */ printf("%d\n", my_array[1000]); n No checking! n C is an UNSAFE language!
Background image of page 8
One-dimensional arrays (6) n NOTE! The following is illegal: int my_array[5]; my_array = { 1, 2, 3, 4, 5 }; /* WRONG */ n The { 1, 2, 3, 4, 5 } syntax is only usable when declaring a new variable, and not for reassigning the contents of the array int my_array[5] = { 1, 2, 3, 4, 5 }; /* OK */ int my_array[] = { 1, 2, 3, 4, 5 }; /* OK */
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Two-dimensional arrays (1) int arr[2][3]; /* NOT arr[2, 3] */ int i, j; int sum = 0; arr[0][0] = 1; arr[0][1] = 23; arr[0][2] = -12; arr[1][0] = 85; arr[1][1] = 46; arr[1][2] = 99; /* continued on next slide */
Background image of page 10
Two-dimensional arrays (2) for (i = 0; i < 2; i++) { for (j = 0; j < 3; j++) { sum += arr[i][j]; } } printf("sum = %d\n", sum);
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 38

C_lecture_3 - CS 11 C track: lecture 3 n This week: n...

This preview shows document pages 1 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online