monday, jan 20, 2002kate gregory with material from deitel and deitel week 3 questions from last...

25
Monday, Jan 20, 2002 Kate Gregory with material from Deitel and Deitel Week 3 • Questions from Last Week • Hand in Lab 1 • Arrays • Pointers • Strings • Lab 2

Upload: christiana-wilshire

Post on 31-Mar-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Week 3

• Questions from Last Week

• Hand in Lab 1

• Arrays

• Pointers

• Strings

• Lab 2

Page 2: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Date Week Topic Chapter Hand Out Due Back Test11-Jan-02 1 Administrivia / Overview / Intro to C++ 118-Jan-02 2 Control Structures, Functions 2,3 Lab 125-Jan-02 3 Arrays, Pointers, Strings 4,5 Lab 2 Lab 1 5%01-Feb-02 4 Classes, Data Abstraction 6 Lab 2 5%08-Feb-02 5 More on Classes 7 Lab 315-Feb-02 6 Operator Overloading 8 Lab 4 Lab 3 5%22-Feb-02 Reading Break01-Mar-02 7 Inheritance 9 Lab 4 5% Midterm 25%08-Mar-02 8 Virtual Functions and Polymorphism 10 Lab 515-Mar-02 9 Stream IO 11 Lab 6 Lab 5 5%22-Mar-02 10 Templates 12 Lab 7 Lab 6 5%29-Mar-02 11 Exceptions 13 Lab 7 5%05-Apr-02 12 File IO 14

??? Exam Final 40%

Page 3: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Arrays

• A collection of items, all the same type, that can be treated as a unit or one at a time

• 10 integers

• 5 floating point numbers

• 100 characters

Page 4: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Memory arrangement

c[6]

-45

6

0

72

1543

-89

0

62

-3

1

6453

78

c[0]

c[1]

c[2]

c[3]

c[11]

c[10]

c[9]

c[8]

c[7]

c[5]

c[4]

Page 5: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Declaring Arrays

int scores[10];

char letters[26];

float ratios[5];

• Size is fixed

Page 6: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Using Array Elements

scores[i]++;

cout << letters[4];

ratio[2] = 4.7;

Page 7: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

No bounds checking in C++

int scores[10];

for (int i=0; i<20; i++)

{

scores[i] = 100;

}

• You’re the programmer!

Page 8: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Bug prevention

#define MAXSCORES 10int scores[MAXSCORES];for (int i=0; i< MAXSCORES; i++){ scores[i] = 100;}

• More readable, less error-prone• Can’t use a variable when declaring the array

Page 9: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Initializers

int n[ 5 ] = { 1, 2, 3, 4, 5 };

• Or just

int n[] = { 1, 2, 3, 4, 5 };

Page 10: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Passing arrays to functions

int total(int[] a);

// ...

int numbers[] = {1,2,3,4,5};

int x = total(numbers);

Page 11: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Taking arrays in functionsint total(int[] a){ int tot = 0; int limit = sizeof(a) / sizeof(int); for (int i=0; i<limit; i++) { tot += a[i]; } return tot;}

Page 12: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Pointers

• Address of a variable in memory

• They have a type

int* p;

char* s;

Page 13: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

& operator

& (address operator) gets address of operandint y = 5;

int *yPtr;

yPtr = &y; // yPtr gets address of y

yPtr

y5

yptr

500000 600000

y

600000 5

Address of y is value of yptr

Page 14: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

* operator

• * (indirection/dereferencing operator)– Gives access to what the pointer points to– *yptr returns y (because yptr points to y)– * can be used for assignment

*yptr = 7; // changes y to 7

• * and & are inverses – They cancel each other out

Page 15: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Pointers and Arrays

int scores[10];

x = scores[3];

x++;

y = scores;

y++;

Page 16: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Pointers and Arrays

scores[1] = 2;

int* p = scores;

p++;

*p = 2;

• Adding is cheaper than multiplying

Page 17: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

String = Array of Characters

char word[] = {‘K’,’a’,’t’,’e’,’\0’};

char word[] = “Kate”;

• Note that ‘\0’ and ‘0’ are different.

• Null terminated strings

Page 18: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Accessing characters

cout << word;

cout << word[0];

word[0] = ‘L’;• Note single quotes

Page 19: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Strings and pointers

for (char* p = word; *p; p++)

{

cout << *p;

}

Page 20: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Arrays of Pointers

char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };– Strings are pointers to the first character– char * – each element of suit is a pointer to a char

– The strings are not actually stored in the array suit, only pointers to the strings are stored

– suit array has a fixed size, but strings can be of any size

Page 21: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Arrays of Pointers

suit[3]

suit[2]

suit[1]

suit[0] ’H’ ’e’ ’a’ ’r’ ’t’ ’s’ ’\0’

’D’ ’i’ ’a’ ’m’ ’o’ ’n’ ’d’ ’s’ ’\0’

’C’ ’l’ ’u’ ’b’ ’s’ ’\0’

’S’ ’p’ ’a’ ’d’ ’e’ ’s’ ’\0’

Page 22: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Multi Dimensional Arrays

• Tables with rows and columns (m by n array)

• Like matrices: specify row, then column

Row 0Row 1Row 2

Column 0 Column 1 Column 2 Column 3a[ 0 ][ 0 ]a[ 1 ][ 0 ]a[ 2 ][ 0 ]

a[ 0 ][ 1 ]a[ 1 ][ 1 ]a[ 2 ][ 1 ]

a[ 0 ][ 2 ]a[ 1 ][ 2 ]a[ 2 ][ 2 ]

a[ 0 ][ 3 ]a[ 1 ][ 3 ]a[ 2 ][ 3 ]

Row subscriptArray name

Column subscript

Page 23: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

Multi Dimensional Arrays

• Initializationint b[2][2] = { { 1, 2 }, { 3, 4 } };

– Initializers grouped by row in braces – If not enough, unspecified elements set to zero

int b[2][2] = { { 1 }, { 3, 4 } }; • Referencing elements

– Specify row, then column

cout << b[0][1];

Page 24: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

String Library

• Functions originally from C– strlen– strcat– strcpy– strcmp– strtok

• Don’t reinvent the wheel

Page 25: Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Week 3 Questions from Last Week Hand in Lab 1 Arrays Pointers Strings Lab 2

Monday, Jan 20, 2002 Kate Gregorywith material from Deitel and Deitel

For Next class

• Complete Lab 2– Late penalty is TERRIBLE

• Read chapter 6