Download presentation

Presentation is loading. Please wait.

Published byChristiana Wilshire Modified over 4 years ago

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

2
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel

3
Monday, Jan 20, 2002Kate Gregory with 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

4
Monday, Jan 20, 2002Kate Gregory with 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]

5
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Declaring Arrays int scores[10]; char letters[26]; float ratios[5]; Size is fixed

6
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Using Array Elements scores[i]++; cout << letters[4]; ratio[2] = 4.7;

7
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel No bounds checking in C++ int scores[10]; for (int i=0; i<20; i++) { scores[i] = 100; } Youre the programmer!

8
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Bug prevention #define MAXSCORES 10 int scores[MAXSCORES]; for (int i=0; i< MAXSCORES; i++) { scores[i] = 100; } More readable, less error-prone Cant use a variable when declaring the array

9
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Initializers int n[ 5 ] = { 1, 2, 3, 4, 5 }; Or just int n[] = { 1, 2, 3, 4, 5 };

10
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Passing arrays to functions int total(int[] a); //... int numbers[] = {1,2,3,4,5}; int x = total(numbers);

11
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Taking arrays in functions int total(int[] a) { int tot = 0; int limit = sizeof(a) / sizeof(int); for (int i=0; i<limit; i++) { tot += a[i]; } return tot; }

12
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Pointers Address of a variable in memory They have a type int* p; char* s;

13
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel & operator & (address operator) gets address of operand int y = 5; int *yPtr; yPtr = &y; // yPtr gets address of y yPtr y 5 yptr 500000600000 y 5 Address of y is value of yptr

14
Monday, Jan 20, 2002Kate Gregory with 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

15
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Pointers and Arrays int scores[10]; x = scores[3]; x++; y = scores; y++;

16
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Pointers and Arrays scores[1] = 2; int* p = scores; p++; *p = 2; Adding is cheaper than multiplying

17
Monday, Jan 20, 2002Kate Gregory with 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

18
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Accessing characters cout << word; cout << word[0]; word[0] = L; Note single quotes

19
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Strings and pointers for (char* p = word; *p; p++) { cout << *p; }

20
Monday, Jan 20, 2002Kate Gregory with 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

21
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Arrays of Pointers suit[3] suit[2] suit[1] suit[0]Hearts \0 Diamonds Clubs Spades

22
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Multi Dimensional Arrays Tables with rows and columns ( m by n array) Like matrices: specify row, then column Row 0 Row 1 Row 2 Column 0Column 1Column 2Column 3 a[ 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 subscript Array name Column subscript

23
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel Multi Dimensional Arrays Initialization int 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];

24
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel String Library Functions originally from C –strlen –strcat –strcpy –strcmp –strtok Dont reinvent the wheel

25
Monday, Jan 20, 2002Kate Gregory with material from Deitel and Deitel For Next class Complete Lab 2 –Late penalty is TERRIBLE Read chapter 6

Similar presentations

OK

Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.

Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google

Download ppt on civil disobedience movement of 1930 Free ppt on number system for class 9 Hrm ppt on recruitment process Ppt on diwali in hindi language Ppt on ice cream industry Free download ppt on exponents and powers Ppt on network theory concepts Ppt on electric power steering Ppt on time management at workplace Ppt on earth hour day