Chapter Thirteen
String Variables



To start off this chapter, we will begin by completing the program we started in the previous chapter. As you may recall, we were printing out a quadratic equation. We have already printed out the a and b terms, and only had the c term to do. First off, let's see if the c term is zero. If it is, we will skip around the whole process:



GETC:

IF C = 0 THEN GOTO FINISH

Now that we are this far, we need to test for an error condition. If a and b are 0 and c isn't, we have an inequality. Something like "5=0". This can't be, so we will print out an error message, and have the user enter another set of numbers, like so:



IF A = 0 AND B = 0 THEN GOTO OOPS

GOTO SHOWC

OOPS:

BEEP

PRINT "How can you tell me that"; C; "=0 is a legitimate statement?"

BEEP

GOTO TOP

Now that we have that out of the way, we only need to print out the c term with it's sign. We can do that with a PRINT USING command:



SHOWC:

PRINT USING "+###"; C;

Now we will print out the "=0" part:



FINISH:

PRINT "=0"

From here, we go back and do it all again:



GOTO TOP

For your benefit, here is the entire program listing:
Download the Quadratic Equation Program


CLS

TOP:

PRINT "ax^2+bx+c=0"

PRINT

PRINT

PRINT "Please enter a ";

INPUT A

PRINT "Please enter b ";

INPUT B

PRINT "Please enter c ";

INPUT C

IF A = 0 AND B = 0 AND C = 0 THEN END

IF A <> 1 THEN GOTO ANOTONE

PRINT "x^2";

GOTO GETB

ANOTONE:

IF A <> -1 THEN GOTO ANOTMINUS1

PRINT "-x^2";

GOTO GETB

ANOTMINUS1:

IF A = 0 THEN GOTO GETB

PRINT USING "###"; A;

PRINT " x^2";

GETB:

IF B <> 1 THEN GOTO BNOT1

PRINT " +x";

GOTO GETC



BNOT1:

IF B <> -1 THEN GOTO BNOTMINUS1

PRINT " -x";

GOTO GETC

BNOTMINUS1:

IF B = 0 THEN GOTO GETC



PRINT USING "+###"; B;

PRINT " x";



GETC:

IF C = 0 THEN GOTO FINISH



IF A = 0 AND B = 0 THEN GOTO OOPS

GOTO SHOWC

OOPS:

BEEP

PRINT "How can you tell me that"; C; "=0 is a legitimate statement?"

BEEP

GOTO TOP

SHOWC:

PRINT USING "+###"; C;

FINISH:

PRINT "=0"

GOTO TOP

If you feel ambitious, fix the problem where a=0 and b is positive; i.e. supress the plus sign in that instance.

Let's jump into something new! We are now going to learn about a new type of variable. Up to this point, we have been using letters to represent storage spaces for numbers. With the addition of a symbol to the variable descriptor, it will have the capabilities of storing not numbers, but letters. They are known as string variables. You are already familiar with string constants. In a command like PRINT "I Am Learning To Program A Computer", the stuff inside the quotes is a string constant. That is because the same thing is printed out every time the program is run. To get the idea of string variables, let's review briefly numeric variables. Type in the following in immediate window:



CLS

LET A=45

PRINT A

The computer responds with 45. The number printed is the current value of the numeric variable a. Now type this in exactly as shown:



LET A$="This Is A String Variable"

PRINT A$

Notice how the computer responds? Now type this in:



LET A$="Hi."

PRINT A$

Notice that when you assign something to a string variable that is already occupied, the stuff that was in it gets cleared out. Otherwise, you would have seen:


Hi.s Is A String Variable

as a response. What if you want to clear out a string variable and not put anything in it? You assign a null string to it. What is a null string? It is a string that contains no characters at all. How do you do it in QBASIC? Like this:



LET A$=""

Two double-quotation marks right next to each other signifies a null string.

Let's put some of this together now. Here we will write a program that accepts a list of students and their test scores, and then sorts the list from highest to lowest, and then prints out the students and their scores in descending test-score-order. The first thing we will do is clear off the screen. From the Program window, enter:



CLS

Now, since we are sorting a list of test scores, we will need to use subscripted variables. This next program line will show us two things - how to dimension more than one variable at a time, and how to dimension a string variable:



DIM NAM$(100),SCORE(100)

At this point, we will have the user tell the program how many students there are:



GETCOUNT:

PRINT"Enter Number of Students";

INPUT NUMBER

Since we only allowed 100 spaces for student names and scores, let's make sure that we don't ask for more than 100 students. Using input range checking would go something like:



IF NUMBER<100 AND NUMBER>0 THEN GOTO GETSTUDENTS

PRINT" That's Too Many Students!!! Try Again"

GOTO GETCOUNT

Now we need to set up a loop to get all of our information entered into the program. Here is how we initialize the loop:



GETSTUDENTS:

FOR N=1 TO NUMBER

So that the person entering the information can keep track of where they are, we will print out the loop variable in our prompt like so:



    PRINT "Enter Student #";N;" Name";

    INPUT NAM$(N)

Now we will ask for the test score for that particular student, and continue until all students are entered:



    PRINT"Enter Test Score For ";NAM$(N);

    INPUT SCORE(N)

NEXT N

Now we need to sort the test scores. We will use the same algorithm (procedure) that we used in chapters 8 and 9. Here is the beginning of the sort segment:



FOR OUTER=1 TO NUMBER-1

    FOR INNER=OUTER+1 TO NUMBER

        IF SCORE(OUTER)>=SCORE(INNER) THEN GOTO NOSWAP

        SWAP SCORE(OUTER),SCORE(INNER)

When we swap the scores around, we have to remember to swap the corresponding name with it!



        SWAP NAM$(OUTER),NAM$(INNER)

Now we can finish the sort algorithm:



NOSWAP:

    NEXT INNER

NEXT OUTER

Now we will clear the screen and print column titles:



CLS

PRINT "Name","Score"

PRINT

That last line simply puts a blank line between the column titles and the first piece of data. Now we will use a final loop to print out the sorted list of names and scores:
Download the Test Scores Sorting Program



FOR N=1 TO NUMBER

    PRINT NAM$(N),SCORE(N)

NEXT N

END

Before you run the program, write down a list of about 9 or 10 names, and associated scores. For speed, use only first names. It types faster that way. Make sure that the test scores are not in any order. Run the program, and enter the names and numbers as you have them written down. When the computer spits out the results, check to make sure that the scores didn't change for the students!

For the next couple of chapters, we will introduce a whole bunch of functions that are connected with string variables. But for now, write a program that accepts a list of words, places the words in alphabetical order, and prints out the list.

HINT: Alphabetizing a list is done by sorting it. When you run the program, use either all upper case letters or all lower case letters. Don't mix and match! The computer considers a lower case A to come after an upper case Z!

Have fun! See you next chapter!!


Introduced In This Chapter:

Concept: String Variables.






Previous Chapter Next Chapter