Lecture 09 Arrays
Lecture 09 Arrays
Arrays
Nishatnagar, Turag,
Dhaka-1230, Bangladesh
1 Arrays
2 Two-Dimensional Array
3 Programming Problems
4 Summary
2
Arrays
3
Declaring Arrays
• An array is a data structure that contains elements of the same data type.
• To declare a one-dimensional array, you must specify its name, the number of its
elements, and their data type, like this:
• The elements of an array can be of any type, while their number must be enclosed
in brackets. For example, the statement -
After declaring an array, you can’t change the number of its elements; it remains fixed.
4
Declaring Arrays
• The array’s length is specified by an integer constant expression; however, it is a
good practice to use a macro instead.
• If you ever need to change it, you only need to change the value of the constant.
For example,
#d e f i n e SIZE 150
f l o a t a r r [ SIZE ] ; /∗ The c o m p i l e r r e p l a c e s SIZE w i t h 150 and c r e a t e s an
a r r a y o f 150 f l o a t s . ∗/
The maximum memory size that can be allocated for an array depends on the available
stack size. A program with large arrays may not run in your computer unless the
available stack size is large enough to hold the values.
To avoid useless waste of memory, don’t declare an array with more length than
needed. 5
Accessing Array Elements
• In an array of n elements, the first one is stored in position [0] , the second one in
position [1] , and the last one in [n−1].
• For example, following statement declares the array grd with 1000 elements of
type float , named grd [0], grd [1], ..., grd[999].
f l o a t grd [ 1 0 0 0 ] ;
C does not check if the index goes out of the array bounds. It is the programmer’s
responsibility to assure that this won’t happen. If it does, the program may behave
unpredictably.
Once more, be careful when you assign a value to an array element because exceeding
the array bounds may cause abnormal program operation.
6
Accessing Array Elements
Don’t forget that the indexing of an array of n elements starts from 0 (not 1) up to
n−1.
7
Array Initialization
Like ordinary variables, the elements of an array can be initialized when it is declared.
If the initialization list is shorter than the number of the elements, the remaining
elements are set to 0.
If the array’s length is omitted, the compiler will create an array with length equal to
the number of the values in the list.
To declare an array whose elements can’t change during program execution, start its
declaration with the keyword const. In that case, the array must be initialized.
9
Practice Problems
What would be the values of the array arr in the following program?
#i n c l u d e < s t d i o . h>
i n t main ( )
{
i n t i , arr [ 1 0 ] = {0};
f o r ( i = 0 ; i < 1 0 ; i ++)
a r r [ i ++] = 2 0 ;
return 0;
}
10
Practice Problems
Write a program that declares an array of 5 elements and uses a loop to assign the
values 1.1, 1.2, 1.3, 1.4, 1.5 to them. Next, the program should display the array’s
elements in reverse order.
11
Outlines
1 Arrays
2 Two-Dimensional Array
3 Programming Problems
4 Summary
12
Two-Dimensional Array Initialization
i n t a r r [ 3 ] [ 3 ] = {{10 , 20 , 30} ,
{40 , 50 , 60} ,
{70 , 80 , 90}};
i n t a r r [ 3 ] [ 3 ] = {10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90};
13
Two-Dimensional Array Initialization
If the initialization list is shorter than the number of the row’s elements, the remaining
elements are set to 0.
14
Two-Dimensional Array Initialization
If we omit the inner braces and the initialization list is shorter than the number of the
array elements, the remaining elements are set to 0.
The number of columns must always be present. However, the number of rows is
optional. If you don’t specify it, the compiler will create a two-dimensional array based
on the initialization list.
i n t a r r [ ] [ 3 ] = {10 , 20 , 30 , 40 , 50 , 60}; /∗ S i n c e t h e a r r a y a r r h a s 3
c o l u m n s and t h e i n i t i a l i z a t i o n v a l u e s a r e 6 , t h e c o m p i l e r c r e a t e s a
two−d i m e n s i o n a l a r r a y o f 2 r o w s and 3 c o l u m n s . ∗/
15
Two-Dimensional Array Initialization
#i n c l u d e < s t d i o . h>
i n t main ( )
{
int i , j , arr [50][100];
f o r ( i = 0 ; i < 5 0 ; i ++)
f o r ( j = 0 ; j < 1 0 0 ; j ++)
arr [ i ] [ j ] = 1;
return 0;
}
16
Practice Problems
Write a program that creates an identity 6x6 array and displays its elements as an
identity 6x6 matrix in algebra form. (Note: In math, an identity matrix has 1’s on the
main diagonal’s elements and 0’s everywhere else.
Write a program that reads 8 integers, stores them in a 2x4 array, and displays the
array elements in reverse order, from the lower-right element to the upper-left one.
17
Outlines
1 Arrays
2 Two-Dimensional Array
3 Programming Problems
4 Summary
18
Home Works
Write a program that declares an array of 5 integers and assigns the values 10, 20, 30,
40, and 50 to its elements. Next, the program should display the elements greater than
20.
What would be the values of the array arr in the following program?
#i n c l u d e < s t d i o . h>
i n t main ( )
{
i n t i , a r r [ 3 ] = {0 , 1 , 2};
f o r ( i = 1 ; i < 4 ; i ++)
a r r [ a r r [ a r r [3− i ] ] ] = i −1;
}
19
Home Works
Write a program that declares an array of 10 integers, assigns random values from 0 to
20 to its elements, and displays their average.
Write a program that reads 100 integers and stores them in an array. Then, the
program should rotate the elements one place to the right. For example, if the array
were 1, −9, 5, 3, the rotated array would be 3, 1, −9, 5.
20
Outlines
1 Arrays
2 Two-Dimensional Array
3 Programming Problems
4 Summary
21
Summary
• Array Declaration
• Array Initialization
• One-Dimensional Arrays
• Two-Dimensional Arrays
22
Thank You!