i1101 algorithm & imperative programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf ·...

58
I1101 Algorithm & Imperative Programming Semester: 2 Academic Year: 2018/2019 Credits: 5 (60 hours) Dr. AntounYaacoub Lebanese University Faculty of Science Computer Science BS Degree

Upload: others

Post on 03-Jun-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

I1101Algorithm & Imperative Programming

Semester: 2

Academic Year: 2018/2019

Credits: 5 (60 hours)

Dr. Antoun Yaacoub

Lebanese University

Faculty of Science

Computer Science BS Degree

Page 2: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Syllabus1. Algorithmics

2. Fundamental elements of a C program

3. Basic types, operators and expressions

4. Read and write data

5. Alternative structure

6. Repetitive structure

7. Arrays

8. Strings

9. Functions

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

2

Page 3: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Arrays

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

3

Page 4: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Introduction

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

4

• Arrays are certainly the most popular structured variables

Page 5: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

One-dimensional arrays• A (uni-dimensional) array A is a structured variable formed of an integer N of simple variables of the same type, which are called the components of the array. The number of components N is then the dimension of the array.

• In making the connection with mathematics, we still say that "A is a vector of dimension N"

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

5

Page 6: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

One-dimensional arraysExample• For example, the declaration:

int DAYS [12] = {31,28,31,30,31,30,31,31,30,31,30,31};

defines an array of type int of dimension 12.

The 12 components are initialized by the respective values 31, 28, 31, ..., 31.

• The first component of the array can be accessed by DAYS [0], the second component by DAYS [1],. . . , at the last component by DAYS [11].

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

6

Page 7: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

One-dimensional arraysDeclaration and Storage• <SimpleType> <ArrayName> [<Dimension>];

• Array names are identifiers that must match the known restrictions.

• Examples:

� int A[25]; // array containing 25 integers

� double B[100]; // array containing 100 doubles

� int C[10]; // array containing 10 characters

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

7

Page 8: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

One-dimensional arraysDeclaration and Storage• In C, the name of an array is the representative of the address of the first element of the array. The addresses of the other components are calculated (automatically) relative to this address.

• Example:short A [5] = {1200, 2300, 3400, 4500, 5600};

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

8

Page 9: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

One-dimensional arraysDeclaration and Storage• If an array consists of N components and if a component needs M bytes in memory, then the array will occupy N * M bytes.

• Example

• Assuming that a variable of the long type occupies 4 bytes (ie, sizeof (long) = 4), for the array T declared by: long T [15];C will reserve N * M = 15 * 4 = 60 bytes in memory.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

9

Page 10: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

One-dimensional arraysInitialization and automatic reservation• When declaring an array, we can initialize the components of the table, by indicating the list of the respective values between braces.

• Examples

� int A[5] = {10, 20, 30, 40, 50};

� float B[4] = {-1.05, 3.33, 87e-5, -12.3E4};

� int C[10] = {1, 0, 0, 1, 1, 1, 0, 1, 0, 1};

• Of course, you must ensure that the number of values in the list matches the size of the array. If the list does not contain enough values for all components, the remaining components are initialized with zero.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

10

Page 11: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

One-dimensional arraysInitialization and automatic reservation• If the dimension is not specified explicitly during initialization, then the computer automatically reserves the necessary number of bytes.

• Examples

� int A[] = {10, 20, 30, 40, 50};==> reservation of 5 * sizeof (int) bytes (in our case: 20 bytes)

� float B[] = {-1.05, 3.33, 87e-5, -12.3E4};==> reservation of 4 * sizeof (float) bytes (in our case: 32 bytes)

� int C[] = {1, 0, 0, 1, 1, 1, 0, 1, 0, 1};==> reservation of 10 * sizeof (int) bytes (in our case: 40 bytes)

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

11

Page 12: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

One-dimensional arraysInitialization and automatic reservation• Examples

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

12

Page 13: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

One-dimensional arraysAccess to components• By declaring an array by:int A[5];we have defined a table A with five components, which can be accessed by:A[0], A[1], ..., A[4]

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

