17
CPS120: Introduction to Computer Science Lecture 15 Arrays

CPS120: Introduction to Computer Science Lecture 15 Arrays

Embed Size (px)

Citation preview

Page 1: CPS120: Introduction to Computer Science Lecture 15 Arrays

CPS120: Introduction to Computer Science

Lecture 15Arrays

Page 2: CPS120: Introduction to Computer Science Lecture 15 Arrays

Arrays: A DefinitionA list of variables accessed using a single identifier

May be of any data type

Can be single or multi-dimensionedVector classifications are object-oriented representations of arrays

Page 3: CPS120: Introduction to Computer Science Lecture 15 Arrays

Declaring an ArrayTo declare an array before it is used in the body of your program, you must use a statement like:

int scores[10];This would declare an array of integers, named "scores". In this case, scores can store up to 10 different integer values.

The positions of the array are identified by their index positions which run from 0 to 9 (not 1 to 10.)

Each one of the 10 variables in scores is called an element

Page 4: CPS120: Introduction to Computer Science Lecture 15 Arrays

Initializing an ArrayIf you wish to initialize each element of an array to a specific value, you can use the statement,

int scores[] = {65, 76, 45, 83, 99};

You don't even have to specify a size of the array in this case since the initialization statementwould cause the compiler to declare an array of size 5 since there are five values in the set of curly braces

Page 5: CPS120: Introduction to Computer Science Lecture 15 Arrays

Using ArraysWhen you visualize an array or represent it on paper, you might think of it like this:

int scores[5] = {65, 76, 45, 83, 99};

scoresindex position 0 1 2 3 4element's value 65 76 45 83 99

Page 6: CPS120: Introduction to Computer Science Lecture 15 Arrays

Pitfalls of Using ArraysBe careful not to attempt to access an index position that is not actually part of an array.

You may not experience a syntax or run-time error. Rather, you may accidentally overwrite another variable int scores[5]; // scores has 5 elements which have index positions 0 through 4scores[5] = 99; // this assignment statement causes an error

Page 7: CPS120: Introduction to Computer Science Lecture 15 Arrays

Loops and ArraysUse loops in conjunction with arrays to allow the user to input data into an array

Use loops with arrays to display data stored in an array

Page 8: CPS120: Introduction to Computer Science Lecture 15 Arrays

Relationship Between Pointers &Arrays

139996139997 140003 state_code…140003 M140004 I140005 \0140006140007

Page 9: CPS120: Introduction to Computer Science Lecture 15 Arrays

C++ and StringsC++ numbers the individual characters in a string beginning with index position 0. After the declaration statement,

string stateName = "Michigan"; 'M' is considered to be in the index position 0, the first 'i' in the index position 1, the 'c' in the index position 2, and so on. Note that index positions may contain a blank space, which is, of course, a valid character. A blank space has an ASCII value of 32.

Page 10: CPS120: Introduction to Computer Science Lecture 15 Arrays

Using ArraysElements must be accessed individually to change their valuesThey are accessed via their subscripts which run from zero to 1 less than the number of elements declaredRemember, you can define an element outside of your array and create an unpredictable situation

Page 11: CPS120: Introduction to Computer Science Lecture 15 Arrays

Changing Values in an ArrayYou may use subscript notation to change the value of one specific character within a string. The assignment statement,

stateName[1] = 'I';would change the lowercase e originally found in index position 1 of the example above to an uppercase I. So the string is now "MIchigan".

Page 12: CPS120: Introduction to Computer Science Lecture 15 Arrays

TemplatesAllow you to create classes and functions that can be used with any data typeAn array class can be created that will allow the programmer to choose the data type used to tore the data in the array

Templates allow a programmer to construct a class that can be utilized by any data type

Page 13: CPS120: Introduction to Computer Science Lecture 15 Arrays

Parallel ArraysSometimes, two one-dimensional arrays are used together to tie two sets of data togetherFor example, you can store 5 student ID numbers in a one-dimensional array of integers, while storing the grades of the same 5 students in an array of doubles.

As long as the student grades are sorted in the same representative order as the student ID numbers, one can use the two arrays as parallel arrays. This allows the same index position to refer to a given student's ID number and his/her grade by accessing the appropriate elements of the two arrays

Page 14: CPS120: Introduction to Computer Science Lecture 15 Arrays

Multi-dimensional Arrays Arrays of more than one dimension can be used to arrange more complicated sets of dataTwo-dimensional arrays are used quite often

For example, a spreadsheet can be thought of as 2-dimensional array (sometimes called a tabl.)

The same variable name (that is, identifier) is used to access each element of the 2-dimensional array but the proper index position subscripts must be used

Page 15: CPS120: Introduction to Computer Science Lecture 15 Arrays

Declaring a Multi-dimensional ArrayTo declare an array of integers called studentGrades to be a 2-dimensional array with 3 rows and 4 columns, you would use the statement:

int studentGrades[3] [4];

where the first integer value is used to specify the number of rows in the array and the second value specifies the number of columns

Think of remote control

Page 16: CPS120: Introduction to Computer Science Lecture 15 Arrays

Initializing a Multi-dimensional ArrayYou can initialize the 2-dimensional array when you declare it by using commas and braces appropriately

int studentGrades[3] [4] = { { 1, 2, 3, 4}, { 5, 6, 7, 8}, { 9, 10, 11, 12} };

Be careful though when assigning values to a specific position within a 2-dimensional array.

Just like one-dimensional arrays, the subscript positions with regard to the rows and the columns begin at 0, notIn the example above the value of the variable studentGrades[0] [2] is 3

Page 17: CPS120: Introduction to Computer Science Lecture 15 Arrays

Displaying a Multi-dimensional ArrrayA two dimensional array can be obtained as input and displayed as a table or matrix using double nested loops

The use of '\t' causes the elements to be neatly separated by tabs. The cout << endl; statement separates the rows