Vous êtes sur la page 1sur 9

Algorithm Design and Programming Techniques Lecture 4

1st class Asst. Lecture Omar Nowfal

Array:
An array is a group of consecutive, adjacent locations (i.e. elements) that all have the
same data type. Arrays may have from one to several dimensions. In this course, we will study
the one-dimensional (1D), and two-dimensional (2D) arrays.
1. One Dimensional Array (1D Array [Vector]):
 Definition: ArrayName[0..n-1] //array of size n.
The size n must be an integer greater than zero
Example:
a[0..9] this definition means that the size of the array (a) equal 10.
name[0..19] this definition means that the size of the array (name) equal 20.

 Accessing array elements: ArrayName[index]


Note: All arrays have (0) as the index of their first element and (n-1) as the index of their
last element.
Example:
Array: a[0..9] (the size of this array is 10 elements)
The first element is a[0]
The last element is a[9]
The array is a[0] , a[1] , a[2] , . . . , a[9]

34
Algorithm Design and Programming Techniques Lecture 4
1st class Asst. Lecture Omar Nowfal

Example:
 a[3] ← 60 assign 60 to the fourth element.
 input(mark[3]) read the value of the 4th mark.
 for i ← 0 to 9 do
Input(a[ i ]) input values to the array.
 for j ← 0 to 9 do
output(a[ j ]) print values of the array.

Example1: Write an algorithm that reads an integer array of size 10 and prints the array values.

Algorithm ArrayIO(a[0..9])
Begin
for i ← 0 to 9 do
input(a[i])
output(“Array is:”)
for i ← 0 to 9 do
output(a[i])
End

Example2: Write an algorithm that calculates the average of an integer array A[0..4].

Algorithm ArrayAverage(A[0..4])
Begin
for i ← 0 to 4 do
input(A[i])
sum ← 0
for i ← 0 to 4 do
sum ← sum + A[i]
output(sum / 5)
End

35
Algorithm Design and Programming Techniques Lecture 4
1st class Asst. Lecture Omar Nowfal

Example3: Write an algorithm that inputs ten integer numbers in an array and finds the
maximum number in the array.

Algorithm MaxArray (A[0..9])


Begin
for i ← 0 to 9 do
input(A[i])
max ← A[0]
for i ← 1 to 9 do
if (A[i] > max) then
max ← A[i]
output(“The max number = ”,max)
End

Example4: Write an algorithm that computes the number of even integer numbers in an array
of size n entered by the user.

Algorithm ArrayEven(B[0..99])
Begin
input(n)
for i ← 0 to n-1 do
input(B[i])
count ← 0
for i ← 0 to n-1 do
if (B[i] mod 2 = 0) then
count ← count +1
output(“The count of even numbers = “,count)
End

36
Algorithm Design and Programming Techniques Lecture 4
1st class Asst. Lecture Omar Nowfal

Example5: Write an algorithm that inputs an integer array of size 10 and arranges it in an
ascending order.

Algorithm ArrayOrder (N[0..9])


Begin
for i ← 0 to 9 do
input(N[i])
for i ← 0 to 8 do
for j← i+1 to 9 do
if (N[i] > N[j]) then
temp ← N[i]
N[i] ← N[j]
N[j] ← temp
output(“The Array in ascending order: ”)
for i ← 0 to 9 do
output(N[i])
End

2. Two Dimensional Array (2D Array [Matrix])


Two-dimensional arrays consist of values arranged in rows and columns.
 Definition: ArrayName[0..n-1 , 0..m-1] this definition means that the size of the array
is m×n elements.
Example:
G[0..2, 0..3] The size of the matrix G is 2×3 elements.
 Accessing 2D array elements: ArrayName[RowInsex, ColumnIndex]

37
Algorithm Design and Programming Techniques Lecture 4
1st class Asst. Lecture Omar Nowfal

Example:
 a[3 , 4] ← 60 assign 60 to the 3×4 element.
 Input(mark[3 , 1]) read the element 3×1 of the matrix mark.
 for i ← 0 to 9 do
for j ← 0 to 9 do
input(a[ i , j ]) input values to the 2D array.
 for p ← 0 to 9 do
