computer science and programming - cs.duke.edu base = 2, expo = 4 find 2^17 find 2^8 find 2^4 find...

Post on 03-Apr-2018

230 Views

Category:

Documents

5 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Chapter 1

Computer Science andProgramming

1

2 CHAPTER 1. COMPUTER SCIENCE AND PROGRAMMING

10

10

5

5

Q

Q

7

7

9

9

A

A

8

8

3

3

7

7

K

K

8

8

Q

Q

J

J

Figure 1.1:

3

10

10

5

5

Q

Q

7

7

9

9

A

A

8

8

3

3

7

7

K

K

8

8

Q

Q

J

J

Figure 1.2:

4 CHAPTER 1. COMPUTER SCIENCE AND PROGRAMMING

Language

Architecture

Theory

Figure 1.3:

5

Efficiencyand

complexity

Conceptualand formal

models

Levelsofabstraction

Figure 1.4:

6 CHAPTER 1. COMPUTER SCIENCE AND PROGRAMMING

(3 + 5i ) * (2 - 7i )3.14 * 6.0231,285 * 573/4 * 8/9

ComplexRealIntegerRational

73,245 18.91222 41 - 11i2/3

Figure 1.5:

7

// pre: post:

7

8

int operator * (int x, int y)

1

2

5

010001100101001000011110010101

6

1,285 Y

call

mov 0xd, %o3

3

4

x Z

}

8

x 57

{

73,245

mov 0xd,%o2

set L8,%o1

ld [%fp+-0x8],%o0

73,245

Figure 1.6:

8 CHAPTER 1. COMPUTER SCIENCE AND PROGRAMMING

Rover

Figure 1.7:

9

Figure 1.8:

10 CHAPTER 1. COMPUTER SCIENCE AND PROGRAMMING

Figure 1.9:

Chapter 2

C++ Programs: Form andFunction

11

12 CHAPTER 2. C++ PROGRAMS: FORM AND FUNCTION

<return type>function name (parameter list){

...

}

int main(){

...

}

#include <iostream>

// author: Owen Astrachan, 02/22/99// traditional first program

#include statement(s)

C++ statement 0; C++ statement 1;

C++ statement (n-1) ;

C++ statement (n-1);

C++ statement 1; C++ statement 0;

using namespace std;

void Hello()

cout << "Hello World" << endl;

{

}

int main(){ Hello();

}

return 0;

2

3

4

1

Figure 2.1:

13

{

cout << "Happy birthday to you" << endl;

cout << "Happy birthday to you" << endl;

cout << endl;

}

int main()

{

Sing("Grace");

Sing("Alan");

...

}

cout << "Happy birthday to you" << endl;

cout << "Happy birthday dear " << person << endl;

"Grace"

void Sing(string person)

Figure 2.2:

14 CHAPTER 2. C++ PROGRAMS: FORM AND FUNCTION

{

cout << "and on his farm he had a " << animal << ", ";

EiEiO();

}

{

...

}

void Pig()

{

Refrain();

HadA("pig");

WithA("oink");

Refrain();

}

"pig"

"oink"

cout << "With a " << noise << " " << noise << " here" << endl;

void WithA(string noise)

void HadA(string animal)

Figure 2.3:

15

void

{

Refrain()

Had(animal);

WithA(noise);

Refrain();

}

int main()

{

Verse("pig","oink");

...

}

"pig" "oink"

Verse(string animal, string noise)

Figure 2.4:

16 CHAPTER 2. C++ PROGRAMS: FORM AND FUNCTION

Chapter 3

Program Design andImplementation

17

18 CHAPTER 3. PROGRAM DESIGN AND IMPLEMENTATION

int main()

string animal;

string noise;

cout << "Enter noise that a "<< animal <<" makes ";

cout << "Enter the name of an animal ";

cin >> noise;

cin >> animal;

cout << endl;

Verse (animal,noise);

return 0;

}

"COW"

"MOO"

int main()

string animal;

string noise;

cout << "Enter noise that a "<< animal <<" makes ";

cout << "Enter the name of an animal ";

cin >> noise;

cin >> animal;

cout << endl;

Verse (animal,noise);

return 0;

}

"COW"

"MOO"

1

2

3

Figure 3.1:

19

Memorylocation

cow

animal memory location/variableName of

Value of variable (type string)

Figure 3.2:

20 CHAPTER 3. PROGRAM DESIGN AND IMPLEMENTATION

47 1347

28

1316

31

1347/47

1347 % 47

Figure 3.3:

21

(40 - 32) * 5/9 (40.0 - 32.0) * 5/9

8 * 5/9

40/9

4

8.0 * 5/9

40.0/9

4.44444

Figure 3.4:

