CPS120: Introduction to Computer Science

Download Report

Transcript CPS120: Introduction to Computer Science

CPS120: Introduction
to Computer Science
Lecture 15
Arrays
Arrays: A Definition
A list of variables accessed using a single
identifier
May be of any data type
Can be single or multi-dimensioned
Vector classifications are object-oriented
representations of arrays
Declaring an Array
To 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
Initializing an Array
If 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 statement
would cause the compiler to declare an array of
size 5 since there are five values in the set of curly
braces
Using Arrays
When you visualize an array or represent it on paper,
you might think of it like this:
int scores[5] = {65, 76, 45, 83, 99};
scores
index position
element's value
0
65
1
76
2
45
3
83
4
99
Pitfalls of Using Arrays
Be 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 4
scores[5] = 99;
// this assignment
statement causes an error
Loops and Arrays
Use 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
Relationship Between Pointers &Arrays
139996
139997
…
140003
140004
140005
140006
140007
140003
M
I
\0
state_code
C++ and Strings
C++ 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.
Using Arrays
Elements must be accessed individually to
change their values
They are accessed via their subscripts which run
from zero to 1 less than the number of elements
declared
Remember, you can define an element outside
of your array and create an unpredictable
situation
Changing Values in an Array
You 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".
Templates
Allow you to create classes and functions that
can be used with any data type
An 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
Parallel Arrays
Sometimes, two one-dimensional arrays are used
together to tie two sets of data together
For 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
Multi-dimensional Arrays
Arrays of more than one dimension can be used to
arrange more complicated sets of data
Two-dimensional arrays are used quite often
For example, a spreadsheet can be thought of as 2dimensional 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
Declaring a Multi-dimensional Array
To 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
Initializing a Multi-dimensional Array
You 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, not
In the example above the value of the variable studentGrades[0] [2]
is 3
Displaying a Multi-dimensional Arrray
A 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