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

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

View Full Document Right Arrow Icon
CS 11 C track: lecture 3 ± This week: ± Arrays ± one-dimensional ± multidimensional ± Command-line arguments ± 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 ± What is an "array"? ± A way to collect together data of a single type in a single object ± A linear sequence of data objects e.g. ± array of int s ± array of char s (string)
Background image of page 2
Creating and using arrays ± 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) ± Arrays can be ± initialized ± partially initialized ± not initialized ± Uninitialized space contains? ± "garbage"
Background image of page 4
One-dimensional arrays (2) ± 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) ± Note on partial initialization: int my_array[10] = { 1, 2, 3, 4, 5 }; ± rest of array initialized to 0 int my_array[10]; ± entire array uninitialized ± contains garbage
Background image of page 6
One-dimensional arrays (4) ± Explicit initialization of arrays: int i; int my_array[10]; for (i = 0; i < 10; i++) { my_array[i] = 2 * i; } ± Usually the best 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) ± 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]); ± No checking! ± C is an UNSAFE language!
Background image of page 8
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 9

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

View Full DocumentRight Arrow Icon
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 10
Two-dimensional arrays (3) ± Two-dimensional arrays can be split into component one-dimensional arrays: int arr[2][3]; /* initialize. .. */ /* arr[0] is array of 3 ints */ /* arr[1] is another array of 3 ints */
Background image of page 11

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

View Full DocumentRight Arrow Icon
Two-dimensional arrays (4) ± Conceptual picture of arr : 12 3 - 1 2 85 46 99 arr[0] arr[1]
Background image of page 12
Two-dimensional arrays (5) ± Actual picture of arr : 12 3 - 1 2 85 46 99 arr[0] arr[1]
Background image of page 13

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

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

This note was uploaded on 04/30/2008 for the course C CMPE 150 taught by Professor Tuna during the Spring '08 term at Boğaziçi University.

Page1 / 35

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

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

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