Introduction to Programming 3

If you have gotten through the first 2 lessons in this series and finished all the challenges you should be fairly comfortable with variables. But in a lot of situations making a single variable for each bit of data that you want to store can be impractical. To demonstrate this we are going to use the first challenge from the last lesson. The challenge was to write a program that ask the user for 5 numbers and outputs the average of the numbers they enter. Below is how I wrote the program.

user_input$ = ""

Print "Please enter 5 numbers"

user_input$ = Input$("Enter Number 1: ")
number1 = Val(user_input$)

user_input$ = Input$("Enter Number 2: ")
number2 = Val(user_input$)

user_input$ = Input$("Enter Number 3: ")
number3 = Val(user_input$)

user_input$ = Input$("Enter Number 4: ")
number4 = Val(user_input$)

user_input$ = Input$("Enter Number 5: ")
number5 = Val(user_input$)

average = (number1 + number2 + number3 + number4 + number5) / 5


Print "The average of the numbers is "; average

In this program I started by creating a string variable called user_input$ and storing an empty string inside it. Then I used the PRINT command to write "Please enter 5 numbers".  For each number I use the INPUT$ function to prompt the user to enter a number and then I store the text the user enters into the user_input$ variable.  By now you should already know that the INPUT$ function returns a string so it can only be stored in a string variable.  Since we need to be able to add the number the user enters we have to use the VAL function to convert the string to a number and then store the number into a number variable.  I named the number variables in my program number1 to number5.  After I get all 5 numbers from the user I create a final variable called average and using the Order of Operations (PEMDAS), I add all the numbers together and divide the sum by 5.  And finally I output the average to the console.

This program was relatively simple since we only had 5 numbers.  But what if we wanted to get the average of 20 numbers or 100 numbers.  It would be impractical to continue making that many variables individually.  In situations like this where you need to store large amounts of the same type of data it is a good idea to use an array.  An array is a single variable that can store multiple values of the SAME TYPE. I emphasize that an array can only hold values of the same type as it is important that you realize that an array, like a variable, can either be a number or string.  Arrays are created using the DIM keyword with square brackets "[]"  used to set the amount of data that the array can hold.  Look at the following example.


Dim number_array[5]

Dim string_array$[7]

The first line in the above example creates a number array called number_array that can hold 5 numbers.  The next line creates a string array called string_array$ that can hold 7 strings.  Since arrays are just variables you can name an array anything you could name a normal variable.  In the example above, since number_array can hold 5 numbers it can be used to hold the data that we would have put in 5 separate variables.  I will be referring to the separate variables in an array as indexes.  Each index in an array can be access by entering the number of the index inside the square brackets.  Look at this example.

Dim x[4]
x[0] = 13
x[1] = 15
x[2] = 29
x[3] = 47

Print x[2]

'This will output 29 to the console since we set x[2] to 29 above

The code above will output 29 to the console. Notice how I did not store a number in x[4].  That is because in this example, the array x only goes up to index 3. RCBasic starts its array indexes at 0 and since we created x with 4 indexes it will have x[0] to x[3] which is a total of 4 indexes.  Now that we have the basics of how arrays work lets use what we have learned to rewrite the program that averages the 5 numbers the user enters.  Instead of having 5 different variables for the 5 numbers we are going to have one array that stores all five numbers. Our program will start off the same but we are going to create an array called numbers that has 5 indexes.  So lets start writing the program.

Dim numbers[5]
user_input$ = ""

Print "Please enter 5 numbers"

In the code above, the first line creates an array variable called numbers with 5 indexes in it.  The second line creates a string variable just like in the first version of this program at the beginning of the tutorial.  And again, we output the text "Please enter 5 numbers".  The rest of this program will be pretty much the same as the previous version, but we will be storing the numbers in each index in the numbers array rather than create 5 different variables.


user_input$ = Input$("Enter Number1: ")
numbers[0] = Val(user_input$)

user_input$ = Input$("Enter Number2: ")
numbers[1] = Val(user_input$)

user_input$ = Input$("Enter Number3: ")
numbers[2] = Val(user_input$)


user_input$ = Input$("Enter Number4: ")

numbers[3] = Val(user_input$)


user_input$ = Input$("Enter Number5: ")

numbers[4] = Val(user_input$)

The above example is the same as the version of this program I showed at the beginning of this tutorial except that it is using the array numbers instead of the 5 different number variables.  And finally we need to get the average and output it to the console.


average = (numbers[0] + numbers[1] + numbers[2] + numbers[3] + numbers[4]) / 5

Print "The average of the numbers is "; average

The code above really did not change much from the last example either other than using the array.  So the final program looks like this.

Dim numbers[5]

user_input$ = ""

Print "Please enter 5 numbers"

user_input$ = Input$("Enter Number1: ")
numbers[0] = Val(user_input$)

user_input$ = Input$("Enter Number2: ")
numbers[1] = Val(user_input$)

user_input$ = Input$("Enter Number3: ")
numbers[2] = Val(user_input$)

user_input$ = Input$("Enter Number4: ")
numbers[3] = Val(user_input$)

user_input$ = Input$("Enter Number5: ")
numbers[4] = Val(user_input$)

average = (numbers[0] + numbers[1] + numbers[2] + numbers[3] + numbers[4]) / 5

Print "The average of the numbers is "; average

You are probably asking what was the point of this. This program is actually more lines of code than the other one and we still had to do the same amount of work to get the numbers from the user.  Well to answer that, using an array served no point in the program we just wrote because of the way we wrote it.  If we needed to get more info from the user, we would have written just as much more code doing it this way as we would have doing it the way I showed at the beginning of this tutorial.  To use arrays in your programs effectively you should write your program to take advantage of the fact that your data is stored in an array. So lets rewrite this program one more time to demonstrate this. Our program will still start off the same so here is the start of the program.

Dim numbers[5]

user_input$ = ""


Print "Please enter 5 numbers"

The code above is the exact same as the previous example. In the part where we get the numbers from the user we are about to change the code drastically.  Instead of manually writing code to get input and convert it to a number for all 5 numbers we want to get from the user we are going to do all this in a loop that repeats 5 times.  Since we know that we want to loop 5 times, we might as well just use a FOR loop.  If you do not remember how a FOR loop works please reread the explanation of the FOR loop in the last two lessons.  When you are ready take a look at our loop below.


For index = 0 To 4
user_input$ = Input$("Enter Number" + Str$(index+1) + ": ")
numbers[index] = Val(user_input$)
Next

The first line in the code above starts a FOR loop, creates a number variable called index and sets it to an initial value of 0, and tells the computer to repeat all the code between the FOR and NEXT lines until index is equal to 4.  The second line is a little more complex than what we have seen up to this point.  But it shouldn't be too hard to understand.  We are using the INPUT$ function to ask the user to enter a number.  If you break this line down, you will find that it is combining a few concepts we have already covered.  Here is a breakdown of the different parts.

 

 

Use Input$ Function

string

 

index coverted to string

 

Add a ":" after the number

 

user_input$

=

Input$ (

"Enter Number"

+

Str$( index + 1 )

+

": "

 )


The reason we add 1 to index in the STR$ function is because index will equal 0 to 4 and we want to output "Enter Number1" for the first number and "Enter Number5" for the last number. So we just convert index + 1 to a string. The reason we have to convert index + 1 to a string here is because we are adding this number to another string. Once we get the input from the user we store it in the string variable user_input$ just like in the previous version. The next line is using the index variable (which will be 0 at the start of the loop and 4 by the end of the loop) to determine which index in the numbers array to store the number we got from the user in.  The NEXT line is just telling the computer to jump back to the start of the FOR loop if index is not 4 yet.  Once index is 4 and the last number is stored in the array we just need to output the average.  This will be the exact same as the previous version.

average = (numbers[0] + numbers[1] + numbers[2] + numbers[3] + numbers[4]) / 5


Print "The average of the numbers is "; average


So the final program will look like this.


Dim numbers[5]

user_input$ = ""



Print "Please enter 5 numbers"

For index = 0 To 4

user_input$ = Input$("Enter Number" + Str$(index+1) + ": ")

numbers[index] = Val(user_input$)

Next

average = (numbers[0] + numbers[1] + numbers[2] + numbers[3] + numbers[4]) / 5



Print "The average of the numbers is "; average

I know arrays can be quite a bit to take in so I encourage you to read through this tutorial multiple times before moving on.

And now some challenges.

1. Write a simple database program that ask the user for 4 peoples names (only has to be first names for this) and there ages. Store the names in a string array and store the ages in a number array. After you get the 4 peoples names and ages, start a loop and keep asking the user to enter the name of the person they want to know the age of. If the user types "quit" then output "Have a good day" and end the program.