Archive

Archive for December, 2011

QBasic Tutorial 21-Preventing Duplicate Data In The Same Array

December 1, 2011 Leave a comment

QBasic Tutorial 21 – Preventing Duplicate Data In The Same Arrray

 

Make a program that will hold 4 First names within an array.

 

So first we declare FN which is First Names as a String:

DIM FN AS STRING

Declare an array called FNames, there will be four first names in this array. Created

as a string.

DIM FNames(4) AS STRING

We need a two counter-variables for a couple of FOR LOOP’s:

So we declare x as an integer as well as y:

DIM x AS INTEGER DIM y AS INTEGER

 

Then we declare Found as an Integer:

DIM Found AS INTEGER

Then clear the screen:

CLS

 

So here are declare statements:

DIM FN AS STRING DIM FNames(4) AS STRING DIM x AS INTEGER DIM y AS INTEGER DIM Found AS INTEGER

CLS

Start the FOR LOOP:

FOR x = 1 TO 4

Next start the DO LOOP:

FOR x = 1 TO 4  DO   INPUT “Enter Name: “, FN      —Placing the user input value within

FN.

Then set Found to equal 0 after the user input enter name:

INPUT “Enter Name: “, FN Found = 0

FOR x = 1 TO 4  DO   INPUT “Enter Name: “, FN   Found = 0     FOR y = 1 TO 4    IF UCASE$(FN) = UCASE$(FNames(y)) THEN

 

–This statement above means if the uppercase of FN is the same as the uppercase

conversion UCASE FNames y THEN we know that the name has been found.

So set Found to equal 1:

FOR y = 1 TO 4  IF UCASE$(FN) = UCASE$(FNames(y)) THEN   Found = 1  END IF NEXT y

IF Found = 1 THEN   PRINT “Duplicate Names Are Not Accepted”  END IF LOOP UNTIL Found = 0

FNames(x) = FN NEXT x

 

 

So here is what’s going to happen:

We start the FOR LOOP at FOR x = 1 TO 4. Then we start the DO LOOP until Found =

False or also 0.

We’ll get the user input and place it within FN, set Found to equal false or 0.

Within the second FOR LOOP we will check to see if the user input is the same as

what’s already in the array. If it is then Found becomes 1 or True.

Then we will go ahead and print duplicate names are not accepted loop back to prompt

the user to enter a name again.

If the name is not found the LOOP ends, then we’re alound to place our name within

our name array, then we go to our next x, and then repeating the process until our

loop ends.

The full code so far:

 

DIM FN AS STRING DIM FNames(4) AS STRING DIM x AS INTEGER DIM y AS INTEGER DIM Found AS INTEGER

CLS

FOR x = 1 TO 4  DO   INPUT “Enter Name: “, FN   Found = 0

FOR y = 1 TO 4    IF UCASE$(FN) = UCASE$(FNames(y)) THEN     Found = 1    END IF   NEXT y      IF Found = 1 THEN    PRINT “Duplicate Names Are Not Accepted”   END IF  LOOP UNTIL Found = 0    FNames(x) = FN NEXT x

 

Next we will print out the information: Printing out the entire array

PRINT PRINT PRINT “The Names In The Array: ”

FOR x = 1 TO 4  PRINT FNames(x) NEXT x

 

Ok so now we go ahead and run the code.

So this code is accepting 4 names for an array and will not accept duplicate names.

Final Code:

 

 

DIM FN AS STRING DIM FNames(4) AS STRING DIM x AS INTEGER DIM y AS INTEGER DIM Found AS INTEGER

CLS

FOR x = 1 TO 4  DO   INPUT “Enter Name: “, FN   Found = 0

FOR y = 1 TO 4    IF UCASE$(FN) = UCASE$(FNames(y)) THEN     Found = 1    END IF   NEXT y      IF Found = 1 THEN    PRINT “Duplicate Names Are Not Accepted”   END IF  LOOP UNTIL Found = 0    FNames(x) = FN NEXT x

PRINT PRINT PRINT “The Names In The Array: ”

FOR x = 1 TO 4  PRINT FNames(x) NEXT x

 

 

 

 

 

 

QBasic Boolean Techniques

December 1, 2011 2 comments

QBasicTutorial 20 Boolean Techniques

What this program will do will make an array of integers and have the user enter in

the numbers and a number to search for. If the computer finds the number then a value

