0% found this document useful (0 votes)
30 views24 pages

DAT 13303 Computer Algorithm: Chapter 5: Array

The document discusses arrays in computer algorithms, including how arrays allow storing and manipulating multiple values using indexes, how to declare, initialize, input, output, and manipulate array data through various algorithms like finding maximum/minimum values, calculating sums/averages, and searching/sorting arrays. It provides examples of algorithms to perform common array operations like counting elements that meet certain conditions and searching for a specific value within an array.

Uploaded by

Amir Amjad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views24 pages

DAT 13303 Computer Algorithm: Chapter 5: Array

The document discusses arrays in computer algorithms, including how arrays allow storing and manipulating multiple values using indexes, how to declare, initialize, input, output, and manipulate array data through various algorithms like finding maximum/minimum values, calculating sums/averages, and searching/sorting arrays. It provides examples of algorithms to perform common array operations like counting elements that meet certain conditions and searching for a specific value within an array.

Uploaded by

Amir Amjad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

DAT 13303

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?

variable num store last number (50).


first four numbers gone.
If want to keep previous data (first four), have to use
five variables, instead of one.
Array
• It has a name just like a variable
• can store >1 data at one time because it is allocated many memory
locations as indicated by size of array.
• data type for each data must be same.
• number of data in an array is fix.
Array with 5 data in memory
Array with 10 data in memory
When To Use Array
• many data with same data type to be used repeatedly in algorithm at
different places.
• many data with same data type that must be retained until end of
algorithm.
Array Manipulation
• types of data
• primitive - cannot be broken further into a smaller data
• complex - can be broken further e.g. array
• array can be broken into a few data from a set of data.
• When an array stores 10 data or value, the array can be broken into 10 different data or
values.
• When an array stores 5 data or values, the array can be broken into 5 different data or
values.
• A string is an example of array.
• contains few single characters, depending on size of string when declared.
• e.g. declare string coursecode = “love”;
• initialize string coursecode with values love.
• string coursecode can be broken into 4 single characters l, o, v and e .
Array Manipulation
• Each element accessed by index/subscript number.
• relation between element and index:
element index/subscript
First element 0
Second element 1
Third element 2
:: When array size is N, indexes are 0, 1, 2, …… N-1.
Fifth element 4 Manipulate array just like manipulate variable -
can perform input, process and output
Array declaration
• Before use array in algorithm, declare array - mention data type,
name, and size of array.
• Format: data_type Name-of-array [size of array]
• e.g.
int num[5]
float mark[10]
float salary[30]
char name[30]
Array declaration & initialization
• can declare and initialize array at same time
• Format:
name-of-array[size-of-array] = { values } e.g. number[5] = { 10, 23, 56, 1, 4}

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] = { 10, 23, 56, 1}

num[5] = { 0 }

grade[7] = {‘A’ ,’B’, ‘C’, ‘C’}

• INCORRECT declare – array size < number of initialized values e.g. 


number[5] = { 1, 2, 3, 4, 5, 6, 7, 8}
Storing data in array
• using
• input statement
• assignment statement
• can assign data into
• certain element
• name-of-array [which-element] = data // using assignment statement
num[3] = 4

variable index starts with 0

• read name-of-array [which-element] // using input statement


read num[2]

• entire array or portion of array - use loop


