430 likes | 556 Views
Arrays. Reading. Sections 4.1, 4.2, 4.3. Computers. Perform repetitive, high-precision tasks Organize massive amounts of data to help, C++ offers many ways to create "lists". lists of like-items (all integers, for instance), are called Arrays. lists of unlike-items, are called Tables.
E N D
Reading Sections 4.1, 4.2, 4.3
Computers.... • Perform repetitive, high-precision tasks • Organize massive amounts of data • to help, C++ offers many ways to create "lists". • lists of like-items (all integers, for instance), are called Arrays. • lists of unlike-items, are called Tables.
how your screen is organized... pixel[ 1 1 ] = [ 255 0 0 ] // red pixel[ 2 1 ] = [ 255 0 0 ] // red pixel[ 3 1 ] = [ 255 0 0 ] // red pixel[ 4 1 ] = [ 255 0 0 ] // red pixel[ 5 1 ] = [ 255 0 0 ] // red pixel[ 6 1 ] = [ 255 0 0 ] // red pixel[ 7 1 ] = [ 220 25 10 ] // crimson
what is a matrix? Numbers, whose rows and columns indicate properties and relationships Example: "3D" means [ width, height, depth ]
imagine 4 such objects properties a bug: [ width, height, depth ] a rock: [ width, height, depth ] a person: [ width, height, depth ] a tree: [ width, height, depth ]
“Lists” of Variables Sometimes, it becomes necessary to order variables in a list: Temperature on April 1 = 90 Temperature on April 2 = 87 Temperature on April 3 = 84 in order to spot trends.
Lists of variables • All of the variables and values are of the same type – integers, strings, whatever. • Their order is important. • Their place in the list is important • May be shortened as… Temperature1 = 90 Temperature2 = 87 Temperature3 = 84
Track the order as a separate variable Called an “index”: Temperature[1] = 90 Temperature [2] = 87 Temperature [3] = 84 x = 2 Temperature [ x ] = ? Name [ index ] = value…. index can change!
Arrays • A difficult concept at first : int z[5]; • Means: z[0], z[1] , z[2], z[3], z[4] are separate integer variables • Starts at 0. • An array with 5 elements is indexed (or subscripted) from 0 to 4. • in C++: int z[ 5 ]; // define z as int list
Array example int x; int z[ 10 ]; for (x = 0; x <= 9; x = x+1) { z[x] = x; } What is the value of z[4]? 4! in fact… z[0] = 0 z[1] = 1 z[2] = 2 ... to z[9] = 9
Arrays do not have to be numbers int x = 0; string myArray[ 5 ]; myArray[0] = ”hello "; myArray[1] = ”we "; myArray[2] = ”have "; myArray[3] = ”tests "; myArray[4] = ”this week"; for (x=0; x <= 4; x=x+1) { cout << myArray[x]; } cout << endl; Finally, the "string" type
strings : #include <string> no more complicated than they seem: string Greeting = "Hello, how are you?";
init arrays int z[5] = { 0, 0, 0, 0, 0 }; string names[3] = {“gary”, “frank”, “sue”};
What are arrays good for? – • They are confusing just to read… int a[ 5 ]; EmployeeInfo[ 1021 ] Temperature[ x ]
What are arrays good for? – • They are hard to track… “context awareness” is difficult • What’s going on in this code? int x, RunningTotal = 0; int Sum[ 5 ] = { 0, 0, 0, 0, 0 }; for (x = 0; x <= 4; x++) { RunningTotal = RunningTotal + x Sum[ x ] = RunningTotal; }
Sum[0] = 0 • Sum[1] = 0 + 1 • Sum[2] = 0 + 1 + 2 • Sum[3] = 0 + 1 + 2 + 3 • Sum[4] = 0 + 1 + 2 + 3 + 4 = 10
What are arrays good for? • Think of the use of lists, that are • ordinal (ordered in sequence) • item positions are important • lists can be anything: strings, names, numbers, ages, costs, addresses
Using three arrays to keep information string lastName[ 5 ]; // too many to init string firstName[ 5 ]; int age[ 5 ] = { 0, 0, 0, 0, 0 }; e.g. lastName[ 0 ] = “Truman”; firstName[ 0 ] = “Harry”; age[ 0 ] = 175;
continued lastName[ 1 ] = “Garcia”; firstName[ 1 ] = “Jerry”; age[ 1 ] = 71; lastName[ 2 ] = “Smith”; firstName[ 2 ] = “John”; age[ 2 ] = 24;
continued lastName[ 3 ] = “Mouse”; firstName[ 3 ] = “Mickey”; age[ 3 ] = 75; lastName[ 4 ] = “Gabriel”; firstName[ 4 ] = “Peter”; age[ 4 ] = 63;
array items are correlated by index table array record field field field
What is this? • Information that is grouped by index • Kept in arrays e.g. lastName[1], firstName[1], and age[1] go together to form one profile for person #1 Is a DATABASE. We’re creating our own database.
printing out the info – note index int ix = 0; // note: don’t use the word index for (int ix=0; ix<=4; ix++) { cout << “ First Name: ” << firstName[ix ] ; cout << “ Last Name: ” << lastName[ix ] ; cout << “ Age: ” << age[ix ] << “ for index ” << x; }
printing out one record int ix = 0; cin >> ix; // note: don’t use the word index if ((ix >=0) && (ix <=4)) { cout << “ Item: ” << ix << endl; cout << “First Name: ” << firstName[ix ] << endl; cout << “Last Name: ” << lastName[ix ] << endl; cout << “Age: ” << age[ix ] << endl; } AND !!!
just imagine… • Each array contains thousands of items • More arrays to hold soc. sec. #, birthdate, pay scale, years of service • Program ways to enter data as well as display. • A full-scale Database Management program.
Define arrays OUTSIDE OF MAIN #include <iostream> #include <string> using namespace std; string lastName[ 5 ]; string firstName[ 5 ]; int age[ 5 ] = { 0, 0, 0, 0, 0 }; int main() {
why? Variables that are declared outside of main can be shared with functions No need to pass them in the parenthesis These are called "global"
Changing array values - age void changeAge( int indexToChange ) { int newAge; cout << “Enter age: "; cin >> newAge; age[ indexToChange ] = newAge; } note: • array index is a parameter passed into the method • nothing is returned, "global" array is changed directly within the method.
Changing array values – first name void changeFirstName( int indexToChange ) { string userInput; cout << "Enter First Name: "; cin >> userInput; firstName[ indexToChange ] = userInput; }
Changing array values – last name void changeLastName( int indexToChange ) { string userInput; cout << "Enter Last Name: "; cin >> userInput; lastName[ indexToChange ] = userInput; }
Lab 6 Create an iTunes database
Lab 6 Hint 7: Spaces freak the computer out, sousing cin with spaces requires a change. Use the following as a substitute for the usual cin >> variable. Instead of: cin >> newSongName; // which can’t handle strings with spaces Use the following code to place get a string with spaces, into the variable “newSong”: do { getline( cin, newSong ); } while ( newSong.compare("") == 0 );
DO NOT: while (true) {Song[ 0 ] = "The Cave";Artist[ 0 ] = "Mumford & Sons";Album[ 0 ] = "Sigh No More";Song[ 1 ] = "Toxicity";Artist[ 1 ] = "System of a Down";Album[ 1 ] = "Toxicity";Song[ 2 ] = "The Battle Of Evermore";Artist[ 2 ] = "Led Zeppelin";Album[ 2 ] = "Led Zeppelin IV";