310201 Fundamental Programming More on Arrays
Review simple variables hold a single value in the previous class we saw that arrays hold a collection of related values also in the last class, we saw the basics of declaring, initialising and using arrays in a C++ array, the first value in an array is held at position 0, not 1
More on Arrays in this class we cover some more of the details of implementing arrays in C++ some jargon more on initialisation more on referencing arrays a common error using arrays passing arrays to functions passing array elements to functions
Some Jargon Used With Arrays element : a value held in an array index : an integer value that refers to an element in an array; we say C++ arrays are zero- indexed – the first position is zero subscript : can be used instead of index – as in “subscript out of range”; can also be used to describe the expression that appears between the brackets to refer to an element in an array dimension : the number of elements in an array - the size of the array – as in “the dimensions of my office are 3 meters by 4; the room size” How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.
More on Initialising Arrays if the array size is omitted when it is declared, the compiler can automatically determine the size from initial values int Primes[] = {1, 3, 5, 7, 11}; is equivalent to int Primes[5] = {1, 3, 5, 7, 11}; How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.
More on Referencing Arrays we normally use an integer literal, or an integer variable, to refer to a value of interest in an array Results[0] Results[ResultNbr] also, an integer expression can be used Results[CurrentPos - 1] How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.
A Common Error Using Arrays a common error when using arrays is to refer to an element that does not exist that is: to use an index value that is outside the acceptable range... How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.
Activity can you see how this code refers to an array element that does not exist? const NBR_RESULTS = 3; int Results[NBR_RESULTS] = {0}; for (int ResultNbr = 0; ResultNbr <= NBR_RESULTS; ResultNbr++) cout << Results[ResultNbr]; How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.
Activity Break
Activity Feedback in the last trip through the loop below, the code refers to Results[3], which does not exist const int NBR_RESULTS = 3; int Results[NBR_RESULTS] = {0}; for (int ResultNbr = 0; ResultNbr <= NBR_RESULTS; ResultNbr++) cout << Results[ResultNbr] << endl; How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.
A Common Error Using Arrays C++ does not give an error when using an index value that is out-of-range it is the programmer’s responsibility to ensure that array references are always within the acceptable range if, in an expression, you refer to an array element that does not exist: results are unpredictable – the program may give erroneous output, or go into an infinite loop How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.
A Common Error Using Arrays if you assign a value to an array element that does not exists: with luck, you’ll get a memory violation error – so you know there’s a problem without luck, results are unpredictable: erroneous output produced infinite loops lock-up the program memory used by the operating system is overwritten causing a system crash How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.
Passing Arrays to Functions arrays are always passed to functions by reference – but, no ampersand (&) appears before the parameter name consider a program using an array called Results as shown on the next slide... How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.
Passing Arrays to Functions main Results NbrResults Results Results NbrResults GetResults DisplayResults Results NbrResults SortResults How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.
notes: here, the maximum number of results is defined void GetResults(int Results[], int &NbrResults); void SortResults(int Results[], int NbrResults); void DisplayResults(int Results[], int NbrResults); const int MAX_NBR_RESULTS = 10; void main (void) { int Results[MAX_NBR_RESULTS] = {0}, NbrResults = 0; GetResults(Results, NbrResults); SortResults(Results, NbrResults); DisplayResults(Results, NbrResults); } How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area. notes: here, the maximum number of results is defined as a global constant – it is used to define the size of the Results array and will also be used to avoid an out-of-range error in the GetResults function
notes: the NbrResults parameter is an output of the void GetResults(int Results[], int &NbrResults); void SortResults(int Results[], int NbrResults); void DisplayResults(int Results[], int NbrResults); const int MAX_NBR_RESULTS = 10; void main (void) { int Results[MAX_NBR_RESULTS] = {0}, NbrResults = 0; GetResults(Results, NbrResults); SortResults(Results, NbrResults); DisplayResults(Results, NbrResults); } How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area. notes: the NbrResults parameter is an output of the GetResults function, so an ampersand appears before its name in the GetResults header and declaration
notes: the Results array is also an output of the void GetResults(int Results[], int &NbrResults); void SortResults(int Results[], int NbrResults); void DisplayResults(int Results[], int NbrResults); const int MAX_NBR_RESULTS = 10; void main (void) { int Results[MAX_NBR_RESULTS] = {0}, NbrResults = 0; GetResults(Results, NbrResults); SortResults(Results, NbrResults); DisplayResults(Results, NbrResults); } How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area. notes: the Results array is also an output of the GetResults function, but an ampersand does not appear before its name in GetResults header and declaration
notes: square brackets appear in header and declaration void GetResults(int Results[], int &NbrResults); void SortResults(int Results[], int NbrResults); void DisplayResults(int Results[], int NbrResults); const int MAX_NBR_RESULTS = 10; void main (void) { int Results[MAX_NBR_RESULTS] = {0}, NbrResults = 0; GetResults(Results, NbrResults); SortResults(Results, NbrResults); DisplayResults(Results, NbrResults); } How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area. notes: square brackets appear in header and declaration to show that a parameter is an array
notes: square brackets do not appear in the function call void GetResults(int Results[], int &NbrResults); void SortResults(int Results[], int NbrResults); void DisplayResults(int Results[], int NbrResults); const int MAX_NBR_RESULTS = 10; void main (void) { int Results[MAX_NBR_RESULTS] = {0}, NbrResults = 0; GetResults(Results, NbrResults); SortResults(Results, NbrResults); DisplayResults(Results, NbrResults); } How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area. notes: square brackets do not appear in the function call
notes: one does not need to specify the size of an array void GetResults(int Results[], int &NbrResults); void SortResults(int Results[], int NbrResults); void DisplayResults(int Results[], int NbrResults); const int MAX_NBR_RESULTS = 10; void main (void) { int Results[MAX_NBR_RESULTS] = {0}, NbrResults = 0; GetResults(Results, NbrResults); SortResults(Results, NbrResults); DisplayResults(Results, NbrResults); } How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area. notes: one does not need to specify the size of an array that appears as a parameter in a function header or declaration – it is normal practice to not specify the size of a parameter that is an array
notes: however, called functions need to know how many void GetResults(int Results[], int &NbrResults); void SortResults(int Results[], int NbrResults); void DisplayResults(int Results[], int NbrResults); const int MAX_NBR_RESULTS = 10; void main (void) { int Results[MAX_NBR_RESULTS] = {0}, NbrResults = 0; GetResults(Results, NbrResults); SortResults(Results, NbrResults); DisplayResults(Results, NbrResults); } How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area. notes: however, called functions need to know how many values have been stored in the array
Passing Arrays to Functions the DisplayResults function may look like this… void DisplayResults(int Results[], int NbrResults) { cout << "Results: " << endl; for (int ResultNbr = 0; ResultNbr < NbrResults; ResultNbr++) cout << " Result[" << ResultNbr << "]: " << Results[ResultNbr] << endl; } How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.
Passing Arrays to Functions it is possible to protect the elements of an array passed as an input to a function: put const in front of the parameter declaration any attempt by the function to assign a value to the array will result in an error when compiled How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area. void DisplayResults(const int Results[], int NbrResults)
Activity an interesting, and timely, example of passing an array to a function is shown on the next slide what output do you think this program will produce? How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.
#include <iostream.h> void Transform(char Array[], int NbrElements); void main (void) { const int NBR_CHARS = 3; char Name[NBR_CHARS] = {'I','B','M'}; int Index; cout << "Name before Transform is: " ; for (Index = 0; Index < NBR_CHARS; Index++) cout << Name[Index]; Transform(Name, NBR_CHARS); cout << endl << "Name after Transform is: " ; cout<<Name[Index]; } void Transform(char Array[], int NbrElements) for (int Index = 0; Index < NbrElements; Index++) --Array[Index]; How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.
Activity Break
Activity Feedback output produced by this program is: recall that characters are stored as an integer - the following will output H char MyChar = 'I'; cout << --MyChar; you may know that the computer in 2001: A Space Odyssey was called HAL Name before Transform is: IBM Name after Transform is: HAL
Passing Arrays Elements to Functions the previous example showed how to pass an entire array to a function we can also pass a single array element to a function whole arrays are always reference parameters individual elements of an array can be passed as a value or reference parameter How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.
Activity what output do you think the following code will produce? How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.
Activity #include <iostream.h> void PassByValue(int ArrayElement); void PassByReference(int &ArrayElement); void main(void) { int Marks[3] = {10,20,30}; PassByValue(Marks[0]); PassByReference(Marks[1]); for (int Mark = 0; Mark <= 2; Mark++) cout << “Marks[“ << Mark << “]: “ << Marks[Mark] << endl; } void PassByValue(int ArrayElement) ArrayElement++; void PassByReference(int &ArrayElement) How can a datagram be transmitted across a physical network that does not understand the datagram format? IP Datagram is encapsulated in a hardware frame Placed entirely in the Data area of the frame Hardware does not examine or change the contents of the data area.
Activity Break
Activity Feedback output produced by this program is: Marks[0]: 10
Activity write a function named Smallest, that accepts an array of double precision real numbers, and returns the smallest value stored in the array
Activity Break
Summary in this class we covered: some of the jargon used with arrays automatic sizing of arrays from initial values non-integer array references referencing non-existing array elements assignment to non-existing array elements passing arrays to functions passing arrays elements to functions