3.2 stacks and arrays

12
Microsoft® Small Basic Stacks and Arrays Estimated time to complete this lesson: 1 hour

Upload: allenbailey

Post on 20-May-2015

406 views

Category:

Entertainment & Humor


4 download

TRANSCRIPT

Page 1: 3.2   stacks and arrays

Microsoft® Small Basic

Stacks and Arrays

Estimated time to complete this lesson: 1 hour

Page 2: 3.2   stacks and arrays

Stacks and Arrays

In this lesson, you will learn about:

Using different operations of the Stack object.

Using different operations of the Array object.

Page 3: 3.2   stacks and arrays

Stacks and Arrays

Before we move on to discuss the Array object and the Stack object, let’s first understand when we use both of these objects.

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.

Page 4: 3.2   stacks and arrays

The Array Object

So far, you have learned about variables that store single values. Now, let’s learn about a special variable called an Array.

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.

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.

The name[1], name[2]… may seem to you as different variables, but they are all just one variable!

Page 5: 3.2   stacks and arrays

Operations of the Array Object

Now, let’s discuss some operations of the Array object, such as IsArray, ContainsIndex, and ContainsValue.

You can use the IsArray operation to check whether the specified variable is an array.

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.

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.

Page 6: 3.2   stacks and arrays

Operations of the Array Object

Let’s see how we can use these operations in a program.

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.

OUTPUT

Page 7: 3.2   stacks and arrays

Operations of the Array Object

The Array object also provides more useful operations, such as: GetAllIndices GetItemCount

Look at this example to learn how to use these operations.

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.

Page 8: 3.2   stacks and arrays

The Stack Object

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).

The Stack object consists of three operations:

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.

PushValue PopValue

GetCount Let’s explore each of these operations…

Page 9: 3.2   stacks and arrays

Operations of the Stack Object

The Stack object stores data just as a stack of plates. Let’s take a look at some examples to understand how it works.

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.

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.

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.

Page 10: 3.2   stacks and arrays

Operations of the Stack Object

Let’s write a program to better understand these operations.

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.

Page 11: 3.2   stacks and arrays

Let’s Summarize…

Congratulations! Now you know how to:

Use different operations of the Stack object.

Use different operations of the Array object.

Page 12: 3.2   stacks and arrays

It’s Time to Apply Your Learning…

Using the Array object, write a program for “Flight Reservation” that allows you to perform the following actions:

Reserve seats for 10 passengers.

Display the seat number of the passenger along with his or her name.

Display the total number of seats available.