{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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]; }
Image of page 1

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

View Full Document Right 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).
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern