# sdt topic - 08

Post on 18-Jan-2015

477 views

Category:

## Education

Tags:

• #### single number

Embed Size (px)

DESCRIPTION

TRANSCRIPT

• 1. Topic 8 : Array Software Development Techniques

2. Finding the Highest Number of 3 inputs 3. Finding the Highest Number of 6 inputs 4. 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 5. 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. 6. 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. 7. The Array We can put something into a compartment like so: myArray = 100 This puts the value 100 into the compartment marked as 5 It is the sixth compartment. myArray = 10 myArray = 40 We can get information out too: output myArray 8. The Array The number that indicates a compartment is known technically as an index. The compartment is known as the element. 9. 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 would be 203 The square brackets indicate the index 10. 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. 11. Finding the Highest Number of 3 inputs 12. Finding the Highest Number of 3 inputs 13. 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. 14. Passing an Array to a Function Pseudocode to pass data as array of whole number in function and display it. 15. Main Program 16. 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) 17. 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 18. 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. 19. Returning an Array 20. Main Program 21. 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 22. 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 23. 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. 24. 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 25. 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 = 100 26. Two Dimensional Arrays Getting Output myGrid [10, 5] Or Output myGrid 27. 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 = "Nepal" 5. str = "America" 6. str = "Australia" 7. str = "England" 28. 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 29. End of Topic 8 Software Development Techniques

Recommended