1 / 43

Arrays

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.

galena-hull
Download Presentation

Arrays

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

  2. Reading Sections 4.1, 4.2, 4.3

  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.

  4. color - describable by numbers

  5. 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

  6. music - describable by numbers

  7. here is what we're working towards: MATRICES

  8. what is a matrix? Numbers, whose rows and columns indicate properties and relationships Example: "3D" means [ width, height, depth ]

  9. 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 ]

  10. “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.

  11. 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

  12. 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!

  13. 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

  14. 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

  15. 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

  16. strings : #include <string> no more complicated than they seem: string Greeting = "Hello, how are you?";

  17. init arrays int z[5] = { 0, 0, 0, 0, 0 }; string names[3] = {“gary”, “frank”, “sue”};

  18. What are arrays good for? – • They are confusing just to read… int a[ 5 ]; EmployeeInfo[ 1021 ] Temperature[ x ]

  19. 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; }

  20. 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

  21. 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

  22. 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;

  23. array items are correlated by index

  24. continued lastName[ 1 ] = “Garcia”; firstName[ 1 ] = “Jerry”; age[ 1 ] = 71; lastName[ 2 ] = “Smith”; firstName[ 2 ] = “John”; age[ 2 ] = 24;

  25. continued lastName[ 3 ] = “Mouse”; firstName[ 3 ] = “Mickey”; age[ 3 ] = 75; lastName[ 4 ] = “Gabriel”; firstName[ 4 ] = “Peter”; age[ 4 ] = 63;

  26. array items are correlated by index table array record field field field

  27. 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.

  28. a well-known database

  29. 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; }

  30. 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 !!!

  31. 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.

  32. 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() {

  33. 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"

  34. 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.

  35. Changing array values – first name void changeFirstName( int indexToChange ) { string userInput; cout << "Enter First Name: "; cin >> userInput; firstName[ indexToChange ] = userInput; }

  36. Changing array values – last name void changeLastName( int indexToChange ) { string userInput; cout << "Enter Last Name: "; cin >> userInput; lastName[ indexToChange ] = userInput; }

  37. Lab 6 Create an iTunes database

  38. Help on lab 6….

  39. 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 );

  40. 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";

  41. The while(true) loop

More Related