Arrays (Incomplete)
Arrays (Incomplete)
Objectives
General Objective
To use one dimensional and multidimensionalarray
variables
Arrays in C Specific Objectives:
To
declare them
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 the 10
different integers,
to write a program that will let the user enter 10
integers e.g. 2 4 6 8 … 10 scanf statements to have the user enter
and then print them out in the reverse order that they values for them, and then
were entered i.e. … 8 6 4 2 …
10 printf statements to print them out in
How will you do this? reverse order.
1
3/2/2023
Arrays
Arrays A diagrammatic representation
Arrays
Arrays
2
3/2/2023
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
the array name (identifier) of type char.
the array size (number of values you The name of the array isarr_names.
will store).
Arrays Arrays
Declaring Them Accessing the Values
3
3/2/2023
Arrays Arrays
Accessing Their Elements Accessing the Values
(Array name /
arr_names identifier)
We use the following array syntax:
Elements array_name[i]
[0] [1] [2] [3] [4] Indices / In C, array elements start at position (index) 0.
subscript Square brackets are used to access eachindexed value (aka
element).
Each element can be accessed by a combination of thearray’s Thus:
name and its position in the array (subscript/index).
arr_student_age[5]
BEWARE the off-by-one error; we start from index 0 (not 1).
refers to the sixth element in an array calledarr_student_age.
Arrays Arrays
Accessing Their Elements Accessing Their Elements
int numbers[10]; To access the separate values in this array, you must use
the name of the array followed by
We are declaring an array of 10 the index (subscript) of the element you want to access
integers. within square brackets.
The first element of the array has index 0.
The name of the array is So the numbers array has 10 elements with indexes 0
numbers. through 9.
4
3/2/2023
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.:
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.
marks[4] = 0;
5
3/2/2023
Arrays Arrays
Assigning Values Assigning Values
Arrays Arrays
Assigning Values Declaring Them
Assigning values to array elements: Arrays may consist of any of the valid
numbers[8] = mark; data types.
6
3/2/2023
Output Arrays
Processing Them
The first child's age is 10 years.
The second child's age is 11 years.
The third child's age is 9 years. C does NOT allow single operations involving 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 anelement by
Program ended with exit code: 36 element basis.
7
3/2/2023
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) at a time.
i.e. even if they have the same data type, same
Thus, any such operations must be carried out onan
dimensionality and same size.
element by element basis.
Let numbers and elements be arrays of 10 integers each.
We usually accomplish this within aloop.
It is NOT possible to say:
Each pass through the loop is used to process one array
elements = numbers; 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 }
The condition takes into account the fact
writing data to those elements.
that the numbers array has 10 elements.
8
3/2/2023
9
3/2/2023
Arrays Arrays
Accessing Their Elements – The Example Accessing Their Elements – The Example Explained
Explained
Arrays Arrays
Accessing Their Elements – The Example Explained Accessing Their Elements – The Example Explained
10
3/2/2023
Arrays
Accessing Their Elements – The Example Explained
Arrays
If you wrote: Accessing Their Elements – The Example Explained
Arrays Arrays
Accessing Their Elements – The Example Explained Accessing Their Elements – The Example Explained
11
3/2/2023
Arrays Arrays
Accessing Their Elements – The Example Explained
Accessing Their Elements – A 2nd
printf("%d", numbers[SIZE - 1]); Example:
for (num1 = SIZE - 2; num1 >= 0; num1--)
printf(", %d", numbers[num1]);
printf("\n");
The following programme reads in
In this for loop, notice that unlike our first version of this program, we are text one character at a time until an
now starting num1 at
EOF is read and keeps track of how
SIZE - 2
instead of many times each digit (0 through 9) is
SIZE - 1 encountered.
Why?
We’ve already printed out thearray’s last element.
#include <stdio.h>
Arrays
Accessing Their Elements – A 2nd
Arrays
int main(void)
{
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++)
arr_digits[x] = 0; Just like with simple variables, if the array values are not
while ((c = getchar()) != EOF) initialised, they are unpredictable.
{
if ((c >= '0') && (c <= '9')) Why do we have to initialise the array here but not in the
++arr_digits[c -'0']; first program?
}
for (x = 0; x <= 9; x++) Because in the first program, we have the user enter all
printf("%d\n", arr_digits[x]); the values, and these values are assigned directly to the
return 0; array.
}
Output:
12
3/2/2023
Arrays Arrays
Initialising Values Assigning Values
Arrays Arrays
Accessing Their Elements – A 2nd Example
Assigning Values Explained:
elements = numbers; the character 'c' to find the appropriate index in the array.
Characters used in expressions are treated as their ASCII values, so
'0' - '0' = 0, '1' - '0' = 1, etc.
Instead, you have to copy one element
(value) at a time.
13
3/2/2023
A Grading Program
Write a program that allows me to
Assignment/ enter 5 marks, and then gives me their
Homework
average.
Use what you’ve learned about arrays.
The output should be similar to
what’s in the next slide…
1: 45 is numbered sequentially.
2: 66 is neatly aligned on its own line.
3: 24
4: 88 Always remember that it is crucial that both
5: 32 your code and
14
3/2/2023
Arrays
Arrays
Initialising Values
What does the following code snippet do? Just like with simple variables, if the array
v a l u e s a r e n o t i n i t i a l i s e d, t h e y a r e
unpredictable.
int arr_digits[10], i; Why do we have toinitialise the array here
for (i = 0; i <= 9; i++) but not in the example program (the one
arr_digits[i] = 0; reversing numbers)?
Because in the first program, we have the
It initialises all the values in the array to zero. user enter all the values, and these values
are assigned directly to the array.
15