C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture07_arrays_2d

# C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture07_arrays_2d

This preview shows pages 1–6. Sign up to view the full content.

1 ee312 Spring 2008 Lecture 7 1 Announcements Lecture 7 • Topics for today – array initializers – arrays 2D – Sorting 1D • Assignment 2 Questions – math.h – ceiling, floor. – Rounding. ee312 Spring 2008 Lecture 7 2 One-Dimensional Arrays • (recap) A one-dimensional array is declared in the following way: int a[10]; a 0 1 2 3 4 5 6 7 8 9 Array name The individual elements Indicies/subscripts We can access and use the individual elements like any variable a[i] = a[j] + a [9] - a[k + 2] ;

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

View Full Document
2 ee312 Spring 2008 Lecture 7 3 Variable-Length Arrays (C99 Only) • (recap) In C99, the length of an array can be specified by a variable or other expression: int a[2*n+1]; • An array declared in this way is said to be a variable-length array (it’s not really). • Variable-length arrays are only allowed in functions (including main). ee312 Spring 2008 Lecture 7 4 Array Subscripting • (recap) What C type can you use for subscripting an array? – int – Any expression that evaluates to an int • Is it possible to use a character as a subscript? – Yes. Why? – The C compiler treats characters as integers.
3 ee312 Spring 2008 Lecture 7 5 Using Characters as Subscripts • C treats characters as integers. • Where can you find the equivalent integer value for a character? – ASCII table – ASCII table in book – Complier Help ee312 Spring 2008 Lecture 7 6 Using Characters as Subscripts • ASCII integer values for characters are ordered – A-Z, then – a-z. • A-Z in ASCII are integer values 65 – 90 • a-z in ASCII are integer values 97-122 • Where in your book is there an ASCII table?

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

View Full Document
4 ee312 Spring 2008 Lecture 7 7 Using Characters as Subscripts • PROBLEM! ASCII integer values for a-z and A-Z are not continuous! • AND! They do not start a 0 • What do we do if we want to index an array by a character? – Make a really large array and keep track of ranges of a-z and A-Z? --- not best solution • We can ‘shift’ index references ee312 Spring 2008 Lecture 7 8 Shifting index references • If we need an array that we can index by C characters – We can ‘shift’ the value of array subscript. – C expression “ ‘a’ - 97 “ does equal ‘0’ – Example: – To get to the first index of an array: • array_of_alphabet_chars[‘a’-97] is the same as writing • array_of_alphabet_chars[0]. • (warning) Does this work with non-ASCII based character sets?
5 ee312 Spring 2008 Lecture 7 9 Shifting index references Hard coding the integer value of the character ‘a’ only works if ‘a’ == 97 How do we get around this? Since C treats characters as integers, we can subtract – Example: • Instead of this array_of_alphabet_chars[‘a’-97] • We can write array_of_alphabet_chars[‘a’-’a’] The expression ‘a’-’a’ equals What? More practical example; Say that variable ch holds a

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 22

C__DOCUME~1_MAXWID~1_LOCALS~1_Temp_plugtmp-27_lecture07_arrays_2d

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

View Full Document
Ask a homework question - tutors are online