Download as pdf or txt
Download as pdf or txt
You are on page 1of 52

Introduction to Programming

Arrays
Phạm Nguyên Khang
Department of Computer Science
pnkhang@cit.ctu.edu.vn

5/2021
Contents
• Arrays
• Multi-dimensional arrays
• Arrays and Function
• Practice

2
Arrays

3
Arrays
• An array is a collection of data that holds fixed
number of values of same type.
• For example: if you want to store marks of 100
students, you can create an array for it.

4
Arrays
• Why array ?
• Write a program to enter n integer numbers
and print them in reverse order. n is also
entered by the user.

5
Arrays
• Why array ?
• Write a program to enter n integer numbers
and print them in reverse order. n is also
entered by the user.
– We need n variables to store integer numbers.
– We don’t know the value of n until the user enters

6
Arrays
• Declare an array

• For example:

7
Arrays
• Elements of an array

• Using [] to access an element of an array

8
Arrays
• Elements of an array
– Arrays have 0 as the first index not 1, e.g: mark[0]
– If the size of an array is n, to access the last
element, (n-1) index is used, e.g.: mark[4].

9
Arrays
• Declare and initialize an array
– It's possible to initialize an array during
declaration.

– or

10
Arrays
• Assign a value to an element of an array

11
Arrays
• Get the value of an element of an array

12
Arrays
• Write a program to enter n integer numbers
and print them in reverse order. n is also
entered by the user.
– Declare an array: int A[100]
– Enter n numbers using a loop (e.g.: for)
• scanf(“%d”, &A[i]);
– Print n number in reverse order using a loop
• printf(“%d ”, A[i]);

13
14
Arrays
• Result:

15
Arrays
• Notes:
– The first element of an element is A[0] not A[1].
– If an array has n element, the last element is A[n-
1] not A[n]
– If you access an element out of range (e.g. A[10]
while A has 10 elements) you can get a runtime
error.
– You cannot assign a whole array to another array,
use element-to-element assignment within a loop
instead!

16
Multidimensional arrays

17
Multidimensional arrays
• Write a program to enter a matrix of float
numbers and print it to the screen.

18
Multidimensional arrays
• Write a program to enter a matrix of float
numbers and print it to the screen.
• How to store a matrix ?

19
Multidimensional arrays
• How to store a matrix ?
– Using a table ~ 2D array

20
Multidimensional arrays
• How to declare a multidimensional array?

21
Multidimensional arrays
• How to access an element of a
multidimensional array?
– Using [] operator

22
Multidimensional arrays
• Write a program to enter a matrix of float
numbers and print it to the screen.

23
24
Multidimensional arrays
• Result:

25
Multidimensional arrays
• Declare and initialize a two dimensional array

26
Multidimensional arrays
• Declare and initialize a three dimensional
array

27
Practice 1
1. Write a program to find the sum of all
elements of the array.

2. Write a program to enter n integers and


counts numbers of positive numbers.
Hint:
– Use an array to store n numbers
– Use a “for” loop and “if” statement to count

28
Practice 1
3. Write a program to enter n integers and find
the largest number.
Hint:
– Use an array to store n numbers
– Set the largest to the first element
– Use “for" loop to examine remained elements

29
Practice 1
4. Write a program to enter n integers and find
the smallest number.

5. Write a program to enter n float numbers and


compute the average (mean).

30
Practice 1
6. Write a program to enter n float numbers and
compute the standard deviation (độ lệch
chuẩn).

31
Practice 1
7. Write a program to enter n integers, store
them into an array A, copy the elements this
array into another array B, print elements of
array B.
Hint:
– Use a loop to copy every element of array A into
array B

32
Practice 1
8. Write a C program to print all negative
elements in an array.
9. Write a C program to find second largest
element in an array.
10. Write a C program to count total number of
even and odd elements in an array.
11. Write a C program to put even and odd
elements of array in two separate array.

33
Practice 1
12. Write a C program to print insert a value v at
a position i of an array A.

Hint:
– Move elements from i to n to the right
– Assign v to A[i]

34
Practice 1
13. Write a C program to print delete a value at
position i of an array A.

Hint:
– Move elements from i+1 to n to the left

35
Practice 1
14. Write a C program to add two matrices.
15. Write a C program to multiply two matrices.
16. Write a C program to find sum of main
diagonal elements of a matrix.

36
Arrays and Function

37
Arrays and Function
• Passing an element of an array to a function
– An element is equivalent to a single variable

38
Arrays and Function
• Passing an entire 1D array to a function
– Array as a parameter: the size of array can be
omitted

– Calling function: only name of array is passed

39
40
Arrays and Function
• Passing an entire multidimensional array to a
function
– Only name of array is passed
– Where declare of define a function the first
dimension can be omitted

41
42
Arrays and Function
• Array, Element, Address and SubArray

• mark contains the address of the first element


– mark ~ &mark[0]
• &mark[2] is a subarray of mark containing
mark[2], mark[3] and mark[4]
• When pass an array to a function, the address of
the first element is passed.

43
44
Arrays and Function
• Using function to enter values for an array

45
46
47
Arrays and Function
• Using function to enter values for an array

48
Practice 2
1. Write a program to compute sum of an array of
float using function

Hint:
• Write a function taking an array as a parameter,
compute sum of elements of this array, return
sum:
float sum(float A[], int n) {…}
• Call the function in the main function

49
Practice 2
2. Write a program to compute sum of an array
of float using recursive function

Hint: use recursive relation


Let Sum(A, n) is the sum of n elements of A.
• if n = 0, Sum(A, n) = 0
• Otherwise, Sum(A, n) = Sum(A, n-1) + A[n-1]

50
Practice 2
3. Write a function that takes an 2D array as
parameter and prints its elements to screen, one
row per line.
void print(int A[100][100], int m, int n) {

}

51
Practice 2
3. Write a function that takes an 2D array as
parameter and fills it as follow:

52

You might also like