Practice Session 3 - Arrays in C & Applications
wethanhcong
5 views
34 slides
Oct 22, 2025
Slide 1 of 34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
About This Presentation
This session focuses on understanding the concept and usage of arrays in the C programming language. Students will learn how to declare, initialize, and manipulate one-dimensional and two-dimensional arrays for storing and processing data efficiently. The session includes practical exercises such as...
This session focuses on understanding the concept and usage of arrays in the C programming language. Students will learn how to declare, initialize, and manipulate one-dimensional and two-dimensional arrays for storing and processing data efficiently. The session includes practical exercises such as performing operations on arrays (searching, sorting, and summing elements), handling matrices, and developing simple applications like statistical computations and data storage programs.
Size: 118.64 KB
Language: en
Added: Oct 22, 2025
Slides: 34 pages
Slide Content
Nguyễn Trung Hiếu
Practice Session III
IT1016 – 738793
Arrays
Introduction
A fixed-size collection of similar data items stored in
contiguous memory locations.
int student1
int student2
… ————> int students[]
int student49
int student50
Declaration
One-dimensional array
type arr[size];
Example
int students[5];
Declaration → Memory
Allocation = Array Size * Element Size
Example
int students[5];
1 int = 4 byte
students = 20 bytes
Declaration → Index
Index is used to access array elements: array[index]
It ranges from 0 to size - 1
Example
int students[5];
students[0] = 10;
Initialisation
Initialisation after Declaration
type arr[size];
array[0] = val1;
…
array[size - 1] = valN;
Initialisation
Initialisation after Declaration: Example
int students[5];
students[0] = 234;
…
students[4] = 532;
Initialisation
Initialisation after Declaration using Loop
for (int i = 0; i < N; i++) {
arr[i] = value_i;
}
Initialisation
Initialisation after Declaration using Loop: Example
int N = 5, numbers[N];
for (int i = 0; i < N; i++) {
numbers[i] = i;
}
Initialisation
Initialisation with Declaration
type arr[size] = {v1, v2, … vN};
Example
int students[5] = {23, 12, 45, 29,
33};
Initialisation
Initialisation with Declaration without Size
type arr = {v1, v2, … vN};
Example
int students = {23, 12, 45, 29, 33};
→ students has 5 elements.
Access and Traversal
Using index
int N, arr[N];
for (int i = 0; i < N; i++) {
arr[i];
}
Common Tasks
Read an array from input
A form of initialisation after declaration
int n; scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
Read an array from input
Fixed-size array
int n; scanf("%d", &n);
int arr[m]; // m >> n
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
Print an array
A form of traversal using index
int students[5] = {234, … , 665};
for (int i = 0; i < 5; i++) {
printf("%d ", students[i]);
}
Change values in an array
Index
int numbers[5] = {1, … , 5};
for (int i = 0; i < 5; i++) {
numbers[i] *= 2;
}
Print an array
A form of traversal using index
int students[5] = {234, … , 665};
for (int i = 0; i < 5; i++) {
printf("%d", students[i]);
}
Find max value in array
Set max = arr[0]
Loop from i = 1 to N-1 do
If arr[i] > max:
max = arr[i]
Find min value in array
Set min = arr[0]
Loop from i = 1 to N-1 do
If arr[i] < min:
min = arr[i]
Find value in array
Set flag = 0
Loop from i = 0 to N-1 do
If arr[i] = v:
flag = 1
Break the loop
Sort value in array
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
} } }
Insert value at pos. k
Set arr[N+1], initialise from 0 to N-1
With 0 ≤ k ≤ N-1:
Loop from i = N to k+1 do
arr[i] = arr[i - 1]
arr[k] = m
Multi-dimensional arrays
Multi-dimensional arrays
Declaration
type arr[size_1][size_2] … [size_n];
Multi-dimensional arrays
Declaration of Two-dimensional array
type arr[size_1][size_2];
size1: rows size2: columns.
Multi-dimensional arrays
Initialisation of Two-dimensional array
type arr[size_1][size_2] = { … };
The number of elements in the initialiser list ≤
size_1 * size_2
Multi-dimensional arrays
Initialisation of Two-dimensional array: Example
int arr[3][4] = { 1, 2, …, 11, 12 };
1 2 3 4
5 6 7 8
9 10 11 12
Multi-dimensional arrays
Initialisation of Two-dimensional array
type arr[][size_2] = { … };
Example
int number[][2] = { {1, 2}, {3, 4},
{5, 6}, {7, 8} }
Multi-dimensional arrays
Initialisation of Two-dimensional array using Loops
type arr[size_1][size_2];
for (int i = 0; i < size_1; i++) {
for (int j = 0; j < size_2; j++)
arr[i][j] = value_i_j;
}
Multi-dimensional arrays
Read Two-dimensional Array from Input
type arr[size_1][size_2];
for (int i = 0; i < size_1; i++) {
for (int j = 0; j < size_2; j++)
scanf("%d", &arr[i][j]);
}