220 likes | 320 Views
Arrays. Ethan Cerami New York University 1998. Today. Array Basics (Review) Random Number Example Passing Arrays to Functions Strings. Array Basics. What’s an Array? a group of related data. all data must share the same data type. Examples: int temp[5]; float stock[5];.
E N D
Arrays Ethan Cerami New York University 1998
Today • Array Basics (Review) • Random Number Example • Passing Arrays to Functions • Strings
Array Basics • What’s an Array? • a group of related data. • all data must share the same data type. • Examples: • int temp[5]; • float stock[5];
Initializing Arrays: 3 Options • 1) You know the data ahead of time: • 2) Initialize all data to 0: • 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};
Referencing Array Elements • var_name [index]; • index always starts at 0 • ends at N-1 • 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
Random Number Example • Program simulates the rolling of a single die 6,000 times. • Reports statistical results. Face Frequency 1 990 2 1041 3 993 4 944 5 1035 6 997
Step through Program • First, create an array of integers, size=7; initialize array to 0. • int face, roll, frequency[SIZE] = {0}; • Initialize the random number generator: • srand(time(NULL));
Step through Program for (roll = 1; roll <= 6000; roll++) { face = rand() % 6 + 1; ++frequency[face]; } This bucket is not ever used, because we are only tracking 1-6. Suppose we roll a 5: face = 5; ++frequency[5]; 0 0 0 0 0 1 0 0 1 2 3 4 5 6
Passing Variables to Functions 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(). #include <stdio.h> 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); }
Call by Value • 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.
Call by Reference • 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. • Arrays are passed via call by reference.
Temperature Example • Program creates an array of temperature values. • The function makeHot() receives an array of temperature values, and increases each temperature by 10 degrees.
The makeHot Function void makeHot (int thermo[], int size) { int i; printf ("Making hot!\n"); for (i=0; i<SIZE; i++) thermo[i] += 10; } • 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.
ProgramOutput • 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
Call by Value v. Call by Reference • 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. • 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.
Strings • Creating Strings: • A string is an array of char variables. char name[] = "ETHAN"; This will create the following array of 6 characters: E T H A N \0 This is the NULL terminator. Indicates the end of a string. 0 1 2 3 4 5
Creating/Using Strings char name[] = "ETHAN"; has the same functionality as: 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
Inputting Strings • To input string, use scanf() or gets() • scanf ("%s", name); • Scanf reads in character data until the first white space character. • Whitespace = space, tab, new line character. Note: There is no & needed when reading in strings.
Inputting Strings (Cont) #include <stdio.h> #include <conio.h> main () { char name[255]; printf ("Enter your name: "); scanf ("%s", name); printf ("Hi %s!", name); getche(); }
Inputting Strings (Cont.) • gets: reads in character data until the new line character. • It therefore reads in spaces, tabs, etc. • gets (name);
Comparing Strings • strcmp: takes two strings, returns 0 if they are equal. #include <stdio.h> #include <string.h> #include <conio.h> main () { char password[255]; printf ("Enter password: "); scanf ("%s", password); if (strcmp (password, "bluemoon")== 0) printf ("Welcome!\n"); else printf ("Access Denied.\n"); getche(); }