8/14/2019 Lecture 19 Part 2.pdf
1/16
1992-2012 by Pearson Education, Inc. & John Wiley & SonsSome portions are adopted from C++ f or Everyone by Horstmann
ENGR 1200U Introduc tion to Programming
Lecture 19 Arrays and Vecto rs
Dr. Eyhab Al-Masri
ENGR 1200UWinter 2013 - UOIT
What is the difference between anargument and a return value?
An argument is an expression that is sent to a function as an input to that function (it appears in the parentheses of a function call)
A return value is a piece of data that is output by a function, and sent back to the calling function.
8/14/2019 Lecture 19 Part 2.pdf
2/16
8/14/2019 Lecture 19 Part 2.pdf
3/16
ENGR 1200UWinter 2013 - UOIT
What are global constants?
A global constant is a named constant that is available to every function in a program
Global constants are typically used to represent unchanging values that are needed throughout a program
ENGR 1200UWinter 2013 - UOIT
Can you have two local variableswith the same name?
No.
int sum( int num1)
{ int num1 = 0;return num1;
}
8/14/2019 Lecture 19 Part 2.pdf
4/16
ENGR 1200UWinter 2013 - UOIT
Can you have a local and globalvariables with the same name?
Yes. int num1...
int sum( int num1){
num1 = 0;return num1;
}
ENGR 1200UWinter 2013 - UOIT
What is a static variable?
Sometimes it is desirable for a program to remember what value is stored in a local variable between function calls.
This is accomplished by making the variable static
Static variables are not destroyed when a function returns
8/14/2019 Lecture 19 Part 2.pdf
5/16
ENGR 1200UWinter 2013 - UOIT
What does it mean to overload afunction?
A Two or more functions may have the same name,
as long as their parameter lists are
different
int square( int number){
return number * number;}
double square( double number){
return number * number;}
ENGR 1200UWinter 2013 - UOIT
When working with a large number of values, it is important to be able tomanage large collections of data
ExampleSuppose we would like to store a set of 100 temperaturemeasurements to be used for perform several computationsSolution- Create different names for the temperature measurements?- Use a method that can work with a group of values?
your best choice is to use rr y
8/14/2019 Lecture 19 Part 2.pdf
6/16
ENGR 1200UWinter 2013 - UOIT
By using a array, you Can conveniently manage collections of data
Do not have to worry about the details of how they
are stored
Do not worry about how many are in the vector
automatic resizing
so what is the difference between vector and an array ?
ENGR 1200UWinter 2013 - UOIT
The variables you worked with so far are designed tohold only one value at a timeEach of the variable definitions (below) cause onlyenough memory to be reserved to hold one value of the specified data type
ON EPTAn array allows you to store and work withmultiple values of the same data type
int count; Enough memory for 1 int
double price; Enough memory for 1 double
char letter; Enough memory for 1 char
1234
56.5
A
8/14/2019 Lecture 19 Part 2.pdf
7/16
ENGR 1200UWinter 2013 - UOIT
How it works? An array works like a variable that can store a
group of values, all of the same data type. The values are stored together in consecutive
memory locations.
Examplestring WeekyDays[7]; //enough memory to hold seven string values
Element 0 Element 1 Element 2 Element 3 Element 4 Element 5 Element 6
ENGR 1200UWinter 2013 - UOIT
An array size declarator must be a constantinteger expression with a value greater than 0
It can be either a literal (as in the previous
example) or a named constant, as shown here:
Exampleconst int SIZE = 7; string WeekyDays[SIZE];
8/14/2019 Lecture 19 Part 2.pdf
8/16
ENGR 1200UWinter 2013 - UOIT
A one-dimensional array can be visualized as
a list of values arranged in either a row or a
column
Individual elements of the array are specified
using the array name and an offset .
In C++ the offset of the first element is always
0 (zero).
ENGR 1200UWinter 2013 - UOIT
WeekDays[0] WeekDays[1] WeekDays[2] WeekDays[3] WeekDays[4] WeekDays[5] WeekDays[6]
Exampleconst int SIZE = 7; string WeekyDays[SIZE];
WeekDays[0]
WeekDays[1]
WeekDays[2]
WeekDays[3]
WeekDays[4]
WeekDays[5]
WeekDays[6]
We assign a type and an identifier to an array and
then distinguish between elements , or values, inthe array using offsets
Offsets are also referred to as indices or subscripts
8/14/2019 Lecture 19 Part 2.pdf
9/16
ENGR 1200UWinter 2013 - UOIT
Definition Syntax
data_type identifier[array_size] [= initialization_list] ;
Examplesfloat temperature[100]; // Array of 100 floatschar letter[26]; // Array of 26 charactersdouble size[1200]; // Array of 1200 doublesstring name[10]; // Array of 10 string objects
ENGR 1200UWinter 2013 - UOIT
Initializing Arrays
Initializing array elements (initialization=>declaration)char vowels[5] = {'a', 'e', 'i', 'o', 'u'};bool ansKey[] ={ true , true , false , true , false , false };
vowels'a' 'e' 'i' 'o' 'u'
true falsefalsetruefalsetrue
ansKey
8/14/2019 Lecture 19 Part 2.pdf
10/161
ENGR 1200UWinter 2013 - UOIT Table 6..1: C++ for Everyone by Cay Horstman
ENGR 1200UWinter 2013 - UOIT
CONCEPTIndividual elements of an array are assigned uniquesubscripts. These subscripts are used to access theelements
Individual Variables
Although an entire array has only one name, elementsmay be accessed and used as individual variables
This is possible because each element is assigned a number (or subscript)
A subscript is used as an index to pinpoint a specific element in an array
8/14/2019 Lecture 19 Part 2.pdf
11/161
ENGR 1200UWinter 2013 - UOIT
ExampleWeekDays[0] = Sunday; // pronounced WeekDays sub zero
Sunday ? ? ? ? ?
WeekDays[0] WeekDays[1] WeekDays[2] WeekDays[3] WeekDays[4] WeekDays[5] WeekDays[6]
?
WeekDays[3] = Wednesday;
Sunday? ?
Wednesday? ?
WeekDays[0] WeekDays[1] WeekDays[2] WeekDays[3] WeekDays[4] WeekDays[5] WeekDays[6]
?
ENGR 1200UWinter 2013 - UOIT
Defining an Array
8/14/2019 Lecture 19 Part 2.pdf
12/161
ENGR 1200UWinter 2013 - UOIT
ExampleGiven the array:double values[] = { 32, 54, 67.5, 29, 35, 80, 115, 44.5, 100, 65 };
To access the element at index 4using this notation: values[4]
4 is the index (or subscript)
32.0
54.0
67.5
29.0
35.0
80.0
115.0
44.5
100.0
65.0
ENGR 1200UWinter 2013 - UOIT
ExampleGiven the array:
double values[] = { 32, 54, 67.5, 29, 35, 80, 115, 44.5, 100, 65 };
To access the element at index 4using this notation: values[4]
4 is the index (or subscript)
double values[10];
...cout
8/14/2019 Lecture 19 Part 2.pdf
13/161
ENGR 1200UWinter 2013 - UOIT
Example (contd)The same notation can be used to change the element
values[4] = 17.7; 32.0
54.0
67.5
29.0
35.0
80.0
115.0
44.5
100.0
65.0
ENGR 1200UWinter 2013 - UOIT
Example (contd)The same notation can be used to change the element
values[4] = 17.7; 32.0
54.0
67.5
29.0
17.7
80.0115.0
44.5
100.0
65.0
8/14/2019 Lecture 19 Part 2.pdf
14/16
8/14/2019 Lecture 19 Part 2.pdf
15/161
ENGR 1200UWinter 2013 - UOIT
Example (contd)That is, the legal elements for the values array are:
values[0] , the first elementvalues[1] , the second elementvalues[2] , the third elementvalues[3] , the fourth elementvalues[4] , the fifth element
...values[9] , the tenth nd l st leg l element
recall: double values[10];
The index must be >= 0 and
8/14/2019 Lecture 19 Part 2.pdf
16/16
ENGR 1200UWinter 2013 - UOIT
BUGSC++ does not enforce array bounds
Invalid references are NOT reported by the compiler
May or may not result in run-time error
Examples: segmentation fault, bus error
Such errors are often difficult to identify
Results in unpredictable program behavior