500 likes | 1.43k Views
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:
E N D
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
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
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
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
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
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
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
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
Sentinals • int SENTINEL = -1; • Sentinels may also be used: i = 0; while (speeds[i] != SENTINEL) cout << speed[i] << “\n”; ++ i; } Arrays and Structures
Terminology • Scalars: a single data value • Vectors: a one-dimensional array • Matrices: a two-dimensional array Arrays and Structures
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
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
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
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
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
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
Reading • Chapter 9 • Chapter 11, Section 1-2 • Chapter 13, Sections 1-2 Arrays and Structures