1 / 17

Arrays and Structures

Arrays and Structures. Methods to manipulate a collection of values under one name. Arrays: Homogenous data Variable quantity of data items all of the same type Structures: Heterogeneous data Fixed quantity of data values of different types. Arrays. Declaration:

trey
Télécharger la présentation

Arrays and Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Arrays and Structures • Methods to manipulate a collection of values under one name. • Arrays: • Homogenous data • Variable quantity of data items all of the same type • Structures: • Heterogeneous data • Fixed quantity of data values of different types Arrays and Structures

  2. Arrays • Declaration: <data-type> <var-name>[int-qty] • Access: <var-name>[subscript] • Subscript: • Integer • In the range 0 to n-1, where n is the number of elements Arrays and Structures

  3. Structures • Definition: struct <struct-name> // typically in global area { <elements>…. /* composed of data types and element names */ }; • Declaration: <struct-name> <var-name> • Access: <var-name>.<element-expression> Arrays and Structures

  4. Array Examples * int counts[100]; … counts[0] = 0; • double speeds[2000]; … speeds[1999] = 1.23; • string names[500]; … cout << “Name: “ << names[10]; Arrays and Structures

  5. Structures Example • Structures: • struct employee { string name; float annual_salary; int dept_num; }; • employee boss; • boss.name = “Bill”; • cout << “Annual Salary is “ << boss.annual_salary << “\n”; Arrays and Structures

  6. Nesting • An array may be an array of arrays or an array of structures. • A structure may have arrays as members or other structures as members. • These arrays or structures may also have arrays or structures as members, and so forth to any arbitrary depth. Arrays and Structures

  7. Nesting Example struct class { string time; string place; string students[30]; // structure w/ array }; class schedule[5]; // array of structures schedule[0].place = “Robinson 310”; schedule[0].students[0] = “Bill”; Arrays and Structures

  8. Arrays and For Loops • For loops are typically used to process arrays. • Typically when using arrays, the number of elements that actually contain valid data is saved in a variable. • for (i=0; i<NUM_ELEMENTS; ++i) counts[i] = 0; • for (i=0; i<n; ++i) area[i] = base[i] * height[i]; Arrays and Structures

  9. Sentinals • int SENTINEL = -1; • Sentinels may also be used: i = 0; while (speeds[i] != SENTINEL) cout << speed[i] << “\n”; ++ i; } Arrays and Structures

  10. Terminology • Scalars: a single data value • Vectors: a one-dimensional array • Matrices: a two-dimensional array Arrays and Structures

  11. Strings • A string is a array of characters. • A string literal is delimited by double-quotation marks. • Examples: • “Computer” • “special characters in it: !@#$\”\\ ” • “one” • “1” • “” • NOT strings: ‘a’, ‘1’, 1, one Arrays and Structures

  12. Another method to do input • getline(cin, a_string, ‘\n’); This will accept all input up to a newline character. It will store the input (NOT the newline) in a_string. • There are functions to do character-at-a-time input, as well as other special input functions (see lecture on streams and files). • In practice, these functions sometimes do not mix well with cin, with erratic results. Arrays and Structures

  13. Using Strings in C++ • #include <string> • Declaring strings: • string name; • string account_number; • Using strings: • Name = “Bob”; • cin >> account_number; • whole_name = last_name + “, “ + first_name; // Concatenation • Name_length = name.length(); Arrays and Structures

  14. Pointers • A pointer is a variable that contains the address of another variable, that is, a memory address. • Definition: <data-type> * <var-name> • Access: <var-name> for the pointer itself; *<var-name> for the variable pointed to (“de-referencing”). Arrays and Structures

  15. Dynamic Memory • Three areas of memory: • Global: defined outside any function (allocated once) • Local: defined inside a function (allocated on the system stack) • Dynamic (also known as the heap) • Accessed through operators new and delete • To request a section of memory: <pntr-var> = new <data-type> • To access that memory: use operators [], *, -> • To return that memory: delete <pntr-var> Arrays and Structures

  16. Memory Examples #include <iostream> using namespace std; int g; // global: visible from here to end of file void main() { float l; // local: visible from here to end of block double* h = new double; // heap allocation delete h; // heap de-allocation int* weights = new int[num_weights]; // delete[] } Arrays and Structures

  17. Reading • Chapter 9 • Chapter 11, Section 1-2 • Chapter 13, Sections 1-2 Arrays and Structures

More Related