0% found this document useful (0 votes)
4 views23 pages

Lecture 09 Arrays

The document provides an introduction to arrays in programming, focusing on one-dimensional and two-dimensional arrays. It covers array declaration, initialization, accessing elements, and includes practice problems and homework assignments related to arrays. Key concepts include the fixed size of arrays, the importance of proper indexing, and methods for initializing arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views23 pages

Lecture 09 Arrays

The document provides an introduction to arrays in programming, focusing on one-dimensional and two-dimensional arrays. It covers array declaration, initialization, accessing elements, and includes practice problems and homework assignments related to arrays. Key concepts include the fixed size of arrays, the importance of proper indexing, and methods for initializing arrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Introduction to Computer & Programming Techniques

Arrays

Md. Jahidul Islam


BGMEA University of Fashion and Technology

Nishatnagar, Turag,
Dhaka-1230, Bangladesh

March 29, 2023


Outlines

1 Arrays

2 Two-Dimensional Array

3 Programming Problems

4 Summary

2
Arrays

• The variables we’ve used so far can store a single value.


• In this lecture, we’ll talk about a new type of variable capable of storing a number
of values. This type is called array.
• An array may be multidimensional. We’ll focus on the simplest and most usual
kinds; the one-dimensional and two-dimensional arrays.
• To introduce you to arrays, we’ll show you how to use arrays of integers and
floating-point numbers.

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:

data type array name [ number of elements ] ;

• The elements of an array can be of any type, while their number must be enclosed
in brackets. For example, the statement -

int arr [1000];

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

An array element can be used in the same way as an ordinary variable.

int i , j , arr [10];


a r r [ 0 ] = 2 ; /∗ The v a l u e o f t h e f i r s t e l e m e n t becomes 2 . ∗/
a r r [ 9 ] = a r r [ 0 ] ; /∗ The v a l u e o f t h e l a s t e l e m e n t becomes e q u a l t o t h e
v a l u e o f t h e f i r s t e l e m e n t . ∗/
i = j = a r r [ 0 ] ; /∗ The v a l u e s o f i and j become e q u a l t o t h e v a l u e o f
t h e f i r s t e l e m e n t , t h a t i s 2 . ∗/
a r r [ i+j ] = 3 0 0 ; /∗ S i n c e i+j = 2+2 = 4 , t h e v a l u e o f t h e f i f t h e l e m e n t
becomes 3 0 0 . ∗/

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.

int arr [4] = {10, 20, 30, 40;}

If the initialization list is shorter than the number of the elements, the remaining
elements are set to 0.

int arr [10] = {10, 20};

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.

int arr [ ] = {10, 20, 30, 40};

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.

const int arr [ ] = {10, 20, 30, 40};


8
Practice Problems

What would be the values of the array a 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 [ 3 ] = {4 , 2 , 0} , b [ 3 ] = {2 , 3 , 4};
f o r ( i = 0 ; i < 3 ; i ++)
a [ b [ i ]−a [2− i ]] ++;
return 0;
}

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

What is the output of 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 , 2 0 , 1 0 , 0 , −10 , −20 , −30};
f o r ( i = 0 ; a r r [ i ] ; i ++)
p r i n t f ( ”%d\n” , a r r [ i ] ) ;
return 0;
}

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

• Like one-dimensional arrays, a two-dimensional array can be initialized when


declared.
• A common initialization method is to use the = operator and put the values of
the elements of each row inside braces { }.

i n t a r r [ 3 ] [ 3 ] = {{10 , 20 , 30} ,
{40 , 50 , 60} ,
{70 , 80 , 90}};

• Alternatively, we can omit the inner braces and write -

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.

i n t a r r [ 3 ] [ 3 ] = {{10 , 20} , // The v a l u e s o f a r r [ 0 ] [ 2 ] , a r r [ 1 ] [ 2 ] ,


{40 , 50} , // a r r [ 2 ] [ 1 ] , and a r r [ 2 ] [ 2 ] a r e s e t t o 0 .
{70}};

If we omit the initialization of a row, its elements are set to 0.

i n t a r r [ 3 ] [ 3 ] = { { 1 0 , 2 0 , 3 0 } } ; /∗ The e l e m e n t s o f t h e 2 nd and 3 r d row


a r e s e t t o 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.

i n t a r r [ 3 ] [ 3 ] = {10 , 20}; /∗ The v a l u e o f a r r [ 0 ] [ 0 ] becomes 1 0 , a r r


[ 0 ] [ 1 ] becomes 2 0 , and a l l t h e r e s t e q u a l t o 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

Another usual initialization method is to use a pair of nested for loops.

#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!

You might also like