Post on 18-Jan-2015
description
Topic 8 : ArraySoftware Development Techniques
Finding the Highest Number of 3 inputs
Finding the Highest Number of 6 inputs
Introduction
• Until now we have been working with single units of data.
• A whole number contains a single number, no more.
• Today we are going to introduce a new kind of data structure.
• The Array
• This is a list of similarly types variables.
• A list of whole numbers
• A list of characters
• A list of string
The Problem So Far
• Scalability
• We can write a program to discover the largest number in a set of five numbers.
• But that becomes a much more time-consuming prospect if we want to find the largest in a set of thousand numbers.
• The array is used to manage this problem.
The Array
• We decide on the size of an array.
• We decide on the type of things it will store.
• In our pseudocode, we can create an array using the following syntax:
• data myArray as array of (10) whole numbers
This creates an array of whole numbers, and provides ten compartments starting the compartment number from 0.
So, compartment number of first compartment is 0.
Compartment number five is the sixth compartment in an array.
The Array
• We can put something into a compartment like so:• myArray[5] = 100
• This puts the value 100 into the compartment marked as 5• It is the sixth compartment.
• myArray[7] = 10
• myArray[2] = 40
• We can get information out too:• output myArray[5]
The Array
• The number that indicates a compartment is known technically as an index.
• The compartment is known as the element.
The Structure of an Array
Index Value
0 10
1 50
2 203
3 56
4 79
This is an array of size five.
The contents of myArray[2] would be 203
The square brackets indicate the index
Question
• Draw a diagram of an array called myNums which has been initialized to hold 5 whole numbers. Your diagram should indicate the structure of the array, with the different parts clearly labelled.
Finding the Highest Number of 3 inputs
Finding the Highest Number of 3 inputs
Passing an Array to a Function
• When we pass an array to a function, we do not specify its size.
• There is a size of function that gives you the size of the array.
• But not how many elements have been filled.
Passing an Array to a Function
• Pseudocode to pass data as array of whole number in function and display it.
Main Program
Desk-Checking Main Function
Line Num numbers Remarks
1 0, 0, 0
2 20, 0, 0
3 20, 30, 0
4 20, 30, 15
5 20, 30, 15 Call display(numbers)
Desk-Checking an Array
Line Num nums i len Output Remarks
1 20, 30, 15 0
2 20, 30, 15 0 0
3 20, 30, 15 0 3
4 20, 30, 15 0 3 Loop while i is less than len(3)
5 20, 30, 15 0 3 20
6 20, 30, 15 1 3
7 20, 30, 15 1 3 Goto line 4
4
Returning an Array
• You can return arrays from your functions just like they were our usual variables.
• There are some complications though
• If you return an array, it will overwrite in every the array you are using to hold it.
• It will overwrite the size.
• It will overwrite existing contents.
• You can also declare an array without giving a size.
• But that sets it as null in your desk-check.
Returning an Array
Main Program
Desk-Checking Main Function
Line Num b i sizeof(b) output Remarks
1 null
2 null 0
3 20, 40, 57 0 0 Returning from function
4 20, 40, 57 0 3 Loop while i is less than sizeof b (3).
5 20, 40, 57 0 3 20
6 20, 40, 57 1 3
7 20, 40, 57 1 3 Goto 4
4
Desk-Checking Function
Line Num a Remarks
1 0, 0, 0
2 20, 0, 0
3 20, 40, 0
4 20, 40, 57
5 20, 40, 57 return a
Voting Scenario
Write pseudo code and source code for following scenario.
• The number of different parties standing in the election. This must be a whole number between 2 and 10 inclusive.
• Take the name of parties.
• The number of votes cast for each party standing in the election. These must be whole numbers between 0 and 600,000 inclusive.
Two Dimensional Arrays
• The concepts are identical.
• You just need two indexes, and two sets of sizes.
• These are grids of data.
• Two dimensional arrays allow for several kinds of programs that would otherwise be very difficult.
• Especially games
Two Dimensional Arrays
• Creating
• data myGrid as array (10, 10) of whole numbers
• Or
• Data myGrid as array (10)(10) of whole numbers
• Setting
• myGrid[10, 5] = 100
• Or
• myGrid[10][5] = 100
Two Dimensional Arrays
• Getting
• Output myGrid [10, 5]
• Or
• Output myGrid[10][5]
Two Dimensional Arrays Example
1. data i as whole number
2. data j as whole number
3. data str as array(2)(2) of String
4. str[0][0] = "Nepal"
5. str[0][1] = "America"
6. str[1][0] = "Australia"
7. str[1][1] = "England"
Two Dimensional Arrays Example
8. i = 0
9. loop while i is less than sizeof(str)
10. j = 0
11. loop while j is less than sizeof(str[i])
12. Output str[i][j]
13. j = j + 1
14. end loop
15. i = i + 1
16.end loop
End of Topic 8Software Development Techniques