• E.g. set the entire array X with size 5 using input statement
Display array
• Display an element:
• Format: Display name-of-array [ which element ]
• E.g. Display num[4] // content of element at index 4 (5th element) displayed
• Display entire or portion of array - using loop
• E.g. Display entire array mark with size 10
For(index=0, index<10 , index++)
Display mark[index]
Next
Algorithms using array
• Calculate total of data in array • Calculate average of data in array
Start Start
arraySize = 10
arraySize = 10
num[ arraySize ]
total = 0 num[ arraySize ]
for(index=0, index<arraySize, index++) //input data
//input data //total data in array
display “Enter data: ” Display “Average = “, total /arraySize
read num[index]
//total data in array
End
total = total + num[index]
OR
total += num[index]
Next
Display “Total = ”, total
End
Algorithms using array
• Find maximum, minimum value in array
Start
arraySize = 10
num[ arraySize ]
//input data
//find maximum and minimum data in array
max = num[0]
min = num[0]
For(ind=1, ind < arraySize, ind++)
if ( num[ind] > max )
max = num[ind]
endIF
if ( num[ind]<min )
min = num[ind]
endIF
Next
Display max
Display min
End
Algorithms using array
• Counting ( to count how many certain data exists in the array )
Start
arraySize = 10
num[arraySize]
//input data
//how many even numbers in array
Test yourself
count-even = 0 Determine how many students in
For(ind=0, ind < arraySize , ind++) class DCS 1A pass first test. Assume
if ( num[ind] is even )
count-even = count-even + 1
there are 30 students in class. Passing
OR mark is 50. List all pass marks.
count-even += 1
OR
count-even++
EndIF
Next
Display count-even
End
Algorithms using array
• Searching ( to find whether a certain data is in the array or not ) e.g. find whether 88 is in the array or not.
Solution 1: Use count algorithm Solution 2:
Start Start Note: break - jump out from loop when condition
arraySize = 10 arraySize = 10
num[arraySize] num[arraySize]
(num[ind] == 88 ) true.
    in terms of number of iteration:
//input data //input data • Solution 1 does iteration for arraySize times.
Set ind with 0 set ind with 0
While ind < arraySize repeat arraySize times
• Solution 2 stops iteration when data found
read num[ind] read num[ind] • maximum number of iteration - arraySize times
increase ind by 1 increase ind by 1 • when data found, number of iteration lesser
endWhile endRepeat
   
//count how many 88 in the array //determine whether 88 is in the array or not
Declare count88 As integer = 0 found = 0
For(index=0, index < arraySize, index++) for(ind=0, ind < arraySize, ind++)
if ( num[ind] == 88 )
Test yourself
if (num[index] == 88)
count88 = count88++ found = 1 Ask user to enter 15 number and store into
break

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

Set inner with outer + 1 if ( num[ inner ] < num [outer ] )

While inner < arraySize temporary = num[ inner ]


num[ inner ] = num[ outer ]
if ( num[ inner ] > num [outer ] )
num[ outer ] = temporary
temporary = num[ inner ]
endIF
num[ inner ] = num[ outer ]
increase inner by 1
num[ outer ] = temporary
endWhile
endIF add 1 to outer
increase inner by 1 endWhile
endWhile  
add 1 to outer //display sorted data
endWhile Set index with 0
  While index < arraySize
//display sorted data Display num[index]

Set index with 0 Increase index by 1


endwhile
While index < arraySize
Display num[index] End
Increase index by 1
endwhile

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:

jimmy [3][5]; // is equivalent to


jimmy [15]; // (3 * 5 = 15)
2 Dimensional Array
• Use 2 subscripts to identify tabular data, where data are arranged in rows,
columns
• General form:
ArrayName[no-of-row][no-of-column] e.g. a[3][4]

every element in array a identified by an element name of the form a[ i ][ j ],


where:
a – array name
i, j - subscripts that identify each element in a
Initializing 2-Dimensional Arrays
• may be initialized by specifying bracketed values for each row
e.g.
a[3][4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
}
• nested braces, which indicate the intended row, are optional.
• Following initialization is equivalent to previous example −
a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
Accessing 2 Dimensional Array Elements
• element in 2D array accessed using subscripts, i.e., row index and column index of
array. E.g.
val = a[2][3] //take 4th element from 3rd row of array
Output:
// an array with 5 rows and 2 columns
a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}}; a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
// output each array element's value a[1][1]: 2
for (i = 0; i < 5; i++ ) a[2][0]: 2
a[2][1]: 4
for (j = 0; j < 2; j++ )
a[3][0]: 3
display “a”, “[“, i, “]”, “[“, j, “]: ”, a[i][j], newline a[3][1]: 6
next a[4][0]: 4
next a[4][1]: 8
Test yourself

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

You might also like