13

Page 14: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

One-dimensional arraysAccess to components• Examples

� MAX = (A[0]> A[1])? A[0]: A[1];

� A[4] * = 2;

• Consider an array T of dimension N:

• In C,

� - access to the first element of the array is done by T[0]

� - access to the last element of the array is done by T[N-1]

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

14

Page 15: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

One-dimensional arraysDisplay and assignment• The for structure is particularly suitable for working with arrays. Most

applications can be implemented simply by modifying the standard examples of display and assignment.

• Display the contents of an array

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

15

void main(){

int A[5];int I; /* counter */// assign values here by the userfor (I=0; I<5; I++)

printf("%d ", A[I]);

printf("\n");}

Page 16: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

One-dimensional arraysDisplay and assignment

• Assignment with values from the user

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

16

void main(){

int A[5];int I; /* counter */for (I=0; I<5; I++)

scanf("%d", &A[I]);

for (I=0; I<5; I++)printf("%d ", A[I]);

printf("\n");}

Page 17: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Exercise• Write a program that reads the dimension N of an integer array T (maximum dimension: 50 components), fills the array with values entered on the keyboard and displays the array.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

17

Page 18: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Exercise - continued• Calculate and then display the sum of the elements of the array.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

18

Page 19: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Exercise - continued• Then delete all occurrences of the value 0 in the table T and tamp the remaining elements. Display the resulting array.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

19

Page 20: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Exercise• Write a program that reads the dimension N of an integer array T (maximum dimension: 50 components), fills the array with values entered on the keyboard and displays the array.

• Then store the elements of T in the reverse order without using an auxiliary array. Display the resulting array.

• Idea: Swap the elements of the array using two indices that run through the array, starting at the beginning and at the end of the array respectively and meeting in the middle.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

20

Page 21: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Exercise• Write a program that reads the dimension N of an integer array T (maximum dimension: 50 components), fills the array with values entered on the keyboard and displays the array.

• Then copy all strictly positive components into a second TPOS array and all strictly negative values into a third TNEG array. Display TPOS and TNEG arrays.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

21

Page 22: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Two-dimensional arrays• In C, a two-dimensional array A is to be interpreted as an array (uni-dimensional) of dimension R, each component of which is a (one-dimensional) array of dimension C.

• We call R the number of rows in the array and C the number of columns in the array. R and C are then the two dimensions of the array. A two-dimensional array therefore contains R * C components.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

22

Page 23: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Two-dimensional arrays• A two-dimensional array is said to be square, if R is equal to C.

• Comparing with mathematics, we can say that "A is a vector of R vectors of dimension C", or better:

• "A is a matrix of dimensions R and C".

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

23

Page 24: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Two-dimensional arraysExample• Consider a one-dimensional array GRADE to memorize the grades of 20 students in a class in a assignment:int GRADE[20] = {45, 34, ..., 50, 48};

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

24

Page 25: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Two-dimensional arraysExample• To memorize students' grades in 10 assignments, we can collect several of these one-dimensional tables in a two-dimensional NOTES table:int GRADES[10] [20] = {{45, 34, ..., 50, 48},

{39, 24, ..., 49, 45},... ... ...

{40, 40, ..., 54, 44}};

• In one line we find the notes of all students in a homework assignment. In a column, we find all the notes of a student.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

25

Page 26: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Two-dimensional arrays Declaration and Storage• <SimpleType> <TableName> [<DimRow>] [<DimCol>];

• Examples

� int A[10][10];

� double B[2][20];

� chariot D[15][40];

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

26

Page 27: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Two-dimensional arrays Declaration and Storage• As with one-dimensional arrays, the name of a array is the representative of the address of the first array element (ie the address of the first row of the array). The components of a two-dimensional array are stored line by line in memory.

• Example: Storing a two-dimensional arrayshort A [3] [2] = {{1, 2},

{10, 20},{100, 200}};

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

27An array of R and C dimensions, consisting of components each of which

needs M bytes, will occupy R * C * M bytes in memory.