22 CHAPTER 3. PROGRAM DESIGN AND IMPLEMENTATION

void SlicePrice(int radius, double price)

{

...

...

cout << "$" < 3.14159*radius*radius/price

...

}

int main()

{

int radius;

double price;

...

SlicePrice(radius,price);

}

8

11.95

8 11.95

Figure 3.5:

23

Figure 3.6:

24 CHAPTER 3. PROGRAM DESIGN AND IMPLEMENTATION

Ascend

Rise

Cruise

Balloon

gballoon.h

Member functions

Header file

Class name

GetLocation

GetAltitude

Figure 3.7:

25

executable program

client program

class implementation

COMPILE

object code

object code

LINK

gfly

int main(){ Balloon b; b.Ascend(30); return 0;}

#include "gballoon.h"gfly.cpp

void Balloon::Ascend(int height){ cout << endl; cout << "***** (Height = "; cout << myAltitude << ")";

...

gballoon.cpp#include "gballoon.h" gballoon.o

110101110101010101011110111100001...

0101010011101010101101010110101...

gfly.o

Figure 3.8:

26 CHAPTER 3. PROGRAM DESIGN AND IMPLEMENTATION

Figure 3.9:

Chapter 4

Control, Functions, andClasses

27

28 CHAPTER 4. CONTROL, FUNCTIONS, AND CLASSES

int amount;

87

Before execution

amount = amount - quarters*25;

int quarters;

After execution

int amount;

3

12

87 - 3*25

Figure 4.1:

29

}

}

...

...

double

{

double value;

cin >> value;

cout << ... << sqrt(value) << endl;

cout << ... << 10.7238 << endl;

cin >> value;

sqrt(double x) 115.0

115.0Calling function sqrt(115.0)

Returning 10.7238 from sqrt(115.0)

}

return root;

// code to give root a value

double root;

Figure 4.2:

30 CHAPTER 4. CONTROL, FUNCTIONS, AND CLASSES

Chapter 5

Iteration with Programsand Classes

31

32 CHAPTER 5. ITERATION WITH PROGRAMS AND CLASSES

next statement;

statement list;

test test

statement list;

next statement;

while (test){ statement list;}next statement;

statement list;

if (test){

}next statement;

true

false

true

false

Figure 5.1:

33

Product

Count 0 1 2 3 4 5

1 1

6

1202462 720

Figure 5.2:

34 CHAPTER 5. ITERATION WITH PROGRAMS AND CLASSES

Figure 5.3:

35

int limit = sqrt(n) + 1;

int divisor = 3;

while (divisor <= limit)

{

if (n % divisor == 0)

{

return false;

}

divisor += 2;

}

k = low;

while (k <= high)

{

if (IsPrime(k))

{

cout << k << endl;

numPrimes += 1;

}

k += 1;

}

Test

Initialization

Update

Body

Figure 5.4:

36 CHAPTER 5. ITERATION WITH PROGRAMS AND CLASSES

Figure 5.5:

Chapter 6

Classes, Iterators, andPatterns

37

38 CHAPTER 6. CLASSES, ITERATORS, AND PATTERNS

Dice (int sides)

int Roll()

int NumSides () const

int NumRolls () const

myRollCount

mySides

Pub

licP

riva

te

Beh

avio

rS

tate0

6

Dice (int sides)

int Roll()

int NumSides () const

int NumRolls () const

myRollCount

mySides

Pub

licP

riva

te

Beh

avio

rS

tate0

cube, after Dice cube(6); dodeca , after Dice dodeca (12);

12

Figure 6.1:

39

Owen 6

Owen

GiveQuiz(student, correctCount, total);

void GiveQuiz(string name, int & correct, int & total)

10

Function prototype/header, formal parameters

int correctCount,total; string student;

Function call, arguments

Figure 6.2:

40 CHAPTER 6. CLASSES, ITERATORS, AND PATTERNS

Figure 6.3:

Chapter 7

Class Interfaces, Design,and Implementation

41

42CHAPTER 7. CLASS INTERFACES, DESIGN, AND IMPLEMENTATION

0

Figure 7.1:

43ste

p siz

e

a

Y

X

cos(a) = X /step sizesin(a) = Y/step size

Figure 7.2:

44CHAPTER 7. CLASS INTERFACES, DESIGN, AND IMPLEMENTATION

–40

–35

–30

–25

–20

–15

–10

–5

0

5

–25 –20 –15 –10 –5 0 5 10 15 20

Brownian motion: 1,024 steps of unit length

Figure 7.3:

45

–5

0

5

10

15

20

25

30

35

40

–10 –5 0 5 10 15 20 25 30

Brownian motion: 1,024 steps of unit length

Figure 7.4:

46CHAPTER 7. CLASS INTERFACES, DESIGN, AND IMPLEMENTATION

(0, 1)

(1, 0)

Figure 7.5:

47

distance = 3d

Fig

ure

7.6:

48CHAPTER 7. CLASS INTERFACES, DESIGN, AND IMPLEMENTATION

Chapter 8

Arrays, Data, and RandomAccess

49

50 CHAPTER 8. ARRAYS, DATA, AND RANDOM ACCESS

Track 3

114 115 116 117

114

hello world

Figure 8.1:

51

0 1 2 3 4 5 6

0 1 2 3 4

Numbers

Words

Figure 8.2:

52 CHAPTER 8. ARRAYS, DATA, AND RANDOM ACCESS

tvector<int> diceStats(2*DICE_SIDES+1);

Type of variable Variable identifier

Dice d(6);

Argument passed to constructor

Figure 8.3:

53

switch (sum){ case 2: twos += 1; break; case 3: threes += 1; break; case 4: fours += 1; break; case 5: fives += 1; break; case 6: sixes += 1; break; case 7:

break; case 8: eights += 1; break;}

sevens += 1;

diceStats[sum]++;

0 1 2 3 4 5 6 7 8

Figure 8.4:

54 CHAPTER 8. ARRAYS, DATA, AND RANDOM ACCESS

myCount

myCount

80 1 2 3 4 5 6 7

80 1 2 3 4 5 6 7

80 1 2 3 4 5 6 7

80 1 2 3 4 5 6 7

myCount

myCount

DAdd to vector maintained in sorted order

Original list

loc = 8

loc = 6

loc = 4

loc = 2

B C F M Q S T V

B C F M Q

B C F

B C F F

M Q

M Q S T

Q S T

V

V

S T T V

Figure 8.5:

55

one guess

two guesses

three guesses

four guesses

five guesses

six guesses

Sequential search Binary search

(low, high, high, low)

Figure 8.6:

56 CHAPTER 8. ARRAYS, DATA, AND RANDOM ACCESS

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

Figure 8.7:

Chapter 9

Strings, Streams, andOperators

57

58 CHAPTER 9. STRINGS, STREAMS, AND OPERATORS

wordCount++

inWord ! inWord! isspace(ch)

isspace(ch)

Figure 9.1:

59

FIRST_SLASHTEXT

Echo(ch)

COMMENT

ch != '/'

ch == '/'

ch != '/'

ch == '/'

ch != '\n'

ch == '\n'

Echo('\n')

Echo('/')Echo(ch)

Figure 9.2:

60 CHAPTER 9. STRINGS, STREAMS, AND OPERATORS

Figure 9.3:

Chapter 10

Recursion, Lists, andMatrices

61

62 CHAPTER 10. RECURSION, LISTS, AND MATRICES

number number1478 147

Print(1478)

From main( )

PrintDigit(8);

PrintDigit(4);

Print(1)

Print(147)

Print(14)

PrintDigit(7);

PrintDigit(1);

number number 114

Figure 10.1:

63

base = 2, expo = 4

find 2^17 find 2^8 find 2^4

find 2^2 find 2^1 find 2^0

return 2*result*resultreturn 2*result*result return result*result

return result*result return 2*result*result return 1

base = 2, expo = 1 base = 2, expo = 0

base = 2, expo = 17 base = 2, expo = 8

base = 2, expo = 2Return 4 Return 2 Return 1

Return 16Return 256

int result = Power(2,35);base = 2, expo = 35

Return 34359738368 Return 131072

return result*result

Figure 10.2:

64 CHAPTER 10. RECURSION, LISTS, AND MATRICES

code

chapter2 chapter4 chapter3prompt.h dice.h

pizza.cpppts.cpphello.cpp bday.cpp isleap.cpp fly.cpp fahr.cpp gfly.cpp

Figure 10.3:

65

chap1 chap2 chap3

tapestry

finalbigpic.epschapter1.tex

hello.cpp bday.cpp oldmac.cpp

oldmac.eps

progs progs

gfly.cpp pizza.cpp macinput.cpp

library

prompt.hdice.h dice.cpp

book.tex

chapter2.tex chapter3.tex

Figure 10.4:

66 CHAPTER 10. RECURSION, LISTS, AND MATRICES

"tapestry"

0

path

tabCount

1num

ProcessDir("tapestry\chap1",1)

ProcessDir("tapestry\chap3",1)

ProcessDir("tapestry\library",1)

num 5

num 2

num 3

ProcessDir("tapestry",0);

ProcessDir("tapestry\chap2",1)

ProcessDir("tapestry\chap3\progs",2)

2

path

tabCount

num

"tapestry\chap3\progs"

2

path