for q ← 0 to 9 do
output(a[p , q]) print values of 2D array.
output(NewLine)

38
Algorithm Design and Programming Techniques Lecture 4
1st class Asst. Lecture Omar Nowfal

Example6: Write an algorithm that finds the average of each row of a 3× 4 matrix input by the
user.

Algorithm MatrixRowAverage (B[0..2 , 0..3])


Begin
for i ← 0 to 2 do
for j ← 0 to 3 do
input(B[i , j])
for i ← 0 to 2 do
sum ← 0
for j ← 0 to 3 do
sum ← sum + B[i , j]
output(sum / 3)
End

Example7: Write an algorithm that exchanges row3 with row1 in a 4× 4 integer matrix input
by the user.

Algorithm MatrixRowExchange (A[0..3 , 0..3])


Begin
for i ← 0 to 3 do
for j ← 0 to 3 do
input(A[i , j])
for i ← 0 to 3 do
temp ← A[2 , i]
A[2 , i] ← A[0 , i]
A[0 , i] ← temp
for i ← 0 to 3 do
for j ← 0 to 3 do
output(A[i , j])
output(NweLine)
End

39
Algorithm Design and Programming Techniques Lecture 4
1st class Asst. Lecture Omar Nowfal

Example8: Write an algorithm that inputs a 4×4 integer matrix and finds the maximum value
in the primary diagonal and the minimum value in the secondary diagonal

Algorithm MatrixMaxMin (A[0..3 , 0..3])


Begin
for i ← 0 to 3 do
for j ← 0 to 3 do
input(A[i , j])
max ← A[0 , 0]
min ← A[0 , 3]
for i ← 0 to 3 do
if (A[i . i] > max) then
max ← A[i , i]
for i ← 0 to 3 do
if (A[i , 3-i] < min) then
min ← A[i , 3-i]
output(“Max = “, max,” “, “Min = “, min)
End

Example9: Write an algorithm that adds two 3×4 matrices A and B both are entered by the user.
The result should be stored in matrix C.

Algorithm MatricesAddition (A[0..2 , 0..3] , B[0..2 , 0..3] , C[0..2 . 0..3])


Begin
for i ← 0 to 2 do
for j ← 0 to 3 do
input(A[i , j])
for i ← 0 to 2 do
for j ← 0 to 3 do
input(B[i , j])
for i ← 0 to 2 do
for j ← 0 to 3 do
C[i , j] ← A[i , j] + B[i , j]
output(C[i , j])
output(NewLine)
End

40
Algorithm Design and Programming Techniques Lecture 4
1st class Asst. Lecture Omar Nowfal

Example10: Write an algorithm that multiplies 3×4 matrix by 4×3 matrix both are entered by
the user. The result should be stored in a third matrix.

Algorithm MatricesMultiplication (A[0..2 , 0..3] , B[0..3 , 0..2] , C[0..2 . 0..2])


Begin
for i ← 0 to 2 do
for j ← 0 to 3 do
input(A[i , j])
for i ← 0 to 3 do
for j ← 0 to 2 do
input(B[i , j])
for i ← 0 to 2 do
for j ← 0 to 2 do
C[i ,j] ← 0
for k ← 0 to 3 do
C[i , j] ← C[i , j] + (A[i , k] * B[k , j])
for i ← 0 to 2 do
for j ← 0 to 2 do
output(C[i , j])
output(NewLine)
End

41
Algorithm Design and Programming Techniques Lecture 4
1st class Asst. Lecture Omar Nowfal

Homework:
1. Write an algorithm that inputs an integer array of 20 elements and prints only the odd
numbers in the array.

2. Write an algorithm that reads an integer array A[10] and finds the max value with its
position and the min value with its position.

3. Write an algorithm that inputs an integer array B[10] and then reverse it and print the
reversed array.

4. Write an algorithm that exchanges the primary and secondary diagonals of 4×4 matrix.

5. Write an algorithm that converts a two dimensional array into one dimensional array.
Then print the 1D array.

6. Write an algorithm that creates the following matrix:

7. Write an algorithm that computes the sum of the secondary diagonal elements in a square
integer matrix.

8. Write an algorithm that print the one’s and zero’s square matrix.

42

Vous aimerez peut-être aussi