Title: Arrays
1 2Lists 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.
3Lists 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
- Temp1 90
- Temp2 87
- Temp3 84
4Track the order as a separate variable
- Called an index
- Temp1 90
- Temp2 87
- Temp3 84
- x 2
- Temp x ?
- Name index value. index can change!
5Arrays
- A difficult concept at first int z5
- Means z0, z1 , z2, z3, z4 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
6Array example
- int x
- int z 10
- for (x 0 x lt 9 x x1)
-
- zx x
-
- What is the value of z4? 4!
- in fact
- z0 0 z1 1 z2 2 ... to z9
9
7Arrays do not have to be numbers
- int x
- string myArray 5
- myArray0 hello"
- myArray1 we don't"
- myArray2 have"
- myArray3 a test"
- myArray4 next week"
- for (x0 x lt 4 x)
- cout ltlt myArrayx
-
- cout ltlt endl
8What are arrays good for?
- They are confusing just to read
- int a 5
- EmployeeInfo 1021
- Temperature x
9What are arrays good for?
- They are hard to track context awareness is
difficult - Whats going on in this code?
- int x, RunningTotal 0
- int Sum 5
- for (x 0 x lt 4 x)
-
- RunningTotal RunningTotal x
- Sum x RunningTotal
-
10- Sum0 0
- Sum1 0 1
- Sum2 0 1 2
- Sum3 0 1 2 3
- Sum4 0 1 2 3 4 10
11What 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
12Using three arrays to keep information
- string lastName 5
- string firstName 5
- int age 5
- e.g.
- lastName 0 Truman
- firstName 0 Harry
- age 0 175
13array items are correlated by index
14continued
- lastName 1 Garcia
- firstName 1 Jerry
- age 1 55
- lastName 2 Smith
- firstName 2 John
- age 2 24
15continued
- lastName 3 Mouse
- firstName 3 Mickey
- age 3 75
- lastName 4 Buckley
- firstName 4 Mike
- age 4 39
16array items are correlated by index
table
record
field
17What is this?
- Information that is grouped by index
- Kept in arrays
- e.g. lastName1, firstName1, and age1 go
together to form one profile for person 1 - Is a DATABASE.
- Were creating our own database.
18printing out the info note index
- int index 0
- cin gtgt index
- if (index gt0) (index lt4)
-
- cout ltlt Item ltlt index ltlt endl
- cout ltlt First Name firstName index ltlt
endl - cout ltlt Last Name lastName index ltlt
endl - cout ltlt Age age index ltlt endl
-
-
19just 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.
20Changing array values - age
- void changeAge( int indexToChange )
-
- int newAge
- cout ltlt Enter age "
- cin gtgt newAge
- age indexToChange newAge
-
- note
- array index is a parameter passed into the method
- nothing is returned, array is changed directly
within the method.
21Changing array values first name
- void changeFirstName( int indexToChange )
-
- string userInput
- cout ltlt "Enter First Name "
- cin gtgt userInput
- firstName indexToChange userInput
-
22Changing array values last name
- void changeLastName( int indexToChange )
-
- string userInput
- cout ltlt "Enter Last Name "
- cin gtgt userInput
- lastName indexToChange userInput
-
23printing out the info note index
- for (int index0 indexlt4 index)
-
- cout ltlt First Name ltlt firstName index
- cout ltlt Last Name ltlt lastName index
- cout ltlt Age ltlt age index ltlt for
index ltlt x -