Beaver Creek

March 16, 2012 Leave a comment

20120316-204204.jpg

Advertisements
Categories: The Cardinal

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.

What I’ve been working on for the past week ‘-‘

November 26, 2011 Leave a comment

QBasic Tutorial 7 If Statements and Syntax

Some beginning Syntax:

IF<Condition> THEN  Do Something

ELSEIF<Conditon>THEN  Do Something

Else  Do Something

END IF

The Relational Operators

< Less Than

<= Less Than Or Equal To

> Greater Than

>=Greater Than Or Equal To

= Equal To

<> Not Equal To

Starting with a simple if statement:

If 5 > 2 THEN  Print “5 IS Greater Than 2” End IF

5 Is Greater Than 2 Appears which that statement is true.

If we replace the > sign with a < then this statement is not true and nothing pops up in the preview box.

Ex. IF 5 > 10 THEN     PRINT “5 is greater than 10” ELSE     PRINT “5 is less than 10” END IF

Now we’re gonig to have the user enter in two integers and we will check to see which integer is larger than the other.

So we start:

DIM Num1 AS INTEGER DIM Num2 AS INTEGER

CLS

INPUT “Enter First Integer: “, Num1 INPUT “Enter Second Integer: “, Num2

IF Num1 > Num2 THEN     PRINT Num1; “Is Greater Than”; Num2 ELSE     PRINT Num2; “Is Greater Than”; Num1 END IF

Try entering the integers 10 and 10

You get a respose 10 is greater than 10 and that’s not true so let’s have a look at that.

Change this by placing an Else IF statement.

Ex.

DIM Num1 AS INTEGER DIM Num2 AS INTEGER

CLS

INPUT “Enter First Integer: “, Num1 INPUT “Enter Second Integer: “, Num2

IF Num1 > Num2 THEN     PRINT Num1; “Is Greater Than”; Num2 ELSE*if Num2 > Num1 then     PRINT Num2; “Is Greater Than”; Num1 ELSE      —There is only 3 possible                          situations here.  Print “The numbers are the same”                —Num1 is Greater than Num2,Num2 is                                                     Greater than Num1, or that the     End IF                                             numbers are the same.

So the finaly code:

DIM Num1 AS INTEGER DIM Num2 AS INTEGER

CLS

INPUT “Enter First Integer: “, Num1 INPUT “Enter Second Integer: “, Num2

IF Num1 > Num2 THEN     PRINT Num1; “Is Greater Than”; Num2 ELSEIF Num2 > Num1 THEN     PRINT Num2; “Is Greater Than”; Num1 ELSE     PRINT “The Numbers Are The Same” END IF

What I’ve been working on for the past week ‘-‘

November 26, 2011 Leave a comment

Qbasic Tutorial 6 -User Input-

Declare Varible Num2 as Integer

Program Asking the user to input their first and last name

So 2 variables one called FirstName and the second called LastName

Declare FirstName as a string as well with the LastName

Get the user input by placing the command INPUT then the actual prompt ex.”Enter First Name: “

Ex. Input “Enter First Name: “, FirstName     Input “Enter Last Name: “, LastName

Note-If you replace the commas here with ; it places a ? after Enter First Name

print the output “Nice to meet you, ” ; FirstName; ” ” ; LastName

Then we modify the program to ask the user to enter 2 numbers so you can add them together.

This is done by declaring Num1 as an Integer by:

DIM Num1 AS INTEGER

Doing the same for the second variable Num2

DIM Num2 AS INTEGER

Now we get the input from the user promting the user to enter the first integer.

Doing this by Input “Enter First Integer: “, Num1

Same with the second integer

Input “Enter Second Integer: “, Num2

Now we print the output as:

Print Num1; ” + “; Num2; ” = “; Num1 + Num2

Example: So is the user enters in 10 as the first number and 20 as the second variable. We would get 10 plus 20 = 10 plus 20 (the actual calculation)

What I’ve been working on for the past week ‘-‘

November 26, 2011 Leave a comment

QBasic 1.1

Variables and Data Types QB64 Tutorial 4

Variables:

-Hold data in memory

-Are assigned a data type

-The data can change throughout the program’s operation

-The data entered must be the same data type as assigned to the variable

Qbasic 1.1 Data Types

String……..Text and Characters

Integer…………..Non-Floating-Point

-32,768 to 32,767

Long…………..Non-Floating-Point

-2,147,483,648 to 2,147,483,647

Single…………Floating Point

-1.67×10^308 to 1.67×10^308

Note: QBasic 1.1 May have a problem with computations involving double data types

*Updated* Data types

_Integer64   &&

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

_Float       ##

-1.18×10^4932 to 1.18×10^4932

Info on all data types go to: http://www.qb64.net/wiki/index.php?title=Data_types

*Header$ in this example is a string

*Num1% in this example is an integer to = 5

*Num2% is an integer to = 6

*Num3% is a long integer = to 45000

*Num4% is also a long integer set to =  54000

*Num5! (Single) = 4.5

*Num6! = 6.75

*Num7# is a double data type set(Double Integer)?I believe? to = 56000.25

*Num8# is another double data type set to 89000.34

He then prints the header

Then prints some calculations

*This is a way to declare variables*

Another way to declare variables- You can use this in visual basic as well*

Xbox Fall 2011 PREVIEW PROGRAM

October 12, 2011 Leave a comment

Hurry this will only be up for a short while click here

%d bloggers like this: