Arrays
CSE 4373: Computer Programming and Applications
Ajwad Abrar
Junior Lecturer, CSE
A scenario
● Imagine you have a massive
collection of books
● You’ve kept them in your shelf
● Each book has a number to
identify it and separate it from
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
● Each item in the bookshelf is a book but each book it different
● We can assign numbers to each item to identify them
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
4.7 4.4 4.2 4.3 4.8
1 2 3 4 5
4
How do we translate this into
programming?
5
A scenario
A naive approach would be declaring 5 different variables and
assigning the rating values to each variable
float book_1 = 4.7, book_2 = 4.4, book_3 = 4.2 ... ;
What if there were 100 books? What if there were 1000 books?
There must be a better way to do this
6
Arrays
An array is a sequence of objects of the same type that occupy a
contiguous area of memory
Sequence of objects → Collection of items in a shelf
Same type → All items are books
Contiguous area of memory → Books stored one after the other
7
Arrays
● Arrays are data structures containing many values of the same
type
● Each value / element in an array can be accessed individually
using an unique number based on their position
● These unique numbers are called indexes
A dance
A game of A clash of A storm of A feast for
with
thrones kings swords crows
dragons
0 1 2 3 4
8
Arrays
● Arrays can be considered as a list containing many items
● Each item is called an element of the array
● All elements are of the same data type (int, float)
● Each element can be accessed by using their index
● Indexing starts from 0
book_ratings
0 1 2 3 4
9
Arrays
Declaration of arrays
type identifier[count];
int array with 5 elements can be declared as
int prices[5]; prices
0 1 2 3 4
float array with 5 elements can be declared as
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
What is the size of the array?
int x = 2, y = 4;
int array_1[++y];
int array_2[x * y + 3];
int array_2[x * x / y];
int array_3[0];
13
Arrays
What is the size of the array?
int x = 2, y = 4;
int array_1[++y]; // Size of the array is 5
int array_2[x * y + 3]; // Size of the array is 11
int array_2[x * x / y]; // Size of the array is 1
int array_3[0]; // This is not allowed
14
Arrays
How to access an element in an array?
identifier[index]
a a[0] a[1] a[2] a[3] a[4]
0 1 2 3 4
Think of an array having a collection of variables each holding
different data of the same type
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
printf(“%d”, a[1]); scanf(“%d”, &a[1]);
16
Arrays
Accessing elements in an array
int book_ratings[3];
book_ratings[0] = 4.3; // will be truncated to integer
book_ratings[1] = 4;
book_ratings[2] = 3.7; // will be truncated to integer
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
for(i = 0; i < 5; i++)
scanf(“%d”, &marks[i]);
18
Pop Quiz
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
Reverse a series of numbers
● Prompt the user to enter 10 numbers
● Store them in an array
● Print them in reverse order
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
m[i][j] → Access jth column of ith row
29
Multidimensional arrays
In memory, they are stored as flat one-dimensional array
Do not write m[i][j] as m[i, j]
30
Pop Quiz
31
Multidimensional arrays
Nested loops → Ideal for processing multidimensional arrays
Create an identity matrix
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