11 + 12 + 13. Arrays Flashcards

(38 cards)

1
Q

array

A

collection of data items of the sametype

eg. array of 5 names

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

arrays are ordered

A
  • there is an order to slots in an array
  • each slot is numbered
  • in C++ slots numbered from 0 upwards
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

need for arrays

A
  • allows to store lots of values at one time

- can print in order of slots

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

declaring arrays syntax

A

data_type array_name[size];

must put declared size in the square brackets

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

explain this array:

double grades[6];

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

declaring arrays - size of array

A

size of array must be known

can’t do this:
int size;
cin&raquo_space; size;
int array[siz];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

accessing array elements

A

in array[x], x is the subscript/index

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how to access array elements examples

A

cin&raquo_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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

array index expressions

A

index can be any expression whose value is an integer between 0 and (arraysize - 1)

eg. grades[i+2]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

For loops and arrays

A

use for loops for handling all elements of an array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

for loops and arrays syntax

A

for (i = 0; i

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

note on for loop condition

A

use arraysize in the FOR conditions rather than writing the number

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

index out of range errors

A

-occur in trying to access an array element beyond the end of the array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

what will happen?

double grades[6];
grades [7] = 78;

A

will overwrite memory locations storing some other variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what will happen?

double grades[6];
for (i = 1; i <=6; i++)

A

possibly disastrous

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

restrictions on array processing

A
  • 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
17
Q

syntax of passing array elements to another array

A

for (i = 0; i < arraysize; i++) {
array[i] = array2[z];
}

18
Q

syntax of passing in values into each slot of an array

A

for (i = 0; i < arraysize; i++) {
cin&raquo_space; array[i];
}

19
Q

specific example - array of month names

A
  • 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
20
Q

array slots

A

not every slot has to be filled

21
Q

passing in array elements to functions

A

for non-void functions, arrays should match function data type

22
Q

functions that take arrays

A

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

23
Q

passing arrays into functions syntax + declaration of the function

A
declaration of the function:
datatype function(datatype array[], int size)
calling the function:
function(arrayname, ARRAY_SIZE);
24
Q

functions and array elements

A

functions modify elements of array, array parameters are like reference parameters, even though they don’t have an ampersand

25
reading into arrays
functions can be used to read in values into an array as array parameters are modifiable
26
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
27
const modifier
used if you dont want function to change elements in array
28
syntax of const modifier
datatype function(const datatype a[], int size);
29
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
30
not recommended
generally a function shouldn't both update array elements and return a value
31
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
32
read to sentinel function syntax
check notes
33
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
34
declaring array size - good practice
``` this is good practice: const SIZE = 6; double array [SIZE]; ``` for ... blah blah instead of writing double array[6];
35
why declare array size like this
-will make SIZE easier to change later if we want to
36
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
37
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!
38
read and print function
- always void, do not return anything | - print arrays should use const so as not to change the array