Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays Ethan Cerami New York University 1998. Today n Array Basics (Review) n Random Number Example n Passing Arrays to Functions n Strings.

Similar presentations


Presentation on theme: "Arrays Ethan Cerami New York University 1998. Today n Array Basics (Review) n Random Number Example n Passing Arrays to Functions n Strings."— Presentation transcript:

1 Arrays Ethan Cerami New York University 1998

2 Today n Array Basics (Review) n Random Number Example n Passing Arrays to Functions n Strings

3 Array Basics n What’s an Array? – –a group of related data. – –all data must share the same data type. n n Examples: – –int temp[5]; – –float stock[5];

4 Initializing Arrays: 3 Options n 1) You know the data ahead of time: n 2) Initialize all data to 0: n 3) Let compiler determine size of array: int temp[5] = {45, 47, 44, 56, 49}; int temp[5] = {0}; int temp[] = {45, 47, 44, 56};

5 Referencing Array Elements n n var_name [index]; n n index always starts at 0 n n ends at N-1 n n C does not provide any array bounds checking. int temp[25] = {0}; int x = temp[100]; /* This will compile, but it is a bug */ /* It is outside the 0..24 range

6 Random Number Example n Program simulates the rolling of a single die 6,000 times. n Reports statistical results. Face Frequency 1 990 2 1041 3 993 4 944 5 1035 6 997

7 Step through Program n First, create an array of integers, size=7; initialize array to 0. –int face, roll, frequency[SIZE] = {0}; n Initialize the random number generator: –srand(time(NULL));

8 Step through Program for (roll = 1; roll <= 6000; roll++) { face = rand() % 6 + 1; ++frequency[face]; } Suppose we roll a 5: face = 5; ++frequency[5]; 0 1 2 3 4 5 6 0000010 This bucket is not ever used, because we are only tracking 1-6.

9 Passing Variables to Functions #include void test (int); main () { int x = 5; printf ("In main x: %d\n", x); test (x); printf ("In main x: %d\n", x); } void test (int x) { x += 10; printf ("In Test x: %d\n", x); } Output: In main x: 5 In Test x: 15 In main x: 5 When you increment x within test, it does not affect the x within main().

10 Call by Value n Call by Value: – –When you pass a variable, you pass a "copy" of the variable. – –Changes to the copy variable do not affect the original variable.

11 Call by Reference n n Call by Reference: – –When you pass a variable, you pass a reference to the original variable. – –Changes to the reference do affect the original variable. n n Arrays are passed via call by reference.

12 Temperature Example Temperature Example n Program creates an array of temperature values. n The function makeHot() receives an array of temperature values, and increases each temperature by 10 degrees.

13 The makeHot Function void makeHot (int thermo[], int size) { int i; printf ("Making hot!\n"); for (i=0; i<SIZE; i++)thermo[i] += 10; } n Function Prototypes/Definitions –To specify an array parameter, indicate it with brackets. –No need to include size in brackets. (In fact, size is ignored.) –Specify the size of the array as a separate value.

14 Program Output n The Big Picture: makeHot() changes the values of the temp array. Since arrays are passed by reference, the changes do affect the original variable. Output: 75 65 89 72 Making hot! 85 75 99 82

15 Call by Value v. Call by Reference n Call by Value: – –When you pass a variable, you pass a "copy" of the variable. – –Changes to the copy variable do not affect the original variable. n Call by Reference: – –When you pass a variable, you pass a reference to the original variable. – –Changes to the reference do affect the original variable.

16 Strings n Creating Strings: –A string is an array of char variables. char name[] = "ETHAN"; This will create the following array of 6 characters: 0 1 2 3 4 5 ETHNA\0 This is the NULL terminator. Indicates the end of a string.

17 Creating/Using Strings char name[] = {'E', 't', 'h', 'a', 'n', '\0'}; You can also reference individual elements within a string: printf ("%c", name[2]) This will print: H char name[] = "ETHAN"; has the same functionality as:

18 Inputting Strings n To input string, use scanf() or gets() – –scanf ("%s", name); n n Scanf reads in character data until the first white space character. n n Whitespace = space, tab, new line character. Note: There is no & needed when reading in strings.

19 Inputting Strings (Cont) #include main () { char name[255]; printf ("Enter your name: "); scanf ("%s", name); printf ("Hi %s!", name); getche(); }

20 Inputting Strings (Cont.) n n gets: reads in character data until the new line character. – –It therefore reads in spaces, tabs, etc. –gets (name);

21 Comparing Strings n n strcmp: takes two strings, returns 0 if they are equal. #include main () { char password[255]; printf ("Enter password: "); scanf ("%s", password); if (strcmp (password, "bluemoon")== 0) printf ("Welcome!\n"); else printf ("Access Denied.\n"); getche(); }


Download ppt "Arrays Ethan Cerami New York University 1998. Today n Array Basics (Review) n Random Number Example n Passing Arrays to Functions n Strings."

Similar presentations


Ads by Google