Chapter6 - Chapter 6 Arrays 1 Dr Serdar Kirli Array...

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
Dr. Serdar Kirli 1 Chapter 6 Arrays
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Dr. Serdar Kirli 2 Array Declarations One Dimensional (Vector): int months[12]; 0 1 2 3 4 5 6 7 8 9 10 11
Background image of page 2
Dr. Serdar Kirli 3 Array Declarations One Dimensional (Vector): int months[12]= {31,28,31,30,31,30,31,31,30,31,30,31}; 0 1 2 3 4 5 6 7 8 9 10 11 31 28 31 30 31 30 31 31 30 31 30 31
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Dr. Serdar Kirli 4 Array Declarations Two Dimensional (Matrix): int sales[3][6]; 0 1 1 0 2 4 3 2 5 All C++ array indices start at zero Two-dimensional arrays are stored in memory by rows, i.e. the second index will vary most often sales[0][0], sales[0][1], sales[0][2], sales[0][3], sales[0][4], sales[0][5], sales[1][0], sales[1][1], sales[1][2], etc. sales [0][0] [0][1] [0][2] . . . [1][0] [1][1] . . . . [2][0] Etc.
Background image of page 4
Dr. Serdar Kirli 5 Array Declarations Order in memory: c[0][0][0], c[0][0][1], c[0] [1][0], c[0][1][1], c[0][2] [0], c[0][2][1], etc. N Dimensional: int c[4][3][2]; X Y Z
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Dr. Serdar Kirli 6 double exam1[200]; Stores exam grades for 200 students How many bytes are used for this array? Array Examples int months[12]; Each element holds number of days in corresponding month months[0] → Days in January months[1] → Days in February
Background image of page 6
Dr. Serdar Kirli 7 Array Examples double grades[200][13]; Similar to the previous example, but stores grades for 200 students for 13 separate assignments, rather than just one. e.g. 8 projects, 3 tests, 2 quizzes How many elements are in this array? How many bytes are used for this array? Student 1 Student 2 Student 3 Assignment 1 Assignment 13 Student 198 Student 199 Student 200 ? ? grades[2][8] grades[198][2]
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Dr. Serdar Kirli 8 Array Examples float sales[12][4][25]; Stores the number of sales made for 25 different products in four different regions in each month What does sales[3][2][18] refer to? Month Region Product January 0 February 1 . December 11 North 0 East 1 South 2 West 3 0 . . 24
Background image of page 8
Dr. Serdar Kirli 9 Array Examples Be careful when declaring arrays! Things can get out of hand easily. You may run out of memory if the array is too large. float x[50][365][24][60][100]; Stores state, day, hour, minute and type 2 billion cells, 8 billion elements double y[3][21][54][60]; How many bytes?
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Dr. Serdar Kirli 10 Array Initialization int a[4] = {1,2,3,4}; int b[] = {2,6,3,9}; int c[4] = {1,2}; int d[4] = {1,2,3,4,5}; a a[0] a[1] a[2] a[3] b b[0] b[1] b[2] b[3] c c[0] c[1] c[2] c[3] d d[0] d[1] d[2] d[3]
Background image of page 10
Dr. Serdar Kirli 11 Array Initialization int a[4] = {1,2,3,4}; int b[] = {2,6,3,9}; int c[4] = {1,2}; int d[4] = {1,2,3,4,5}; 1 2 3 4 a a[0] a[1] a[2] a[3] b b[0] b[1] b[2] b[3] c c[0] c[1] c[2] c[3] d d[0] d[1] d[2] d[3]
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Dr. Serdar Kirli 12 Array Initialization int a[4] = {1,2,3,4}; int b[] = {2,6,3,9}; int c[4] = {1,2}; int d[4] = {1,2,3,4,5}; 1 2 3 4 a a[0] a[1] a[2] a[3] 2 6 3 9 b b[0] b[1] b[2] b[3] c c[0] c[1] c[2] c[3] d d[0] d[1] d[2] d[3] If an array is declared and filled at the same time, the size does not need to be specified
Background image of page 12
Dr. Serdar Kirli 13 Array Initialization int a[4] = {1,2,3,4}; int b[] = {2,6,3,9}; int c[4] = {1,2}; int d[4] = {1,2,3,4,5}; 1 2 3 4 a a[0] a[1] a[2] a[3] 2 6 3 9 b b[0] b[1] b[2] b[3] 1 2 0 0 c c[0] c[1] c[2] c[3] d d[0] d[1] d[2] d[3] This is a legal statement. Only the first two elements are filled, and the rest are set to zero.
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Dr. Serdar Kirli 14 Array Initialization int a[4] = {1,2,3,4}; int b[] = {2,6,3,9}; int c[4] = {1,2}; int d[4] = {1,2,3,4,5}; 1 2 3 4 a a[0] a[1] a[2] a[3] 2 6 3 9 b b[0] b[1] b[2] b[3] 1 2 0 0 c c[0] c[1] c[2] c[3] d d[0] d[1] d[2] d[3] This statement will cause a compilation error. The number
Background image of page 14
Image of page 15
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 205

Chapter6 - Chapter 6 Arrays 1 Dr Serdar Kirli Array...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online