Académique Documents
Professionnel Documents
Culture Documents
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.
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.
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.
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.
Example7: Write an algorithm that exchanges row3 with row1 in a 4× 4 integer matrix input
by the user.
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
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.
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.
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.
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