1 / 20

Lecture 20: Arrays

CSC 107 – Programming For Science. Lecture 20: Arrays. Today’s Goal. Become familiar with simple arrays Declaring an array variable Assigning data to array entries Using values stored in an array Know connection between arrays and loops. Variables.

knut
Download Presentation

Lecture 20: 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. CSC 107 – Programming For Science Lecture 20: Arrays

  2. Today’s Goal • Become familiar with simple arrays • Declaring an array variable • Assigning data to array entries • Using values stored in an array • Know connection between arrays and loops

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

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

  5. Could Have Spent Time

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

  7. Declaring Array Variables • Like all variables, must declare before use • Type, name, & sizeneeded 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

  8. Declaring Array Variables • Like all variables, must declare before use • Type, name, & sizeneeded 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

  9. 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, & sizeneeded for array declaration intplanetsWeight[NOT_PLUTO];float armada[MAX_SHIPS];double annualIncomes[MAX_LIFETIME];char responses[17];long timeToWait[35];

  10. Initializing an Array • Can set initial values when declaring array • Will need to specify value for every entry in array • Initialize all entries to same value, if desired • Or the value of each entry specified separately • Additional way of specifying size also provided double taxrate[LEVELS] ={0.15, 0.25, 0.3};intvector[100]={0}; // all 100 entries set to 0integer[]={5,0,-5};// eger's size will be 3

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

  12. 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 as1 • Stupidity defense legal, if array declared with size 0

  13. 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 indices like -1 or 1029374729192 that stupid • 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

  14. Using An Array • Each array entry behaves like variable • Accessed via array variable is only difference • To use or assign entry, specify index inside brackets • Example code snippet computing powers of 2: long loserArray[10];loserArray[0] = 1;for (long i=0; i < sizeof(loserArray);i++){loserArray[i] = loserArray[i-1] * 2;cout << loserArray[i] << endl;}

  15. Using An Array • Each array entry behaves like variable • Accessed via array variable is only difference • To use or assign entry, specify index inside brackets • Example code snippet computing powers of 2: long loserArray[10];loserArray[0] = 1;for (longi=0; i < sizeof(loserArray);i++){loserArray[i] = loserArray[i-1] * 2;cout << loserArray[i] << endl;}

  16. Using An Array • Each array entry behaves like variable • Accessed via array variable is only difference • To use or assign entry, specify index inside brackets • Example code snippet computing powers of 2: long loserArray[10];loserArray[0] = 1;for (inti=0; i < sizeof(loserArray); i++){loserArray[i] = loserArray[i-1] * 2;cout << loserArray[i] << endl;}

  17. Using An Array • Each array entry behaves like variable • Accessed via array variable is only difference • To use or assign entry, specify index inside brackets • Example code snippet computing powers of 2: const int BORED_NOW = 10;long loserArray[BORED_NOW];loserArray[0] = 1;for (inti=0; i < BORED_NOW; i++){loserArray[i] = loserArray[i-1] * 2;cout << loserArray[i] << endl;}

  18. Let's Trace This Code int main() {const int BORED_NOW = 4;long loserArray[BORED_NOW];loserArray[0] = 1;for (inti=1; i < BORED_NOW; i++){loserArray[i] = loserArray[i-1] * 2;cout << loserArray[i] << endl;}cout << "Sorry its stupid!" << endl;return 0; }

  19. Your Turn • Get into your groups and try this assignment

  20. For Next Lecture • Read 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 #7 out & due Wednesday • Avoid the rush by start working on it now • Programming Assignment #2 now on Angel • This is a larger assignment; start it now!

More Related