11 + 12 + 13. Arrays Flashcards
array
collection of data items of the sametype
eg. array of 5 names
arrays are ordered
- there is an order to slots in an array
- each slot is numbered
- in C++ slots numbered from 0 upwards
need for arrays
- allows to store lots of values at one time
- can print in order of slots
declaring arrays syntax
data_type array_name[size];
must put declared size in the square brackets
explain this array:
double grades[6];
declares an array
- called grades
- that can hold up to 6 values
- of type value
the slots in this array are are numbered 0 to 5
declaring arrays - size of array
size of array must be known
can’t do this:
int size;
cin»_space; size;
int array[siz];
accessing array elements
in array[x], x is the subscript/index
how to access array elements examples
cin»_space; grades[1]
-reads in a value for slot 1
x = grades[i]
-gets value of the slot numbered by whatever value i currently has and ssigns it to x
array index expressions
index can be any expression whose value is an integer between 0 and (arraysize - 1)
eg. grades[i+2]
For loops and arrays
use for loops for handling all elements of an array
for loops and arrays syntax
for (i = 0; i
note on for loop condition
use arraysize in the FOR conditions rather than writing the number
index out of range errors
-occur in trying to access an array element beyond the end of the array
what will happen?
double grades[6];
grades [7] = 78;
will overwrite memory locations storing some other variable
what will happen?
double grades[6];
for (i = 1; i <=6; i++)
…
possibly disastrous
restrictions on array processing
- cant assign a whole array to another, have to copy element by element (slot by slot) - use for loops to do this
- cant cin a whole array of elements - must read in slot by slot using for loop
syntax of passing array elements to another array
for (i = 0; i < arraysize; i++) {
array[i] = array2[z];
}
syntax of passing in values into each slot of an array
for (i = 0; i < arraysize; i++) {
cin»_space; array[i];
}
specific example - array of month names
- a program to map month numbers to month names
- create array of month names
- use slots numbered 1 to 12, leave 0 blank to make it easier to read
array slots
not every slot has to be filled
passing in array elements to functions
for non-void functions, arrays should match function data type
functions that take arrays
you can pass a whole array into a function or just an element from an array
function should be told size of array if you are passing a whole array
passing arrays into functions syntax + declaration of the function
declaration of the function: datatype function(datatype array[], int size)
calling the function: function(arrayname, ARRAY_SIZE);
functions and array elements
functions modify elements of array, array parameters are like reference parameters, even though they don’t have an ampersand
reading into arrays
functions can be used to read in values into an array as array parameters are modifiable
read_array function syntax
void read_array(datatype a[], int arraysize) { int i; for (i = 0; i < arraysize, i++) cin >> a[i]; }
note –> can initialise i in for loop
const modifier
used if you dont want function to change elements in array
syntax of const modifier
datatype function(const datatype a[], int size);
remember when passing arrays into functions
- functions processing arrays should take a size parameter
- if you don’t want to modify array, use const modifier
not recommended
generally a function shouldn’t both update array elements and return a value
how to use arrays where we don’t know how many slots will be filled
1) declare array big enough to handle max possible problem siz: DECLARED_SIZE
2) keep track of how many slots are filled: array_size or eg. num_Grades
3) use the latter number in FOR loops that process the array
read to sentinel function syntax
check notes
calling readtosentinel function syntax
readtosentinel(array, MAX_SIZE, count, sentinel);
note -> write what the sentinel is, or initialise the sentinel at the start or before the main and write sentinel
declaring array size - good practice
this is good practice: const SIZE = 6; double array [SIZE];
for … blah blah
instead of writing double array[6];
why declare array size like this
-will make SIZE easier to change later if we want to
finding max/min of set of numbers
-safer to set maxes and mins to the first value obtained instead of setting it to zero initially
syntax of finding max or min values in notes
index out of range errors
-watch out for when doing things such as:
double grades[5];
grades[i] = grades[i + 1];
what would happen when i = 4? There is no slot 5!
read and print function
- always void, do not return anything
- print arrays should use const so as not to change the array