tabCount

num

"tapestry\chap2\progs"

path

tabCount

num

"tapestry\chap3"

1

path

tabCount

num

1

"tapestry\chap1"

"tapestry\chap2"

1

2

path

tabCount

num

0

0

0

0

0

path

tabCount

num

"tapestry\library"

1

ProcessDir("tapestry\chap2\progs",2)

Figure 10.5:

67

num 5 4num num 3

num 2num 1num 0

RecFactorial(5)

RecFactorial(4) RecFactorial(3)

RecFactorial(1)RecFactrial(0)

RecFactorial(2)

Return 5 * ___ = 120Return 4 * ___ = 24 Return 3 * ___ = 6

Return 1 * ___ = 1

Return 2 * ___ = 2

Return 1

Figure 10.6:

68 CHAPTER 10. RECURSION, LISTS, AND MATRICES

6

5 4

1

2 1 1 0 01 01

0

2 2 1 2 1 1 03

3 3 24

RecFib(6)

Figure 10.7:

69

main

int first,second;

while

if

int second;

int first;

Figure 10.8:

70 CHAPTER 10. RECURSION, LISTS, AND MATRICES

s2s1 s4s3

"tomato" "celery""carrot"

s5

empty

Tail( )

Head( )

"peapod"

Figure 10.9:

71

1 2 3

��

���

���

��

��

���

��

��

��

��

Figure 10.10:

72 CHAPTER 10. RECURSION, LISTS, AND MATRICES

0

0

0

1

1

1

1

0

0

1

0

1 0

0

0

1

1

1

1

1

1

0

0

0

0

0

0

0

0

0

0

0

0

Figure 10.11:

73

28

28

10

10

25 32

12 28

25

32

32 32 32

2525

25 10

1010

10 12 12

12

25 25

32 32

10

18

18

18

10

32 32

3-neighborhood 5-neighborhood

Figure 10.12:

74 CHAPTER 10. RECURSION, LISTS, AND MATRICES

Figure 10.13:

75

Figure 10.14:

76 CHAPTER 10. RECURSION, LISTS, AND MATRICES

Chapter 11

Sorting, Templates, andGeneric Programming

77

78CHAPTER 11. SORTING, TEMPLATES, AND GENERIC PROGRAMMING

4223 18 7 57 38

4218 23 57 38

MinIndex = 3 Swap(a[0],a[3]);

MinIndex = 1 Swap(a[1],a[1]);

MinIndex = 3 Swap(a[2],a[3]);

MinIndex = 5 Swap(a[3],a[5]);

57 MinIndex = 5 Swap(a[4],a[5]);

42

0 1 2 3 4 5

0 1 2 3 4 5

0 1 2 3 4 5

0 1 2 3 4 5

0 1 2 3 4 5

0 1 2 3 4 5

7

4218 23 57 387

2318 42 57 387

2318 38 427

2318 38 577

Figure 11.1:

79

0 1 2 3 4 5

0 1 2 3 4 5

0 1 2 3 4 5

0 1 2 3 4 5

0 1 2 3 4 5

0 1 2 3 4 5

0 1 2 3 4 5

loc = 4

loc = 0

loc = 2

loc = 1

loc = 0

loc = 3

hold = 18

hold = 23

hold = 42

hold = 7

hold = 57

hold = 38

Original vector

18 42 7 57 38

3823 57

2318

18 23 42 57

18 23 42

7 57 384223

421823 7 57 38

18 7

42 57 38

7

7

Figure 11.2:

80CHAPTER 11. SORTING, TEMPLATES, AND GENERIC PROGRAMMING

0

2

4

6

8

10

12

14

16

1000 2000 3000 4000 5000 6000 7000 8000 9000 10000

Tim

e (s

econ

ds)

Number of elements

"Insertion""Selection"

"Bubble"

Figure 11.3:

81

2527

18

20

19

2531

33

27

Pivot

Pivot

3129

33

2722

25

25

24

20 18

22

2429

31

29

332525

22

24

18

2019

31

31

1931

Figure 11.4:

82CHAPTER 11. SORTING, TEMPLATES, AND GENERIC PROGRAMMING

if (a[k] <= piv){ p++; Swap(a[k],a[p])}

elements <= X elements >= XX

Pivot

Pivot

First

First

First

First Last

Last

Last

Last

X

X

<= X ???

<= X

> X

> X

> X<= X

kp

p

X

Desired properties of vector, partitioned around pivot

After several iterations, partially re-arranged, more elements to process

All elements processed

Final configuration after swapping first element into pivot location

}Swap(a[p],a[first]);

return p;

Figure 11.5:

83

nonzeros examined ?????

nonZeroIndex k

Figure 11.6:

top related