Page 28: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Two-dimensional arrays Initialization and automatic reservation• When declaring an array, we can initialize the components of the array, by indicating the list of the respective values between braces. Inside the list, the components of each row of the table are once again enclosed in braces. To improve the readability of the programs, we can indicate the components in several lines.

• Examples

int A[3][10] ={{ 0,10,20,30,40,50,60,70,80,90},

{10,11,12,13,14,15,16,17,18,19},

{ 1,12,23,34,45,56,67,78,89,90}};

float B[3][2] = {{-1.05, -1.10 },

{86e-5, 87e-5 },

{-12.5E4, -12.3E4}}; Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

28

Page 29: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Two-dimensional arrays Initialization and automatic reservation

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

29

Page 30: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Two-dimensional arrays Initialization and automatic reservation

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

30

Page 31: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Two-dimensional arrays Initialization and automatic reservation

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

31

Page 32: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Two-dimensional arrays Initialization and automatic reservation

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

32

Page 33: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Two-dimensional arrays Initialization and automatic reservation• If the number of rows R is not specified explicitly during initialization, the computer automatically reserves the necessary number of bytes.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

33

Page 34: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Two-dimensional arrays Access to components• The elements of an array of dimensions R and C are as follows:

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

34

/ \| A[0][0] A[0][1] A[0][2] . . . A[0][C-1] || A[1][0] A[1][1] A[1][2] . . . A[1][C-1] || A[2][0] A[2][1] A[2][2] . . . A[2][C-1] || . . . . . . . . . . . . . . . || A[R-1][0] A[R-1][1] A[R-1][2] . . . A[R-1][C-1] |\ /

Page 35: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Two-dimensional arraysDisplay and assignment

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

35

void main(){

int A[5][10];int I,J;/* for each row... */for (I=0; I<5; I++) {

/* ... Consider each component*/for (J=0; J<10; J++)

printf("%7d", A[I][J]);printf("\n");

}}

Page 36: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Two-dimensional arraysDisplay and assignment

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

36

void main(){

int A[5][10];int I,J;/* for each row... */for (I=0; I<5; I++) {

/* ... Consider each component*/for (J=0; J<10; J++)

scanf("%d", &A[I][J]);printf("\n");

}}

Page 37: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Exercise• Write a program that reads the R and C dimensions of a two-dimensional array T of type int (maximum dimensions: 50 rows and 50 columns). Fill in the array with values entered on the keyboard and display the table and the sum of all its elements.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

37

Page 38: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Exercise• Write a program that reads the R and C dimensions of a two-dimensional array T of type int (maximum dimensions: 50 rows and 50 columns). Fill in the array with values entered on the keyboard and display the table and the sum of each row and column using only one auxiliary variable for the sum.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

38

Page 39: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

Exercise• Write a program that transfers a two-dimensional array R and C (maximum dimensions: 10 rows and 10 columns) into a one-dimensional array V of size R * C.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

39

/ \| a b c d | / \| e f g h | ==> | a b c d e f g h i j k l || i j k l | \ /\ /

Page 40: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

AttentionThe following exercises are to be treated according to the same scheme:

• Variable declaration

• Reading the dimensions of the arrays

• Reading array data

• Treatments

• Display results

Choose the appropriate maximum dimensions for the tables.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

40

Page 41: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

ExerciseScalar product of two vectors• Write a program that calculates the scalar product of two integer vectors U and V (of the same dimension).

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

41

/ \ / \| 3 2 -4 | * | 2 -3 5 | = 3*2+2*(-3)+(-4)*5 = -20\ / \ /

Page 42: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

ExerciseCalculation of a polynomial of degree N• Calculate, for a given float value X, the numeric value of a polynomial of degree n: � � = ���� + �������� + ⋯ + ��� + �

• The values of coefficients An, ..., A0 will be entered by keyboard and stored in a float array A of dimension n + 1.

� Use the pow() function for calculation.

� Use the Horner scheme that avoids the exponentiation operations:

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

42

Page 43: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

