DAT 13303 Computer Algorithm: Chapter 5: Array
DAT 13303 Computer Algorithm: Chapter 5: Array
COMPUTER ALGORITHM
CHAPTER 5: ARRAY
Lesson outcomes
At the end of this chapter you should be able to :
• Differentiate Between Simple Variable And Array
• Understand How Computer Executes Algorithm Contains Array
• Construct Algorithm Using Array (Maximum, Minimum, Count,
Average, Summation)
• Apply Sorting And Searching Problem
Rubik’s Cube Solver
Code: https://github.com/matt2uy/Cube-Solver/blob/master/Cube_Solver_No_GUI.cpp
Simple variable
• can only store one value at one time.
• When assign new value to variable, existing value replaced
Start
variable = 10;
Display “ initial value = “ , variable
variable = variable + 5
Display “ new value = “ , variable
End
Assume data entered are 10, 23, 7, 10 and 50 .
1. Trace
2. Output?
Or
name-of-array[ ] = { values } e.g. grade[ ] = {‘A’ ,’B’, ‘C’, ‘C’, ‘A’, ‘F’, ‘E’}
Array declaration & initialization
• When array size > number of initialized values, system store all initialized values
in array and the rest - zero(0) when array type numeric OR null character (■)
when array type character
num[5] = { 0 }
next
endIF
endIF array NUMBER. Then ask user to input a
next
if (count88 == 0)
Display “88 is NOT in the array“
if ( found == 1 ) number NO. Determine whether NO is in
Else
Else
Display “88 in the array “
array NUMBER or not.
Display “88 is in the array“
Display data “ 88 not in the array “
endIF
endIF
End
End
Algorithms using array
• Sort - takes an unordered collection and makes it an ordered one. methods - bubble, heap, binary, etc.
Descending order - bubble Ascending order - bubble
Start
Start
arraySize = 10
arraySize = 10
num [arraySize ]
num [arraySize ]
//input data
//input data ::
:: //sort data in descending order
Set outer with 0
//sort data in ascending order While outer < arraySize
Set outer with 0 Set inner with outer + 1
While outer < arraySize While inner < arraySize
End
Multidimensional array
• not limited to two indices (i.e., two dimensions)
• can contain as many indices as needed
• amount of memory needed for an array increases exponentially with each
dimension.
• E.g. char century [100][365][24][60][60]; declares an array with an element of
type char for each second in a century. This amounts to more than 3 billion char!
So this declaration would consume more than 3 gigabytes of memory!
• multidimensional arrays are just an abstraction for programmers, since the same
results can be achieved with a simple array, by multiplying its indices:
Trace output:
jimmy[3][5];
for (n=0; n<3; n++)
for (m=0; m<5; m++)
jimmy[n][m]=(n+1)*(m+1);
next
next