Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

11/9/2022

Objectives
General Objective
To use one dimensional and
multidimensional array variables
Specific Objectives:
To
declare them

Arrays in C initialise them


access their elements
discuss how they are stored in memory

Arrays Arrays
Arrays are used when you are dealing with
several related variables of the same type.  You may decide to have
E.g. say you want
10 different variables to represent
to write a program that will let the user the 10 different integers,
enter 10 integers e.g. 2 4 6 8 …
10 scanf statements to have the
and then print them out in the reverse user enter values for them, and then
order that they were entered i.e. … 8 6 4
2… 10 printf statements to print them
How will you do this? out in reverse order.

1
11/9/2022

Arrays
Arrays A diagrammatic representation

This is clearly too much code for


such a simple task.
Imagine if you want to do the
same thing with 1000 integers!
Fortunately, C allowsarrays:
a sequence of variables of the
same type.

Arrays
Arrays
An array is an identifier that refers to a
collection of data items that all have thesame
name. I T E M S
The data items must be of thesame type (e.g.
all integers or all characters). 0 1 2 3 4
you CANNOT store multiple types of data in
an array. A representation of an array with 5
Note that: an array is a fixed number of data
items.
character elements
Once you create it you cannot change its size.

2
11/9/2022

Arrays Arrays
Declaring Them Declaring Them
To declare an array, you must
specify 3 things: char arr_names[5];
the array type This declares an array of5
elements of type char.
the array name (identifier)
the array size (number of Th e n a m e o f t h e a r r a y i s
values you will store). arr_names.

Arrays Arrays
Declaring Them Accessing the Values
We now know an array is a
collection of data items that all
arr_names (Array name / identifier)
have the same name and type.
How can we distinguish the
individual array elements from one
<=5 characters shall be stored here
another?
By using the value that is assigned
to a subscript.

3
11/9/2022

Arrays Arrays
Accessing Their Elements Accessing the Values
(Array name / We use the following array syntax:
arr_names identifier)
array_name[i]
Elements
In C, array elements start at position (index) 0.
[0] [1] [2] [3] [4] Indices /
subscript Square brackets are used to access eachindexed
value (aka element).
Each element can be accessed by a combination of
Thus:
the array’s name and its position in the array
(subscript/index). arr_student_age[5]
BEWARE the off-by-one error; we start from index refers to the sixth element in an array called
0 (not 1). arr_student_age.

Arrays Arrays
Accessing Their Elements Accessing Their Elements

int numbers[10]; To access the separate values in this array, you


must use

We are declaring an the name of the array followed by

array of 10 integers. the index (subscript) of the element you want


to access within square brackets.

The name of the array is The first element of the array has index 0.

numbers. So the numbers array has 10 elements with


indexes 0 through 9.

4
11/9/2022

Arrays
Accessing Array Elements
Accessing Their Elements
numbers
NOTE:
The value inside of the brackets does not
have to be a constant.
To access numbers’ first element:
It could be a variable or expression.
numbers[0] E.g.:

To access the tenth (last) element: numbers[age]

numbers[9]
or even

numbers[mark * 2 + 1]

Arrays Arrays
Assigning Values Assigning Values
int marks [5]; //var declaration
marks We have created an array calledmarks
marks[0] = 21;
21 28 8 16 0 marks has space for 5 int variables.
marks[1] = 28; [0] [1] [2] [3] [4] We then assign integer values to the variables.
Notice that we stored 0 in the last space of
marks[2] = 8; the array.

marks[3] = 16; This is a common technique used by C


programmers to signify the end of an array.
marks[4] = 0;

5
11/9/2022

Arrays Arrays
Assigning Values Assigning Values

A single value in an array can be


assigned a value just like any The following are valid expressions
other variable. (assuming numbers is an array of
integers):
Just specify the array with the
index to the left of an numbers[5] = 820;

assignment operator. numbers[x] = numbers[x-1] + numbers[x-2];


arr_digits[i] =0;

Arrays Arrays
Assigning Values Declaring Them
Assigning values to array elements: Arrays may consist of any of
numbers[8] = mark; the valid data types.
Arrays are declared along
Assigning array elements to a
variable: with all other variables in the
declaration section of the
mark = numbers [9]; program …

6
11/9/2022

Array Example Array Example


#include <stdio.h> #include<stdio.h>