ExerciseMaximum and minimum values of an array• Write a program that determines the largest and the smallest value in an array of integers A.

• Next, display the value and the position of the maximum and minimum values.

• If the array contains several maxima or minima, the program will retain the position of the first maximum or minimum encountered.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

43

Page 44: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

ExerciseInsert a value into a sorted array• An array A of dimension N + 1 contains N integer values sorted in ascending order; the (N + 1) th value is undefined.

• Insert a given VAL value read from the keyboard in the array A to obtain a sorted array of N + 1 values.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

44

Page 45: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

ExerciseFinding a value in an array• Search an array of integers for a VAL value entered on the keyboard. Display the position of VAL if it is in the array, otherwise display a corresponding message. The POS value that is used to memorize the position of the value in the array, will have the value -1 as long as VAL was not found.

• Implement two versions:

• a) Sequential research: Successively compare the values of the array with the given value.

• b) Dichotomous search ('binary search')� Condition: Table A must be sorted

� Compare the searched number to the value in the middle of the table,

� if there is a tie or if the table is exhausted, stop the treatment with a corresponding message.

� If the value sought precedes the current value of the table, continue the search in the half-table to the left of the current position.

� if the desired value follows the current value of the table, continue the search in the half-table to the right of the current position.

� Write the program for the case where the array A is sorted in ascending order.

• Question: What is the advantage of dichotomous research? Explain briefly.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

45

Page 46: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

ExerciseMerge two sorted arrays• We have two arrays A and B (of respective dimensions N and M), sorted in ascending order. Merge the elements of A and B into a third MERGE array sorted in ascending order.

• Method: Use three indexes IA, IB and IMERGE. Compare A[IA] and B[IB]; replace FUS[IMERGE] with the smaller of the two elements; advance in the table MERGE and in the table that contributed its element. When one of the two arrays A or B is exhausted, simply copy the remaining elements of the other array into the MERGE array.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

46

Page 47: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

ExerciseSort by maximum selection• Sort the elements of an array A in descending order.

• Method: Traverse the array from left to right using the index I. For each element A[I] of the array, determine the PMAX position of the (first) maximum to the right of A[I] and exchange A[I] ] and A[PMAX].

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

47

Page 48: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

ExerciseSort by propagation (bubble sort)• Sort the elements of an array A in ascending order.

• Method: Starting again each time at the beginning of the table, we carry out several times the following treatment: We propagate, by successive permutations, the largest element of the array towards the end of the array (like a bubble that goes back to the surface of a liquid).

• Implement the algorithm considering that:

� The part of the table (on the right) where there were no permutations is sorted.

� If no swapping has occurred, the array is sorted.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

48

Page 49: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

ExerciseDistinct elements in an array• Let X be an array containing n positive integers. Write a program that

creates:

� an array Dist containing the distinct elements of X

� An array Effec containing the number of occurrence of each distinct element of X

� An integer k containing the number of distinct elements of X.

• Example:

� X={4, 5, 5, 7, 4, 2, 3, 2}

� Dist={4, 5, 7, 2, 3}

� Effec={2, 2, 1, 2, 1}

� K=5

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

49

Page 50: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

ExerciseDelete an element from an array• Write a program that deletes the first occurrence of an element in an array

• Example

� A={0, 1, 0, 1, 2, 3, 1}

� Delete the first occurrence of 0

� A={1, 0, 1, 2, 3, 1}

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

50

Page 51: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

ExerciseZeroing the main diagonal of a matrix• Write a program that zeros the elements of the main diagonal of a given square matrix A.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

51

Page 52: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

ExerciseUnit Matrix• Write a program that constructs and displays a unitary square matrix U of dimension N. A unitary matrix is a matrix, such that:

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

52

/ 1 if i=j

uij = |

\ 0 if i≠j

Page 53: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

ExerciseTransposition of a matrix• Write a program that performs the transposition tA of a matrix A of dimensions N and M into a matrix of dimensions M and N.

• a) The transposed matrix will be stored in a second matrix B which will then be displayed.

