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

C_DOCUME~1_MAXWID~1 - 1 ee312 Spring 2008 Lecture 6 1 Announcements Lecture 6 • Make sure you read Chapter 8 • Topic for today – Arrays –

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: 1 ee312 Spring 2008 Lecture 6 1 Announcements Lecture 6 • Make sure you read Chapter 8 • Topic for today – Arrays – Covered over two lectures ee312 Spring 2008 Lecture 6 2 Arrays - DeFnitions • An array is group (or set) of multiple variables of the same data type. The values can be all: int, double, char, etc. • An array allows us to store and process all the values in a collection (or set) of data, rather than just the most recent value. They are stored in consecutive memory locations. • The whole collection is referred to by a single array name which contains a reference to its location • The individual variables (sometimes called elements or cells) are accessed by using an integer index (also called a subscript) which indicates that value’s position in the set. • Index values range from 0 to one less than the number of elements • Arrays can be multi-dimensional 2 ee312 Spring 2008 Lecture 6 3 One-Dimensional Arrays • A one-dimensional array is declared in the following way: int a[10]; • The number 10 declares that the array named a has ten elements (numbered from 0 to 9) a 0 1 2 3 4 5 6 7 8 9 Array name The individual elements Indices/subscripts ee312 Spring 2008 Lecture 6 4 Declaring Variable Arrays • The length of an array can be speciFed 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 .: • Variable-length arrays are only allowed in functions. – Is “main” a function? – Yes. • Does this compile? #include <stdio.h> #deFne n 10 /*int n=10; */ int a[n]; What about this? #include <stdio.h> int n=10; int a[n]; /* Wrong! */ int main( void ) /* the rest of the program */ 3 ee312 Spring 2008 Lecture 6 5 Array Subscripting Warning : Array bounds in C are not checked; when a subscript goes out of bounds, the result is unpredictable. On some systems, the following innocent-looking for statement causes an inFnite loop: int a[10], i; /* initialize all values of a to 0 */ for (i = 1; i <= 10; i++) a[i] = 0; /* wrong! */ •To select an element of an array, use the [ ] operator: a[0] a[i] a[i*2+1] /* any integer expression may be used */ •This operation is called array subscripting....
View Full Document

This note was uploaded on 04/15/2008 for the course EE 312 taught by Professor Shafer during the Spring '08 term at University of Texas at Austin.

Page1 / 14

C_DOCUME~1_MAXWID~1 - 1 ee312 Spring 2008 Lecture 6 1 Announcements Lecture 6 • Make sure you read Chapter 8 • Topic for today – Arrays –

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