int main()
void main()
{
{ // Declare an array to store the ages (in years) of five children
int ageArray[5];
int numbers[100]; <— Xcode warning: Unused //Enter the values of the elements of the array

float averages[20];
ageArray[0]=10;
variable ‘averages' ageArray[1]=11;
numbers[2] = 10; ageArray[2]=9;
ageArray[3]=8;
--numbers[2]; ageArray[4]=12;

printf("The 3rd element of array Numbers is


//Now print the ages of the children
printf("The first child's age is %d years.\n"
,ageArray[0]);
%d\n", numbers[2]); printf("The second child's age is %d years. \n"
printf("The third child's age is %d years. \n"
,ageArray[1]);
,ageArray[2]);
} printf("The fourth child's age is %d years. \n",ageArray[3]);
Output? }
printf("The fifth child's age is %d years. \n"
,ageArray[4]);

The 3rd element of array Numbers is 9 Output?


Program ended with exit code: 0

Output Arrays
Processing Them
The first child's age is 10 years.
The second child's age is 11 years. C does NOT allow single operations involving
The third child's age is 9 years. entire arrays.
The fourth child's age is 8 years.
The fifth child's age is 12 years. Any operations must be carried out on an
Program ended with exit code: 36 element by elementbasis.
Note: Examples of operations:
The exit code is not 0 as the compiler
assignment
warned us that:
comparison
Return type of ‘main’ is not ‘int’

7
11/9/2022

Arrays Arrays
Assigning Values
Processing Them
For instance, it is NOT acceptable to assign one
array to another array even if they are similar:
Instead, you have to copy one element (value)
i.e. even if they have the same data type, at a time.
same dimensionality and same size.
Thus, any such operations must be carried out
Let numbers and elements be arrays of 10 on an element by element basis
.
integers each.
We usually accomplish this within aloop.
It is NOT possible to say:
Each pass through the loop is used to process
elements = numbers; one array element.

Arrays Arrays
Processing Them Processing Them
for (i = 0; i < 10; ++i)
We usually use for loops to {
iterate over an array when printf("The number is: %d",
numbers[i]);
accessing its elements or }
writing data to those The condition takes into account
elements. the fact that the numbers array
has 10 elements.

8
11/9/2022

Arrays Arrays: Processing Them

Processing Them #include <stdio.h>


#define SIZE 10
int main(void)
{
Recall the problem we started with i.e.:
int num1, numbers[SIZE];
write a program that will let the user for (num1 = 0; num1 < SIZE; num1++)
enter 10 integers e.g. 2 4 6 8 … {
printf("Enter number: ");
and then print them out in the reverse scanf("%d", &numbers[num1]);
order that they were entered i.e. … 8 6 }
printf(“\nReversing the numbers:\n"
);
42… for (num1 = SIZE - 1; num1 >= 0; num1--)
How will you do this? printf("%d\n", numbers[num1]);
}
Output?

Sample User’s Screen


NB: This slide shows BOTH the user’s input, and the program’s output.
Arrays
Enter number: 2 Accessing Their Elements – The
Example Explained
Enter number: 4
Enter number: 6
Enter number: 8
Enter number: 10
Enter number: 12
Enter
Enter
number: 14
number: 16
A c o n s t a n t , S I Z E, i s u s e d t o
Enter
Enter
number: 18
number: 20
represent the number of integers
Reversing the numbers:
used.
20
18
16
The reason you might do this is in
14
12
case you think you might change
10
8
your mind and want to change it
6
4
later.
2
Program ended with exit code: 0

9
11/9/2022

Arrays Arrays
Accessing Their Elements – The Accessing Their Elements – The Example Explained
Example Explained

For instance, let's say you want to change the


The first "for" loop loops from 0
program to let the user enter 20 integers and to SIZE-1.
print them out in reverse order. This is because the array is indexed
Now, you only have to change 10 to 20 in one
with these values.
place. The second "for" loop loops from
If you didn't use a constant, how many places
SIZE-1 down to 0.
would you have to change 10 to 20? This is because we are printing out
the array in reverse order.
3 places (everywhere that SIZE is being used).

Arrays Arrays
Accessing Their Elements – The Example Explained Accessing Their Elements – The Example Explained

for (num1 = SIZE - 1; num1 >= 0; num1--)


