CSE 100 Lecture Notes - Lecture 33: Array Data Structure, Bounds Checking, In C

92 views6 pages
Chapter 8
Array subscripts
can be an integer constant, integer variable, of integer expression
Examples →
cin >> tests[3]; (int constant)
cout << tests[i]; (int variable)
cout << tests[i+j]; (int expression)
Accessing All Array Elements
to access each element of an array
Use a loop
Let the loop control variable be the array subscript
A different array element will be referenced each time through the
loop
for(i=0; i<5; i++)
cout << tests[i] << endl;
Getting Array Data from a File
const int ISIZE = 5;
int sales [ISIZE];
ifstream dataFile;
dataFile.open(“sales.dat”);
if(!dataFile)
cout << “Error opening data file\n;
else // Input daily sales
{ for (int day = 0; day < ISIZE; day++)
dataFile >> sales[day];
dataFile.close();
}
Sending Array Data to a File
Open the file using an ofstream object
Use a loop to write each array element to the file
Close the file
No bounds checking
There are no checks in C++ that an array subscript is in range
An invalid array subscript can cause the program to overwrite other memory
Example →
const int ISIZE = 3;
int i = 4;
int num[ISIZE];
num[i] = 25;
[0]
[1]
[2]
25
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in
^^^^^^num^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Off by One Erros
These most often occur when program accesses data one position
beyond the end of an array, or misses the first or last element of an array
If an array has size n, then the subscripts range from 0 to n-1
8.4 Array Initialization
An array can be initialized during program execution with assignment statements
tests[0] = 79;
tests[1] = 82; //etc
It can be initialized at array definitioj with an initialization list
const int ISIZE = 5;
int tests[ISIZE] = {79,82,91,77,84};
Start at element 0 or 1?
You may choose to declare arrays to be one larger than needed. This
allows you to use the element with subscript 1 as the ‘first’ element, etc.,
and may minimize off by one errors
The element with subscript 0 is not used
This is most often done when working with ordered data e.g. months of
the year or days of the week
Partial Array Initialization
If array is initialized at definition with fewer values than the size declarator
of the array, the remaining elements will be set to 0 or the empty string
int tests[ISIZE] = {79, 82};
79
82
0
0
0
Initial values are used in order; you cannot skip over elements to initialize
a noncontiguous range
You cannot have more values in the initialization list than the declared
size of the array
Implicit Array Sizing
C++ can determine the array size by the size of the initialization list
short quizzes[ ] = {12, 17, 15, 11};
12
17
15
11
You must use either array size declarator or an initialization list when the array is
defined
Alternate Ways to Initialize Variables
You can initialize a variable at definition time using a functional notation
int length(12); // same result as int length = 12;
In C++ 11 and higher, you can also do this:
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 6 pages and 3 million more documents.

Already have an account? Log in

Get access

Grade+
$40 USD/m
Billed monthly
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
10 Verified Answers
Class+
$30 USD/m
Billed monthly
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
7 Verified Answers

Related Documents