W17-Arrays

W17-Arrays - Arrays 10/31/05 1 One-dimensional Arrays An...

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

View Full Document Right Arrow Icon
10/31/05 1 Arrays 10/31/05 2 One-dimensional Arrays • An indexed set of variables with common name. • A table with only one row, an indexed list. int list[5]; • Declares 5 (integer) elements with name list , and index 0 thru 4. list[0], list[1], list[2], list[3], and list[4] number of elements (must be integer) indices 10/31/05 3 1-D array is a list. if list[5] contains integers 10 thru 14 then: index variable value algebra equivalent 0 list[0] 10 L 0 1 list[1] 11 L 1 2 list[2] 12 L 2 3 list[3] 13 L 3 4 list[4] 14 L 4
Background image of page 1

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

View Full DocumentRight Arrow Icon
10/31/05 4 Assignment int list[3], b=5, c=0, d; list[0] list[1] list[2] b c d -- -5 0 - 1- - 5 0 - 15 0 - 0 - 55 0 - 0 5 list[0] = 1; list[1] = b; list[2] = list[0]; list[c] = list[1]; d = list[1]; 10/31/05 5 Any Type Arrays Work •in t list[5]; /*array of integers */ •f lo a t grade[10]; /* array of float */ • double speed[100]; /* array of double */ • char lettGrade[20]; /* array of char */ •e t c . 10/31/05 6 How to put data into an array • Assignment list[4] = 5; • Initialization when declared int list[] = {1, 2, 3, 4, 5}; int list[5] = {0}; /* sets all 5 values to 0 */ • Loop for (i=0; i<5; ++i) list[i] = i+1;
Background image of page 2
10/31/05 7 Initializing Char Arrays The previous methods work char vowels[5]; /* declare the array */ vowels[0] = 'a'; /* assign one at a time */ vowels[1] = 'e'; /* etc. */ or char vowels[] = {'a', 'e', 'i', 'o', 'u'}; /* declare and initialize */ but a convenient shortcut is to use a string.
Background image of page 3

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

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

This note was uploaded on 06/28/2009 for the course CS 151 taught by Professor Staff during the Spring '08 term at Oregon State.

Page1 / 9

W17-Arrays - Arrays 10/31/05 1 One-dimensional Arrays An...

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

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