Note: printf("%d\n", numbers[num1]);
for (num1 = SIZE - 1; num1 >= 0; num1--) We place a ‘\n’ at the end of the printf
printf("%d\n", numbers[num1]); statement in the second loop for each value to
Each iteration of this loop contains just one be printed on its own line.
simple statement Often, when printing out the values of an array,
(the call to the printf function) you will want to print them out on one line
separated by spaces or commas.
There is no need to enclose it in curly braces.
Let's say you want to print out the 10
However, it’s preferable that you do so. Why? elements of array numbers on one line
For readability and consistency. separated by commas.
How would you rewrite the output loop?

10
11/9/2022

Arrays
Accessing Their Elements – The Example Explained Arrays
Accessing Their Elements – The Example
If you wrote: Explained

for (num1 = SIZE - 1; num1 >= 0; num1--) You need two extra
printf(“%d, ", numbers[num1]);
statements to ensure:
It would print out:
no comma is printed out
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Program
before the first element to
ended with exit code: 0 be printed or after the last,
and
There would be a comma at the end of only one newline is printed
the list of numbers. out at the end.

Arrays Arrays
Accessing Their Elements – The Example Explained Accessing Their Elements – The Example Explained

printf("%d", numbers[SIZE - 1]); 10, 9, 8, 7, 6, 5, 4, 3, 2, 1


for (num1 = SIZE - 2; num1 >= 0; num1--) Program ended with exit code: 0
printf(", %d", numbers[num1]);
printf("\n"); The last element of the array is printed
first.
Each other element is then printed in
Output:
reverse order with a commabefore each.
When the loop ends, we print out a newline
10, 9, 8, 7, 6, 5, 4, 3, 2, 1 once.
Program ended with exit code: 0

11
11/9/2022

Arrays Arrays
Accessing Their Elements – The Example Explained
Accessing Their Elements – A
printf("%d", numbers[SIZE - 1]);
2nd Example:
for (num1 = SIZE - 2; num1 >= 0; num1--)
printf(", %d", numbers[num1]);
printf("\n"); The following programme
In this for loop, notice that unlike our first version of this
reads in text one character at
program, we are now starting num1 at a time until an EOF is read
SIZE - 2 and keeps track of how many
instead of
times each digit (0 through
SIZE - 1 9) is encountered.
Why?
We’ve already printed out thearray’s last element.

#include <stdio.h>
Arrays
Accessing Their Elements – A
Arrays
int main(void)
{
2nd Example: Initialising Values
char c;
int arr_digits[10];
int x;
We have to initialise all the values in the array.
for (x = 0; x <= 9; x++) Just like with simple variables, if the array
arr_digits[x] =0;
values are not initialised, they are
while ((c = getchar()) != EOF)
{ unpredictable.
if ((c >= '0') && (c <= '9'))
Why do we have to initialise the array here but
++arr_digits[c -'0'];
} not in the first program?
for (x = 0; x <= 9; x++) Because in the first program, we have the
printf("%d\n", arr_digits[x]);
return 0;
user enter all the values, and these values are
} assigned directly to the array.
Output:

12
11/9/2022

Arrays Arrays
Initialising Values Assigning Values

Also note that a single value in an


In this program, we are counting array can be assigned a value just like
digits, and incrementing the values in any other variable.
Just specify the array with the index to the
the array but never assigning them left of an assignment operator.
directly to a specific value. The following are valid expressions
(assuming numbers is an array of
We need all the values to start at 0
integers):
to have this program work. numbers[5] = 820;
numbers[x] = numbers[x-1] + numbers[x-2];

Arrays Arrays
Accessing Their Elements – A 2
nd
Assigning Values Example Explained:

Remember, it is NOT acceptable to In the example2 prog, when we read a digit, we can
assign one array to another array. check if the character 'c' represents a digit by
checking if it is greater than or equal to thecharacter
Let numbers and elements be '0' and less than or equal to thecharacter '9'.
arrays of 10 integers each. This is because the ASCII codes for the digits
0,1,2,…,9 are 48 through 57 respectively.
It is NOT possible to say: It is for this reason also that we can subtract the
elements = numbers; character '0' from the character 'c' to find the
appropriate index in the array.
Instead, you have to copy one Characters used in expressions are treated as their
ASCII values, so '0' - '0' = 0, '1' - '0' = 1, etc.
element (value) at a time.

13
11/9/2022

A Grading Program

Write a program that allows me to


Task enter 5 marks, and then gives me
their average.
Use what you’ve learned about
arrays.
The output should be similar to
what’s in the next slide…

Sample User’s Screen


NB: This slide shows BOTH the user’s input, and Note:
the program’s ouput.
Each mark:
Please enter 5 marks:
is numbered sequentially.
1: 45
2: 66 is neatly aligned on its own line.
3: 24 Always remember that it is crucial that
4: 88 both
5: 32
your code and
The average mark is: 51.00 your output

Program ended with exit code: 0 are readable.

14
11/9/2022

Arrays
Arrays
Initialising Values
What does the following code snippet do?
Just like with simple variables, if the
array values are not initialised, they are
unpredictable.
int arr_digits[10], i; Why do we have to initialise the array
for (i = 0; i <= 9; i++) here but not in the example program (the
arr_digits[i] =0; one reversing numbers)?
Because in the first program, we have
It initialises all the values in the array the user enter all the values, and these
to zero. values are assigned directly to the array.

Arrays Arrays
Simultaneous Declaration & Initialisation
Simultaneous Declaration &
Initialisation
#include <stdio.h>
int main()
In C, it is also possible toinitialise an array as it is
declared.
{
int values[] = { 1,2,3,4,5,6,7,8,9 };
int numbers[10] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; char word[5] = { 'H','e','l','l','o' };
Specify the starting values of the array within curly printf("%s \n", word);
braces. }
i.e. the initial values are enclosed in braces.
Separate them with commas. Output? Xcode warning: Unused variable 'values'
Hello
Program ended with exit code: 0

15
11/9/2022

Arrays
Simultaneous Declaration &
Initialisation

You need not initialise the entire


array.
Multidimensional
int numbers[10] = {9, 8, 7, 6};
Arrays
Only the first 4 values of this array
are initialised.
The remaining 6 elements will be
set to zero.

Multidimensional Multidimensional
Arrays Arrays
The arrays we’ve covered so far are all
linear (one dimensional).
0 1 2 …. n
Think of one-dimensional arrays as a
single sequence of values. 1
We can also declare multidimensional 2
arrays …
For instance, 2D arrays are often used n
to represent grids, game boards, tables,
etc.

16
11/9/2022

Multidimensional Multidimensional
Arrays Arrays
Multi-dimensioned arrays have two or int myArray [10][10];
more index values which specify the Declares an array matrix that is a 2D
0 1 2 …. 9
element in the array. array with 10 rows and 10 columns for 1
a total of 100 elements.
multi[i][j] By convention, 2
In this example, the first dimension (index/subscript) is …
used to represent the rows, numbered
the first index value i specifies a row top to bottom starting at 0, and 9
index, whilst the second dimension (index/subscript)
is used to represent the columns,
numbered left to right starting at 0.
j specifies a column index.

Multidimensional Arrays Multidimensional Arrays


Initialising as you Declare Them Initialising as you Declare Them
Such a table could be declared and
You can also initialise a two dimensional initialised as follows:

array along with thedeclaration. int table [5][3] =


Say you wish to simultaneouslydeclare and {
initialise the following two-dimensional array
{00, 01, 02},
of integers:
{10, 11, 12}, 00 01 02
{20, 21, 22}, 10 11 12
00 01 02
20 21 22
10 11 12 This array has 5 rows {30, 31, 32},
30 31 32
20 21 22 and 3 columns. {40, 41, 42} //notice no comma
40 41 42
30 31 32 }
NB: The inner curly braces are optional but we use them for
40 41 42 our all important readability.

17
11/9/2022

Multidimensional Arrays Multidimensional Arrays


Initialising as you Declare Them Initialising as you Declare Them
You need not initialise the entire array:
int table [5][3] =
Remember C generally ignores white
space, and you don't have to write {
You are initialising only the first
each row on its own line. {00}, element of each row of the matrix.
This would still be correct (but not {10}, The rest of the elements will be set to
as readable): zero.
{20}, The inner curly braces are required
int table [5][3] = {00, 01, 02,10, {30}, here for correct initialisation.
11, 12,20, 21, 22,30, 31, 32,40, {40} //notice no comma for the final row
41, 42}
}

Multidimensional Arrays Multidimensional Arrays


User-assigned Values Example Code
If you want to use scanf() to have the user int arr_mutid1 [10][10];
enter a value for a slot in a two-dimensional int arr_mutid2 [2][2] = { {0,1}, {2,3} };
array, the call may look as follows:
Or more “readably”:
scanf("%d", &table[x][y]); int arr_mutid2 [2][2] =
{
The variables x and y here are assumed to
{0,1},
be integers in the appropriate ranges, but you
{2,3}
could also use constants or other expressions.
};
&
Don't forget the ' ' symbol. sum = arr_mutid1 [i][j] + arr_mutid2 [k][l];

18
11/9/2022

Multidimensional Arrays
Initialising as you Declare Them

00 01 02 Task: Write
Class Exercise
10 11 12 code to print
Multidimensional Arrays
20 21 22 this table on
30 31 32 your screen
exactly as is.
40 41 42

Multidimensional Arrays
Initialising as you Declare Them
Note
To C, 00, 01, and 02 are equivalent to 0,
1, and 2.
Multidimensional Arrays
These are the values that are stored, and
when you print the numbers out, you Some Practical Applications
won't see the 0 in the 10's column
Hint:
Use the 0 flag and a width of 2 in your
printf statement.

19
11/9/2022

Prac App 1 Prac App 1


Multiplication Table

Multidimensional Arrays E.g.:


Multidimensional Arrays E.g.: Multiplication Table
Multiplication Table The array might have been declared and
filled in as follows:
 int mult_table[10][10];
We created a multiplication table for (x = 0; x <= 9; x++)
when working with nested loops.
for (y = 0; y <= 9; y++)
We could have used a two-
dimensional array of integers to mult_table[x][y] = x*y;
store the multiplication table … See the next slide.
You may have to declare the variables x
and y.

20
11/9/2022

Multidimensional Arrays E.g.:


Multidimensional Arrays E.g.:
Multiplication Table
Multiplication Table
The code to print out the matrix(without row
and column headers) could look as follows: printf("%3d", mult_table[x][y]);
for (x = 0; x <= 9; x++) Note that we are specifying a width for
{ each number so that the numbers that
should be in the same columns will line
for (y = 0; y <= 9; y++) up correctly.
printf("%3d", mult_table[x][y]); The inner loop prints out one row of the
printf("\n"); matrix, and a newline character is
printed after each row.
}

Multidimensional Arrays E.g.:


Multiplication Table Prog Output

Prac App 2

21
11/9/2022

Example of Multidimensional Array

Prac App 2
Board Games

Multidimensional Arrays E.g.:


Multidimensional Arrays E.g. Draughts Prog

One common use for


two-dimensional arrays
is to represent game
boards for games such
as Chess or Checkers
(aka Draughts).

22
11/9/2022

Multidimensional Arrays E.g.: Multidimensional Arrays E.g.:


Draughts Prog Draughts Prog

How would The declaration


you for such an array
declare may be:
such an char board[8][8];
array?

Multidimensional Arrays E.g.:


Draughts Prog Multidimensional Arrays E.g.:
Draughts Prog

Possible values for each slot in the board might be Of course, if you prefer,
'r' for a normal red checker, you could also use an
integer array, and use
'R' for a red king, specific integers to
'b' for a normal black checker, represent the different
types of possible pieces.
'B' for a black king,
and some other default value to represent empty
spaces.

23
11/9/2022

Multidimensional
Multidimensional Arrays
Arrays
It is possible to have arrays with more than
2D arrays can also be used for two dimensions.
representing structures such as You can have three-dimensional arrays, four-
spreadsheets. dimensional arrays, etc.
They are also commonly used Practical Example of a 3D array:
in platform video games (e.g. A 5 storey parking area with many slots on
Super Mario) to represent each floor for cars.
terrain or screen. To locate a car you’d need the floor number
along with the row & column number at
that floor.

Multidimensional Arrays
Multidimensional Arrays

Visualise a:
1D array as a row of data
2D array as a table of data/matrix/spreadsheet
3D array as a stack of tables
We use
3 nested loops to process 3D arrays
4 nested loops to process 4D arrays
Etc

int arr[4] float arr_2d[2][3]


int arr_3d[4][3][2]

24
11/9/2022

Multidimensional
Arrays

>= 3 dimensional arrays are not


How Arrays Are
commonly used in real-time applications. Stored In Memory
We won't be using them in this class.

How Arrays Are How Arrays Are


Stored In Memory Stored In Memory
Remember that a variable is reallya named One consecutive slot of memory is allotted.
memory location.
For instance, for an integer array of size 10 on a
Each type of variable has a certain size, or system which uses 4 byte integers, 40 bytes will be
amount of memory, that it is allotted. allotted.

If the address of the first value in the array (e.g.


On most modern systems, integers are allotted 4 numbers[0] in the example program) is located at
bytes (32 bits). x, then the second value will be located at x+4, the
third at x+8, etc.
When an array is declared, enough memory is
allotted for the entire array.

25
11/9/2022

How Arrays Are How Arrays Are


Stored In Memory Stored In Memory
More generally, the following expression holds true:
C provides the operator s
" izeof"
element address = array address + (sizeof(element)
* index) You can use it to determine the size in bytes of a
variable type or of a variable itself.
E.g. assuming the array’s starting at address 0 and the
size of an integer is 4bytes; E.g. if we are dealing with a system that uses 4
Element 1’s address= 0+(4
*0) = 0 byte integers, sizeof(int) will equal 4

Element 2’s address= 0+(4


*1) = 4 If x is declared to be an integer, thansizeof(x)
will equal 4 as well.
Element 3’s address= 0+(4
*2) = 8, etc.

How Multidimensional Arrays How Multidimensional Arrays


Are Stored In Memory Are Stored In Memory

So, the element in a particular row and


How is a two-dimensional array stored in column is stored at the address given by
memory? the following formula:
The entire two dimensional array has its values
element address = array address +
stored consecutively.
(sizeof(element) * (row index *
The values in the row 0 (top row) are stored number of columns + column index))
first (from left to right), then the values in row
1 (the second row), etc. Why?

26
11/9/2022

How Multidimensional Arrays How Multidimensional Arrays


Are Stored In Memory Are Stored In Memory
element address = array address +
(sizeof(element) * element address = array address + (sizeof(element)
*
(row index * number of columns + column index)) (row index * number of columns + column index))
Let’s take an example:int myArray [2] [3]
The column index is the number of values in the
The row index multiplied by the number of columns is current row of the two-dimensional arrayto the left of the
the number of values in the two-dimensional array in current element, and they are stored first.
all rows above the current row
, and they are stored
For our 2nd item in the second row:
first.
So if you are looking at the 2nd item in the second column index= 1 thus there is 1 value in the
row: current row to the left of the current element

Row index 1 * 3 columns = 3 values in


the row above
the current row which are stored first.

How Multidimensional Arrays How Multidimensional Arrays


Are Stored In Memory Are Stored In Memory

element address = array address + (sizeof(element)


*
int myArray [2] [3]
(row index * number of columns + column index))

So the expression "row


index * number of columns + In our example, assuming we are starting at address 0
column index" is the number of elements in the two-dimensional using 4B ints:
array stored before the current element.
element address = array address + (sizeof(element)
*
(1 * 3 + 1) = 4 elements are stored before the current
element
(row index * number of columns + column index))
Each one takes up sizeof(element) bytes.

So the formula takes the starting address of the array and adds the The address of the 2nd element in the 2nd row will be:
number of bytes which is the offset to the current element.
0 + (4) *(1 * 3 + 1) = 16

27
11/9/2022

Arrays
Accessing Out of Bounds Elements

At times you may try to access an array with an index


that is out of bounds.
For our numbers[10] array of 10 integers, what would
Arrays be the the appropriate indices for the array?

The Out of Bounds Error They range from 0 to 9.


If you try to access numbers[10], or numbers[100] the
program does not complain;
it will access the value at the memory location given
by our formula:
element address = array address +
(sizeof(element) * index)

Arrays Arrays
Accessing Out of Bounds Accessing Out of Bounds
Elements Elements
However,
if we are reading the value, it will be
unpredictable, and The “array is out of bounds” error is a
runtime error.
if we are assigning a value, we will be writing
to memory that isn't owned by this array. The compiler will not detect this error
It will cause an error sooner or later unless you
It has no idea you are assigning/accessing
are extremely lucky!
something greater than the array’s size.
Sometimes this type of bug is difficult to track
down.

28
11/9/2022

Arrays
Accessing Out of Bounds
Elements
An out of bounds error can:
cause your program to crash
cause your program to produce inconsistent
results (with garbage data that is meaningless
in the array).
It is up to you (the programmer),not the
compiler, to be super careful when
accessing the array index values in use and/or
writing data to the array with the index value.

29

You might also like