Lec2 - Lecture 2 Arrays Vectors Pointers PIC 10B Todd...

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

View Full Document Right Arrow Icon
1 Lecture 2: Arrays, Vectors, & Pointers PIC 10B Todd Wittman Ch. 9 : Arrays box4 An array is a list of the same data type. The size of an array is fixed. Declare an array with [ ] after the variable name. const int size = 10; //Must be const! string droids[size]; box4 We access an individual element of array a with a[i]. Remember arrays start at position 0. for (int i=0; i < size; i++) droids[i] = "R2D2"; box4 We can create 2D arrays to store data in rows and columns. int randoms[10][5]; //10 rows x 5 columns for (int i=0; i < 10; i++) for (int j=0; j < 5; j++) randoms[i][j] = 1+rand()%100; box4 What does the code above do?
Image of page 1

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

View Full Document Right Arrow Icon
2 Arrays in Functions box4 In the function parameters, denote a parameter as an array with blank brackets [ ]. box4 Generally have to pass the size of the array as a parameter also. void printArray (string names[ ], int size) { for (int i=0; i < size; i++) cout << names[i] << "\n"; return; } box4 The call to a function just passes the array name. string heroes[4] = {"Luke", "Leia", "Han", "Chewbacca"}; printArray (heroes,4); box4 Arrays are always passed by reference, so don't bother with an &. This is done to save memory by preventing local copies of the array variables. 2D Arrays box4 To pass a 2D array, need to specify the dimensions. Best to declare dimensions as global constants. const int table_rows = 20; const int table_cols = 40; void printTable (double table [table_rows] [table_cols]) { for (int i = 0; i < table_rows; i++) { for (int j=0; j < table_cols; j++) { cout << setw(10) << table [i] [j]; } cout << "\n"; } return; } Call this function in main (or wherever) with: printTable (table);
Image of page 2
3 Ch. 9 : Vectors box4 Arrays are limited because they can't change size. box4 Unless you want a 2D array, I would suggest using a vector . It's more flexible and has some nice member functions. box4 To use vectors, need library: #include <vector> box4 The basic form of a vector declaration is: vector<data_type> variable_name (size); box4 For example: vector<string> heroes(4); //Not [] box4 We can still access the element in position i of vector v with v[i].
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
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