Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 107 - Programming for Science Lecture 26: Arrays.

Similar presentations


Presentation on theme: "CSC 107 - Programming for Science Lecture 26: Arrays."— Presentation transcript:

1 CSC 107 - Programming for Science Lecture 26: Arrays

2 Problem of the Day Two English words change their pronunciation when their first letter is capitalized. What are they? Polish/polish Reading/reading

3 Today’s Goal After lecture should be familiar with 1- dimensional arrays  Creating an array variable  Assigning data inside the array  Using the values stored in the array Arrays in functions on Wednesday Higher dimension arrays on Friday

4 Variables Names memory location for program’s use  Initial value is unknown  Value updated via assignments  Value used whenever program uses variable (Should be) Declared within a function Only usable by the declaring function

5 Data Types Each variable also has data type  Specifies how program treats variable’s value 6(+1) numeric types exist  Integer types: short, int, long  Decimal types: float, double, long double  char holds character, but these are numbers C will warn on bad assignments  Assignments that may lose information

6 Problem with Variables Each variable stores single value  Assignment overwrites prior value Storing 2 values at once needs 2 variables Storing 100 values needs 100 variables Images usually contain millions of values  Good luck with all that typing!

7 Arrays Variable holding multiple pieces of data  Really contains many different locations  Locations are numbered sequentially from 0 Each location is like an individual variable  Initial value is unknown  New value set at assignment But location is not independent of array  Can only be used by accessing array variable

8 Declaring an Array Variable Must declare array variable before use Declaration must include type, name, and size  Size should be literal value  Name follows same rules as any other variable  Variable is an array of the requested type  Each location, however, holds data of that type int sampleArray[10]; float armada[200]; FILE* inputs[3];

9 Working With Arrays Only creates location between 0 & size-1 C does not make this process easy  Cannot find what an array’s size is  Will not give you any sort of warning If you do make an illegal access…  Program may crash with “Segmentation Fault”  May see problems with other variable  May not notice any problems  (Major source of computer hacking attempts)

10 Initializing an Array Can set locations’ initial values Must specify value for all locations  Can initialize all locations to same value…  …or can initialize each location separately double taxrate[3]={0.15, 0.25, 0.3}; char list[5]={‘h’,‘e’,‘l’,‘l’,‘o’}; int vector[100]={0}; /*all set to 0*/ int s[]={5,0,-5}; /*a’s size is 3*/

11 Using An Array Each array location used like a variable  But can access only through array variable  Locations are accessed using ‘[’ & ‘]’ Example computing Fibonacci numbers: int i, fib[20]; fib[0] = 1; fib[1] = 1; for (i = 2; i < 20; i++) { fib[i] = fib[i – 1] + fib[i – 2]; }

12 More Uses of An Array #define MAX_SIZE 80 char name[MAX_SIZE]; int inLoc, outLoc; printf(“What is your name? ”); for (inLoc = 0; inLoc < MAX_SIZE; inLoc++) { name[inLoc] = getchar(); if (name[inLoc] == ‘\n’) { break; } } printf(“Hello ”); for (outLoc = 0; outLoc < inLoc; outLoc++) { printf(“%c”, name[outLoc]); }

13 Your Turn Get back into groups and complete the daily activity

14 For Next Lecture Work on weekly assignment #11 Continue on Programming Assignment #2 Keep up on your reading  Do not need to understand all the details  But important knowing what is not understood


Download ppt "CSC 107 - Programming for Science Lecture 26: Arrays."

Similar presentations


Ads by Google