will be loaded into a variable called found. That we can use to see if the computer

has found the variable or not.

First we declare the numbers array. There will be 5 elements as integer:

DIM Numbers(5) AS INTEGER

Next we declare the search number:

DIM SearchNum AS INTEGER

Severl for loops so make x as integer:

DIM x AS INTEGER

Declare Found as integer:

DIM Found AS INTEGER

Clear the screen

So far we have:

DIM Numbers(5) AS INTEGER DIM SearchNum AS INTEGER DIM x AS INTEGER DIM Found AS INTEGER

CLS

Start the for loop for x equals 1 to 5:

FOR x = 1 TO 5  INPUT “Enter Number: “, Numbers(x)  —-User Input, then place the input into

                                                the numbers array. NEXT x

CLS

INPUT “Search Number: “, SearchNum           —User input the search number,placing

                                                the input into SearchNum.

Print 2 spacers by:

PRINT PRINT

Set found to equal 0:

Found = 0

So now we have:

DIM Numbers(5) AS INTEGER DIM SearchNum AS INTEGER DIM x AS INTEGER DIM Found AS INTEGER

CLS

FOR x = 1 TO 5  INPUT “Enter Number: “, Numbers(x) NEXT x

CLS

INPUT “Search Number: “, SearchNum

PRINT PRINT

Found = 0

In Boolean Logic 0 is False and 1 is True, so setting found to 0 basically what it is

were doing is saying Found starts off as false.

So next we’ere going to search the array for the number.

FOR x = 1 TO 5  IF SearchNum = Numbers(x) THEN   Found = 1                    —-So we’ve set found to equal true to

                                                this point.

END IF

NEXT x     —Ending the FOR statement.

Now we will print the confirmation:      –If found = 1 then we know we have found

                                        the number.

IF Found = 1 THEN  PRINT “The Number IS In The Array” ELSE  PRINT “The Number Is Not In The Array” END IF

Ok so go ahead and run:

DIM Numbers(5) AS INTEGER DIM SearchNum AS INTEGER DIM x AS INTEGER DIM Found AS INTEGER

CLS

FOR x = 1 TO 5  INPUT “Enter Number: “, Numbers(x) NEXT x

CLS

INPUT “Search Number: “, SearchNum

PRINT PRINT

Found = 0

FOR x = 1 TO 5  IF SearchNum = Numbers(x) THEN   Found = 1  END IF NEXT x

IF Found = 1 THEN  PRINT “The Number is in the Array” ELSE  PRINT “The Number is not in the Array” END IF

Another teqnique would be to go to the top and replace DIM Found AS INTEGER with DIM Found AS STRING.

Change Found = 0 to Found = “FALSE”

Instead Found = 1 set to Found = “TRUE”   –Get both of these Found = 1

There is a downside to using this technique it is that if I was using lowercase for

the true and false statements when ran the program gives bad results. This means

uppercase is different than lowercase. Make sure if you use this technique you are

using the same case for the whole program.

The final technique

Go back and replace FOUND AS INTEGER at the top in the declare statements.

Then we will make 2 constants: One called false set to = 0, and true set to = 1

CONST False = 0 CONST True = 1

This follows your declare statements.

The first Found we will set to FOUND = False

The Found statement under SearchNum = Numbers(x) THEN:

Found = True

Right under this you have an IF statement, for this method replace the statement with

this:

IF Found = True THEN

So Found is initialized as Flase which also equals 0 so Found = 0.

Here at this statement:

Found = True

This means if we find the number then Found will = True which is also Found will = 1.

Then the final statement changed:

IF Found = True THEN

So for the final techniques the code would be:

DIM Numbers(5) AS INTEGER DIM SearchNum AS INTEGER DIM x AS INTEGER DIM Found AS INTEGER

CONST False = 0 CONST True = 1

CLS

FOR x = 1 TO 5  INPUT “Enter Number: “, Numbers(x) NEXT x

CLS

INPUT “Search Number: “, SearchNum

PRINT PRINT

Found = False

FOR x = 1 TO 5  IF SearchNum = Numbers(x) THEN   Found = True  END IF NEXT x

IF Found = True THEN  PRINT “The Number is in the Array” ELSE  PRINT “The Number is not in the Array” END IF

Go ahead and run and you will see the array works correctly as well as the program.

%d bloggers like this: