Presentation is loading. Please wait.

Presentation is loading. Please wait.

16/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Methods to manipulate a collection of values under one name. Arrays: Homogenous.

Similar presentations


Presentation on theme: "16/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Methods to manipulate a collection of values under one name. Arrays: Homogenous."— Presentation transcript:

1 16/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Methods to manipulate a collection of values under one name. Arrays: Homogenous data Homogenous data Variable quantity of data items all of the same type Variable quantity of data items all of the same typeStructures: Heterogeneous data Heterogeneous data Fixed quantity of data values of different types Fixed quantity of data values of different types

2 26/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Arrays Declaration: [int-qty] [int-qty]Access:<var-name>[subscript]Subscript: Integer Integer In the range 0 to n-1, where n is the number of elements In the range 0 to n-1, where n is the number of elements

3 36/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Structures Definition: struct // typically in global area { …. /* composed of data types and element names */ …. /* composed of data types and element names */};Declaration: Access:<var-name>.<element-expression>

4 46/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Array Examples * int counts[100]; … counts[0] = 0; double speeds[2000]; … speeds[1999] = 1.23; string names[500]; … cout << “Name: “ << names[10];

5 56/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Structures Example Structures: struct employee struct employee{ string name; float annual_salary; int dept_num; }; employee boss; employee boss; boss.name = “Bill”; boss.name = “Bill”; cout << “Annual Salary is “ << boss.annual_salary << “\n”; cout << “Annual Salary is “ << boss.annual_salary << “\n”;

6 66/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays 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.

7 76/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays 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”;

8 86/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays 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];

9 96/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Sentinals int SENTINEL = -1; Sentinels may also be used: i = 0; while (speeds[i] != SENTINEL) cout << speed[i] << “\n”; ++ i; }

10 106/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Terminology Scalars: a single data value Vectors: a one-dimensional array Matrices: a two-dimensional array

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

12 126/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays 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.

13 136/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Using Strings in C++ #include #include Declaring strings: string name; string name; string account_number; string account_number; Using strings: Name = “Bob”; Name = “Bob”; cin >> account_number; cin >> account_number; whole_name = last_name + “, “ + first_name; // Concatenation whole_name = last_name + “, “ + first_name; // Concatenation Name_length = name.length(); Name_length = name.length();

14 146/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Pointers A pointer is a variable that contains the address of another variable, that is, a memory address. Definition: * Definition: * Access: for the pointer itself; * for the variable pointed to (“de-referencing”).

15 156/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Dynamic Memory Three areas of memory: Global: defined outside any function (allocated once) Global: defined outside any function (allocated once) Local: defined inside a function (allocated on the system stack) Local: defined inside a function (allocated on the system stack) Dynamic (also known as the heap) Dynamic (also known as the heap) Accessed through operators new and delete To request a section of memory: = new = new To access that memory: use operators [], *, -> To return that memory: delete To return that memory: delete

16 166/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Memory Examples #include #include 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[] }

17 176/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Reading Chapter 9 Chapter 11, Section 1-2 Chapter 13, Sections 1-2


Download ppt "16/19/2015 1:14 PM6/19/2015 1:14 PM6/19/2015 1:14 PMArrays and Structures Methods to manipulate a collection of values under one name. Arrays: Homogenous."

Similar presentations


Ads by Google