Upcoming SlideShare
×

3.2 stacks and arrays

608 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
608
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
17
0
Likes
0
Embeds 0
No embeds

No notes for slide
• Code:Subjects[1] = &quot;English&quot;Subjects[2] = &quot;History&quot;Subjects[3] = &quot;Computers&quot;Subjects[4] = &quot;Science&quot;Subjects[5] = &quot;Math“TextWindow.WriteLine(&quot;Subjects is an array: &quot; + Array.IsArray(Subjects))TextWindow.WriteLine(&quot;Subjects[4] is available: &quot; + Array.ContainsIndex(Subjects, 4))TextWindow.WriteLine(&quot;Math is available: &quot; + Array.ContainsValue(Subjects, &quot;Math&quot;))Array.GetItemCount(Subjects)
• Slide Notes:The PushValue operation does not return anything.The PopValue operation returns the value from the stack.The GetCount operation returns the number of items in a specified stack.
• Code:container = &quot;empty&quot;For i = 0 To 50 Stack.PushValue(container , &quot;plate number &quot; + i) EndForTextWindow.WriteLine(&quot;The number of plates in the container is &quot; + Stack.GetCount(container))For i = 0 To 8Stack.PopValue(container) EndForTextWindow.WriteLine(&quot;After taking away 8 plates, the total number of plates is &quot; + Stack.GetCount(container))TextWindow.WriteLine(&quot;The top most plate in the container is &quot; + Stack.PopValue(container))
• Solution:TextWindow.WriteLine(“Flight Reservations&quot;)TotalSeats = 10For i = 1 To TotalSeatsTextWindow.Write(&quot;Enter Passenger Name: &quot;) Name[i] = TextWindow.Read() TextWindow.WriteLine(&quot;Seat number &quot; + i + &quot; is reserved by &quot; + Name[i])GetDetails()EndForSub GetDetailsIf Array.GetItemCount(Name) = TotalSeats ThenTextWindow.WriteLine(&quot;No more seats are available!&quot;)ElseArray.GetItemCount(Name)AvailableSeats = TotalSeats - Array.GetItemCount(Name) TextWindow.WriteLine(&quot;Number of available seats is: &quot; + AvailableSeats)TextWindow.WriteLine(&quot;&quot;) EndIfEndSub
• 3.2 stacks and arrays

1. 1. Microsoft® Small Basic<br />Stacks and Arrays<br />Estimated time to complete this lesson: 1 hour<br />
2. 2. Stacks and Arrays<br />In this lesson, you will learn about:<br />Using different operations of the Stack object.<br />Using different operations of the Array object.<br />
3. 3. Stacks and Arrays<br />Before we move on to discuss the Array object and the Stack object, let’s first understand when we use both of these objects.<br />Arrays can be multi-dimensional, but a stack is only one-dimensional. You can use arrays to directly access any of its elements but if you use a stack, you can access only the top element. For instance, if you want to access the last element of the stack, you must go through all the elements from the beginning. <br />
4. 4. The Array Object<br />So far, you have learned about variables that store single values. Now, let’s learn about a special variable called an Array. <br />An array is a type of variable that can store multiple values at a time. If you want to store the names of five users, then instead of creating different variables, you can just use one variable to store all of the five names. <br />You use the indexing method to store multiple values in an array. For example, you can create an array called name as: name[1], name[2], name[3], name[4], and name[5]. Here, 1, 2, 3, 4, and 5 are the indices for the name array.<br />The name[1], name[2]… may seem to you as different variables, but they are all just one variable!<br />
5. 5. Operations of the Array Object<br />You can use the IsArray operation to check whether the specified variable is an array.<br />You can use the ContainsIndex operation to check whether the array contains the specified index. This operation is helpful when you want to check whether the array’s index is initialized by a specific value.<br />You can use the ContainsValue operation to check whether the array contains the specified value. You can use this operation to check if the array’s value was stored in the specified index.<br />Now, let’s discuss some operations of the Array object, such as <br />IsArray, ContainsIndex, and ContainsValue.<br />
6. 6. Operations of the Array Object<br />As you can see, Subjects as an array stores the names of five different subjects. You can use the IsArray operation to check if Subjects is an array. You can also use the ContainsIndex operation to check the availability of the index Subjects[4] in the Subjects array. Lastly, you can use the ContainsValue operation to see if the value “Math” is available in the Subjects array.<br />Let’s see how we can use these operations in a program.<br />output<br />
7. 7. Operations of the Array Object<br />Look at this example to learn how to use these operations.<br />In this example, because you don’t know the indices for the Employee array, you use the GetAllIndices operation. Next, you use the GetItemCount operation in a For loop to list the information stored in the Employee array.<br />The Array object also provides more useful operations, such as: <br /><ul><li> GetAllIndices
8. 8. GetItemCount</li></li></ul><li>The Stack Object<br />Let’s explore each of these operations…<br />For example, if you look down at a stack of plates, you can only see the top plate. To see the next plate, you will have to remove this top plate. You can’t pick a plate from the middle of the stack until you’ve removed the plates above it.<br />The Stack object consists of three operations:<br />The Stack object in Small Basic is a method of storing data in the manner of stacking of plates. It works on the principle of last-in, first-out (LIFO).<br />PushValue<br />PopValue<br />GetCount<br />
9. 9. Operations of the Stack Object<br />The Stack object stores data just as a stack of plates. Let’s take a look at some examples to understand how it works.<br />The PushValue operation is like adding a new plate to the top of the stack of plates. With the help of this operation, you can push a value to the specified stack.<br />The PopValue operation is like picking out a plate from the top of the stack. You can use this operation to pop out a value from the specified stack.<br />The GetCount operation provides the total number of plates in the stack. You can use this operation to determine the count of items in a specified stack.<br />
10. 10. Operations of the Stack Object<br />In this example, you use the PushValue operation to push 50 plates into an empty container. Then by using the PopValue operation, you take eight plates away from the stack. Now, use the GetCount operation to get the total number of plates in the container. You also display the value of the plate, which is placed at the top.<br />Let’s write a program to better understand these operations.<br />
11. 11. Let’s Summarize…<br />Congratulations! Now you know how to:<br />Use different operations of the Stack object.<br />Use different operations of the Array object.<br />
12. 12. It’s Time to Apply Your Learning…<br />Using the Array object, write a program for “Flight Reservation” that allows you to perform the following actions:<br /><ul><li>Reserve seats for 10 passengers.
13. 13. Display the seat number of the passenger along with his or her name.
14. 14. Display the total number of seats available. </li>