CSC 107 – Programming For Science. Today’s Goal Variables Variable Variable name location to...

21
LECTURE 21: ARRAYS CSC 107 – Programming For Science

Transcript of CSC 107 – Programming For Science. Today’s Goal Variables Variable Variable name location to...

Page 1: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

LECTURE 21: ARRAYS

CSC 107 – Programming For Science

Page 2: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Today’s Goal

Become familiar with simple arrays Declaring variables that hold an array Writing the code which assign data to array

entries Using values stored in an array

Be able to explain why arrays ♥ loops

Page 3: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Variables

Variable name location to store data Only for humans; 0x7E8A2410 harder to

remember Assignments update memory location with

new value Memory location updated by assignment

ONLY When variable is used in program…

…uses current value at that memory location

Variable can store only one value Often want multiple values, like adjusting

for interest

Page 4: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Getting an “A”

Page 5: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Adjusting For Interest

First call, computed result for every year Then all but last year computed in second

call Third time we called function, computed all

but last 3… and so on…

Only adjusted for 1 year last time we called function

Interest rate, amount, and results were constant Unless save in variables, we have to

recompute Using variables required knowing years

when coding

Page 6: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Could Have Spent Time

Page 7: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Can Make Stronger, Bigger

Arrays are variables that can hold many items Creates range of locations in which to store

data Locations are numbered sequentially from 0

Array entries like variables in their own right To be able to use them, must declare array

(& entries) Value unknown until assigned in the

program But not a true variable, entries depend

on array Access only via array using the entry's

index

Page 8: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Declaring Array Variables

Like all variables, must declare before use

Type, name, & size needed for array declaration Still variables, so names follow usual rules Variable is array of the type, so use any

legal type Each of the array's entries hold value of

that type Size must be integer since ½ a value hard

to use

Page 9: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Declaring Array Variables

Like all variables, must declare before use

Type, name, & size needed for array declaration Still variables, so names follow usual rules Variable is array of the type, so use any

legal type Each of the array's entries hold value of

that type Size must be integer since ½ a value hard

to use

Page 10: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Declaring Array Size

When declaring array, best use constant for size Changing is easy if larger size needed later Have simple way to find array's size during

program Explain reasoning for size using good

constant name Type, name, & size needed for array

declarationint planetsWeight[NOT_PLUTO];float armada[MAX_SHIPS];double annualIncomes[MAX_LIFETIME];char responses[17];long timeToWait[35];

Page 11: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Initializing an Array

Can set entries’ initial values when array declared Just like with variables, value undefined if not

initialized Must specify value for every entry

Initialize all entries to same value, if desired Or the value of each entry specified separately Additional way of specifying size also provideddouble taxrate[LEVELS] ={0.15, 0.25, 0.3};

int vector[100]={0}; // all 100 entries set to 0

int eger[]={5,0,-5}; // eger's size will be 3

Page 12: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Legal Array Entries

Access array's entries indexed from 0 to size-1 0, 1, 2, 3, 4 legal if size of 5 used to declare

array Array created with size of 8: 0, 1, 2, 3, 4, 5, 6, 7 legal

0 only legal index if size declared as 1

Page 13: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Legal Array Entries

Access array's entries indexed from 0 to size-1 0, 1, 2, 3, 4 legal if size of 5 used to declare

array Array created with size of 8: 0, 1, 2, 3, 4, 5, 6, 7 legal

0 only legal index if size declared as 1 Stupidity defense legal, if array declared

with size 0

Page 14: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Guns (& C++) Don't Kill

C++ make arrays easy, but mistakes easy also Code can access any index within an array No problems compiling, as long as index an int

Includes ridiculous indices like -1 or 1029374729192

To find size, could try using sizeof(array variable)

Entry outside array bounds accessed by program Program may crash with “Segmentation

Fault” Other variable's value used and updated Program may be able to complete normally

Page 15: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Using An Array

Within an array, each entry behaves like variable But need array variable to access the entry To use or assign entry, specify index inside

brackets Example code snippet computing powers

of 2:

long lA[10];lA[0] = 1;for (long i=0; i < sizeof(lA)/sizeof(long);i++){ lA[i] = lA[i-1] * 2; cout << lA[i] << endl;}

Page 16: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Using An Array

Within an array, each entry behaves like variable But need array variable to access the entry To use or assign entry, specify index inside

brackets Example code snippet computing powers

of 2:

long lA[10];lA[0] = 1;for (long i=0; i < sizeof(lA)/sizeof(long);i++){ lA[i] = lA[i-1] * 2; cout << lA[i] << endl;}

Page 17: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Using An Array

Within an array, each entry behaves like variable But need array variable to access the entry To use or assign entry, specify index inside

brackets Example code snippet computing powers

of 2:

long lA[10];lA[0] = 1;for (int i=0; i < sizeof(lA)/sizeof(long);i++){ lA[i] = lA[i-1] * 2; cout << lA[i] << endl;}

Page 18: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Using An Array

Within an array, each entry behaves like variable But need array variable to access the entry To use or assign entry, specify index inside

brackets Example code snippet computing powers

of 2:const int BORED_NOW = 10;long lA[BORED_NOW];lA[0] = 1;for (int i=0; i < BORED_NOW;i++){ lA[i] = lA[i-1] * 2; cout << lA[i] << endl;}

Page 19: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Let's Trace This Code

int main() {const int BORED_NOW = 4;long loserArray[BORED_NOW];loserArray[0] = 1;for (int i=1; i < BORED_NOW; i++){ loserArray[i] = loserArray[i-1] * 2; cout << loserArray[i] << endl;}cout << "Sorry its stupid!" << endl;return 0;

}

Page 20: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

Your Turn

Get into your groups and try this assignment

Page 21: CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.

For Next Lecture

Read more about arrays in Section 10.5 How can we pass arrays as parameters? Can values be changed in the array no

matter what? Why couldn't they be consistent about

params? Weekly Assignment #8 out & due

Tuesday Avoid the rush by start working on it now

Programming Assignment #2 now on Angel Could start working on this now – know all

you need