• b) The matrix A will be transposed by permutation of the elements.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

53

/ \ / \tA = t | a b c d | = | a e i |

| e f g h | | b f j || i j k l | | c g k |\ / | d h l |

\ /

Page 54: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

ExerciseMultiplication of a matrix by a real• Write a program that multiplies an A matrix by a real X.

• a) The result of the multiplication will be memorized in a second matrix B which will then be displayed.

• b) The elements of matrix A will be multiplied by X.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

54

/ \ / \| a b c d | | X*a X*b X*c X*d |

X * | e f g h | = | X*e X*f X*g X*h || i j k l | | X*i X*j X*k X*l |\ / \ /

Page 55: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

ExerciseAddition of two matrices• Write a program that performs the addition of two matrices A and B of the same dimensions N and M.

• a) The result of the addition will be stored in a third matrix C which will then be displayed.

• b) Matrix B is added to A.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

55

/ \ / \ / \| a b c d | | a' b' c' d' | | a+a' b+b' c+c' d+d' || e f g h | + | e' f' g' h' | = | e+e' f+f' g+g' h+h' || i j k l | | i' j' k' l' | | i+i' j+j' k+k' l+l' |\ / \ / \ /

Page 56: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

ExerciseMultiplication of two matrices• By multiplying a matrix A of dimensions N and M with a matrix B of dimensions M and P, a matrix C of dimensions N and P is obtained:A (N, M) * B (M, P) = C (N, P)

• The multiplication of two matrices is done by multiplying the components of the two matrices rows by columns:

• Write a program that performs the multiplication of two matrices A and B. The result of the multiplication will be stored in a third matrix C which will then be displayed.

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

56

/ \ / \ / \| a b c | | p q | | a*p + b*r + c*t a*q + b*s + c*u || e f g | * | r s | = | e*p + f*r + g*t e*q + f*s + g*u || h i j | | t u | | h*p + i*r + j*t h*q + i*s + j*u || k l m | \ / | k*p + l*r + m*t k*q + l*s + m*u |\ / \ /

Page 57: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

ExercisePascal's triangle• Write a program that builds the PASCAL triangle of degree N and stores it in a square matrix P of dimension N + 1.

• Method:

� Calculate and display only the values up to the main diagonal (included). Limit the degree to enter by the user to 13.

� Construct the triangle line by line:

� Initialize the first element and the element of the diagonal to 1.

� Calculate the values between the initialized elements from left to right using the relation: Pi, j = Pi-1, j + Pi-1, j-1

• Example: Pascal's triangle of degree 6:

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

57

n=0 1

n=1 1 1

n=2 1 2 1

n=3 1 3 3 1

n=4 1 4 6 4 1

n=5 1 5 10 10 5 1

n=6 1 6 15 20 15 6 1

Page 58: I1101 Algorithm & Imperative Programmingantoun.me/teaching/2018-2019/i1101/ch6.pdf · One-dimensional arrays Initialization and automatic reservation • When declaring an array,

ExerciseSearch for saddle point

• Search in a given matrix A for elements that are both a maximum on their row and a minimum on their column. These elements are called saddle points. Display positions and values of all found saddle points.

• Examples: The underlined elements are saddle points:

• Method: Create two auxiliary MAX and MIN matrices of the same dimensions as A, such as:

• �� �, � = �1 �� � �, � �� � �� ���� �� ��� �0 ��ℎ������• ��� �, � = �1 �� � �, � �� � ������� �� �!��� �0 ��ℎ������

Alg

ori

thm

& I

mp

era

tive

Pro

gra

mm

ing

(I1

10

1)

20

18

-20

19

58

/ \ / \ / \ / \| 1 8 3 4 0 | | 4 5 8 9 | | 3 5 6 7 7 | | 1 2 3 || | | 3 8 9 3 | | 4 2 2 8 9 | | 4 5 6 || 6 7 2 7 0 | | 3 4 9 3 | | 6 3 2 9 7 | | 7 8 9 |\ / \ / \ / \ /