SDT Topic - 08
Upcoming SlideShare
Loading in...5
×
 

SDT Topic - 08

on

  • 573 views

 

Statistics

Views

Total Views
573
Slideshare-icon Views on SlideShare
321
Embed Views
252

Actions

Likes
0
Downloads
13
Comments
0

3 Embeds 252

http://www.pradipkharbuja.com.np 250
http://translate.googleusercontent.com 1
https://m.facebook.com 1

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    SDT Topic - 08 SDT Topic - 08 Presentation Transcript

    • TOPIC 8 : ARRAY Er. Pradip Kharbuja
    • 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
    • Finding the Largest Number of 7 inputs
    • Finding the Largest Number of 7 inputs
    • Finding the Largest Number of 1000 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 an Array ◦ We do this by separating the elements of an array in its column by a comma. 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
    • 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
    • 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
    • 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
    • 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 8