week5 - EECS 280 Discussion Notes - Week 5 Outline -1)...

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

View Full Document Right Arrow Icon
EECS 280 Discussion Notes - Week 5 Outline ------- 1) Arrays: declaration and uses, strings as arrays 2) Pointers: conceptual 3) Passing pointers as arguments 4) The array/pointer relationship and pointer arithmetic Arrays ------ As discussed in class, an array is a declaration method that allows one to group together multiple instances of an object type while addressing them using a common variable name. For example, a declaration like: int myarr[10]; assigns memory for 10 integers counted from 0. .9 (remember how computer scientists count!). In other words, you can access these integers as a[0], a[1]. ..a[9]. We could have done this alternately (using traditional variable declaration technique) as: int myarr_0, myarr_1, myarr_2, myarr_3, myarr_4, myarr_5, myarr_6, myarr_7, myarr_8, myarr_9; Now, you can see how time consuming this can become. Imagine declaring 100 integers using this traditional method, gasp!. What if you now needed 101 integers, how would you have to change your program? Challenge: find the maximum integer among 100 integers. Think about getting all 100 integers from the user, and then finding the maximum value. One Solution: #include <iostream> using namespace std; int main( void ) { int a[100]; int curr_max = -100000; for ( int i=0; i<100; i++ ) { cin >> a[i]; } for ( int i=0; i<100; i++ ) { if( a[i] > curr_max ) { curr_max = a[i]; }
Background image of page 1

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

View Full DocumentRight Arrow Icon
} cout << "Maximum value in the array: " << curr_max; return 0; } Some uses of arrays: 1. To declare multiple variables of same type. 2. To access variables one after another in a loop. 3. To pass many variables to a function calls. 4. Quick access to a random index, say a[6] (compare to calling list_first() multiple times). Misc notes:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/04/2008 for the course EECS 215 taught by Professor Phillips during the Winter '08 term at University of Michigan.

Page1 / 6

week5 - EECS 280 Discussion Notes - Week 5 Outline -1)...

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

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