21
Arrays Arrays Ethan Cerami Ethan Cerami New York University New York University 1998 1998

Arrays

  • Upload
    frye

  • View
    19

  • Download
    0

Embed Size (px)

DESCRIPTION

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];. - PowerPoint PPT Presentation

Citation preview

Page 1: Arrays

ArraysArraysEthan CeramiEthan Cerami

New York UniversityNew York University

19981998

Page 2: Arrays

TodayToday

Array Basics (Review)Array Basics (Review) Random Number ExampleRandom Number Example Passing Arrays to FunctionsPassing Arrays to Functions StringsStrings

Page 3: Arrays

Array BasicsArray Basics

What’s an Array?What’s an Array?– a group of related data.– all data must share the same data

type. Examples:

– int temp[5];– float stock[5];

Page 4: Arrays

Initializing Arrays: 3 Initializing Arrays: 3 OptionsOptions

1) You know the data ahead of 1) You know the data ahead of time:time:

2) Initialize all data to 0:2) Initialize all data to 0:

3) Let compiler determine size of 3) Let compiler determine size of array:array:

int temp[5] = {45, 47, 44, 56, 49};

int temp[5] = {0};

int temp[] = {45, 47, 44, 56};

Page 5: Arrays

Referencing Array Referencing Array ElementsElements

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

Page 6: Arrays

Random Number Random Number ExampleExample

Program simulates the rolling of a Program simulates the rolling of a single die 6,000 times.single die 6,000 times.

Reports statistical results.Reports statistical results.

Face Frequency 1 990 2 1041 3 993 4 944 5 1035 6 997

Page 7: Arrays

Step through ProgramStep through Program

First, create an array of integers, First, create an array of integers, size=7; initialize array to 0.size=7; initialize array to 0.– int face, roll, frequency[SIZE] = {0};int face, roll, frequency[SIZE] = {0};

Initialize the random number Initialize the random number generator:generator:– srand(time(NULL));srand(time(NULL));

Page 8: Arrays

Step through ProgramStep 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

0 0 0 00 1 0

This bucket is not ever used, because we are only tracking 1-6.

Page 9: Arrays

Passing Variables to Passing Variables to FunctionsFunctions

#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);

}

Output:In main x: 5In Test x: 15In main x: 5

When you increment x within test, it does not affect the x within main().

Page 10: Arrays

Call by ValueCall by Value

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.

Page 11: Arrays

Call by ReferenceCall 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.

Page 12: Arrays

Temperature ExampleTemperature Example

Program creates an array of Program creates an array of temperature values.temperature values.

The function makeHot() receives The function makeHot() receives an array of temperature values, an array of temperature values, and increases each temperature and increases each temperature by 10 degrees.by 10 degrees.

Page 13: Arrays

The makeHot FunctionThe 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.

Page 14: Arrays

ProgramProgram OutputOutput

The Big Picture: makeHot() The Big Picture: makeHot() changes the values of the temp changes the values of the temp array. Since arrays are passed by array. Since arrays are passed by reference, the changes reference, the changes dodo affect affect the original variable.the original variable.

Output:

75 65 89 72Making hot! 85 75 99 82

Page 15: Arrays

Call by Value v. Call by Call by Value v. Call by ReferenceReference

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.

Page 16: Arrays

StringsStrings

Creating Strings:Creating Strings:– A string is an array of char variables.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

E T H NA \0 This is the NULL terminator. Indicates the end of a string.

Page 17: Arrays

Creating/Using StringsCreating/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:

Page 18: Arrays

Inputting StringsInputting Strings

To input string, use scanf() or To input string, use scanf() or gets()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.

Page 19: Arrays

Inputting Strings (Cont)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();

}

Page 20: Arrays

Inputting Strings Inputting Strings (Cont.)(Cont.)

gets: reads in character data until the new line character. – It therefore reads in spaces, tabs,

etc.– gets (name);gets (name);

Page 21: Arrays

Comparing StringsComparing 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();

}