pps_Array
pps_Array
1 2
Array Arrays
An array is an ordered list of values
An array is a collection of data elements that are of the
same type (e.g., a collection of integers, collection of The entire array Each value has a numeric index
has a single name
characters, collection of doubles). 0 1 2 3 4 5 6 7 8 9
scores 79 87 94 82 67 98 87 81 74 91
Arrays are referred to as structured data types. An array is
defined as finite, ordered collection of homogenous data, An array of size N is indexed from zero to N-1
stored in contiguous memory locations. This array holds 10 values that are indexed from 0 to 9
3 4
12/4/2023
scores[2]
where N is the number of dimensions.
refers to the value 94 (the 3rd value in the array)
5 6
Example
int m;
int array[m]; Wrong
The C arrays are static in nature, i.e., they are allocated memory at the
compile time.
7 8
12/4/2023
9 10
When the array is declared or allocated memory, the elements of the Use an initializer list to initialize multiple elements of the array.
array contain some garbage value. So, we need to initialize the array
An initializer list is the list of values enclosed within braces { }
to some meaningful value.
separated b a comma.
1. Array Initialization with Declaration data_type array_name [size] = {value1, value2, ... valueN};
11 12
12/4/2023
Array Initialization with Declaration Array Initialization with Declaration without Size
data_type array_name [size] = {value1, value2, ... valueN}; If we initialize an array using an initializer list, we can skip
declaring the size of the array as the compiler can
automatically deduce the size of the array in these cases.
13 14
the value to each element of the array. // array initialization using initializer list without specifying
size
int arr1[] = { 1, 2, 3, 4, 5 };
for (int i = 0; i < N; i++)
// array initialization using for loop
{
float arr2[5];
array_name[i] = valuei; for (int i = 0; i < 5; i++) {
} arr2[i] = (float)i * 2.1;
}
return 0; }
15 16
12/4/2023
Example
int myNumbers[] = {25, 50, 75, 100};
printf("%d", myNumbers[0]);
// Outputs 25
17 18
// accessing element at index 4 i.e last element int myNumbers[] = {25, 50, 75, 100};
printf("Element at arr[4]: %d\n", arr[4]); Element at arr[0]: 15
myNumbers[0] = 33;
printf("%d", myNumbers[0]);
// accessing element at index 0 i.e first element
printf("Element at arr[0]: %d", arr[0]);
// Now outputs 33 instead of 25
return 0; }
19 20
12/4/2023
21 22
23 24
12/4/2023
25 26
Example Example
// C Program to demonstrate the use of array
#include <stdio.h> // C Program to demonstrate the use of Output
array
Elements in Array: 10 20 100 40 50
int main() #include <stdio.h>
{ int main()
// array declaration and initialization {
int arr[5] = { 10, 20, 30, 40, 50 }; // array declaration and
initialization
int arr[5] = { 10, 20, 30, 40, 50 };
// modifying element at index 2
arr[2] = 100; // modifying element at index 2
arr[2] = 100;
// traversing array using for loop // traversing array using for loop
printf("Elements in Array: "); printf("Elements in Array: ");
for (int i = 0; i < 5; i++) { for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]); printf("%d ", arr[i]);
}
}
return 0; }
return 0; }
27 28
12/4/2023
29 30
• It is easy to apply the search process. • A more efficient way to store multiple values of the same type.
• As arrays create a single array of multiple elements at once, you need to • In C, you can use various built-in functions, such as searching and
use fewer lines of code. This results in cleaner and more optimized code. sorting, to manipulate arrays.
• Sorting code is easier as you have to write just a few lines of code.
31 32
12/4/2023
33 34
35 36
12/4/2023
Two-Dimensional Arrays
A Two-Dimensional array or 2D array in C is an array that has
exactly two dimensions.
Multidimensional Arrays They can be visualized in the form of rows and columns organized
in a two-dimensional plane.
array_name[size1] [size2];
Here,
•size1: Size of the first dimension.
•size2: Size of the second dimension.
37 38
The first dimension represents the number of rows [2], while the int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
second dimension represents the number of columns [3]. The values are printf("%d", matrix[0][2]);
placed in row-order, and can be visualized like this:
// Outputs 2
39 40
12/4/2023
• Example • Example
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
int matrix[2][3] = { {1, 4, 2}, {3, 6, 8} };
matrix[0][0] = 9;
int i, j;
printf("%d", matrix[0][0]);
for (i = 0; i < 2; i++) {
// Now outputs 9 instead of 1 for (j = 0; j < 3; j++) {
printf("%d\n", matrix[i][j]);
This statement change the value of the element in the first }
row (0) and first column (0).
}
41 42
43 44
12/4/2023
Examples
/* Valid declaration*/
int abc[2][2] = {1, 2, 3 ,4 }
/* Valid declaration*/
int abc[][2] = {1, 2, 3 ,4 }
/* Invalid declaration – you must specify second dimension*/
int abc[][] = {1, 2, 3 ,4 }
/* Invalid because of the same reason mentioned above*/
int abc[2][] = {1, 2, 3 ,4 }
45