Lecture 5 - Arrays
Lecture 5 - Arrays
collection of books
the others
2
A scenario
We can represent your bookshelf in another manner
A dance
A game of A clash of A storm of A feast for
with
thrones kings swords crows
dragons
1 2 3 4 5
3
A scenario
A dance
A game of A clash of A storm of A feast for
with
thrones kings swords crows
dragons
1 2 3 4 5
Say you want to give a rating to each book based on how much you like it
1 2 3 4 5
4
How do we translate this into
programming?
5
A scenario
What if there were 100 books? What if there were 1000 books?
6
Arrays
7
Arrays
0 1 2 3 4
8
Arrays
book_ratings
0 1 2 3 4
9
Arrays
Declaration of arrays
type identifier[count];
0 1 2 3 4
float book_ratings[5];
10
Arrays
Different ways to declare arrays
int n = 3;
int array_1[n]; // The size of the array can be a variable
int n;
scanf(“%d”, &n);
int array_1[n]; // User can input the size the size of the array
11
Pop Quiz
12
Arrays
int array_1[++y];
int array_2[x * y + 3];
int array_2[x * x / y];
int array_3[0];
13
Arrays
14
Arrays
0 1 2 3 4
15
Arrays
Accessing elements in an array
array_1
0 1 2 3 4
array_1[0] = 10;
array_1 10
0 1 2 3 4
4 4 3
0 1 2 3 4
17
Arrays
Using loops to iterate through an array
int marks[5], i;
for(i = 0; i < 5; i++)
marks[i] = 0; // Setting each element to 0
19
Arrays
array_1 4 0 3 4 2
0 1 2 3 4
int x = 3, y = 2, i = 0;
array_1[x % y] = x;
array_1[++x] = x;
while(i < 5)
array_1[++i] = 0
20
Arrays
array_1 4 0 3 4 2
0 1 2 3 4
int x = 3, y = 2, i = 0;
array_1[x % y] = x; // Sets the second element to 3
array_1[++x] = x; // Sets the fourth element to 3
while(i < 5)
array_1[++i] = 0; // Sets all the elements to 0
21
Arrays
22
Array initialization
23
Array initialization
● If no value is given, it initially holds garbage values
● Most common → List of constants enclosed in braces, separated by
comma
int a[6] = {1, 4, 4, 4, 0, 1};
● Initialization can be shorter than array
int a[6] = {1, 4, 4}; // {1, 4, 4, 0, 0, 0}
● Initialize array with the same number
int a[6] = {0}; // {0, 0, 0, 0, 0, 0}
● Length of an array can be omitted
int a[] = {1, 4, 3, 2, 1}; // Size of array is 5 24
Multidimensional arrays
25
Multidimensional arrays
In C programming, you can create an array of arrays. These arrays
are known as multidimensional arrays.
26
Multidimensional arrays
4-D plot
27
Multidimensional arrays
A simple 2-dimensional array
int a[2][5];
Imagine an array of size 5 containing arrays each of size 2
0 1 2 3 4
0 0 0 0 0
1 1 1 1 1
28
Multidimensional arrays
It has 2 rows and 5 columns, rows and columns are indexed from 0
0 1 2 3 4
29
Multidimensional arrays
31
Multidimensional arrays
Nested loops → Ideal for processing multidimensional arrays
32
Multidimensional arrays
int N = 3;
int identity[N][N], row, col;
for(row = 0; row < N; row++)
for(col = 0; col < N; col++)
if(row == col)
identity[i][j] = 1;
else
identity[i][j] = 0;
33
Multidimensional arrays
● Nesting one-dimensional initializers
int m[2][3] = {{1, 1, 1},
{0, 1, 0}};
● If initializer is shorter than array, rest is filled with zeros
int m[2][3] = {{1, 2, 1},
{1}}; // {{1, 2, 1}, {1, 0, 0}}
● Can omit inner braces → Risky due to extra/missing elements
34
Variable length array
35
Variable length array
● Length of array is computed during program execution
int m, n;
scanf("%d %d", &m, &n);
int arr[m][n];
● Reduces wastage of memory
● Cannot have initializers
● Major cause of stack overflow (No need to understand)
● Not available in standard C++ or many other languages
36
Reading Tasks
● C Programming - A Modern Approach | Chapter 8
37
Thank You
38