Programming Intro and Qbasic
Programming Intro and Qbasic
Programming Intro and Qbasic
Programming languages differ from most other forms of human expression in that they require a
greater degree of precision and completeness. When using a natural language to communicate with
other people, human authors and speakers can be ambiguous and make small errors, and still expect
their intent to be understood. However computers
"do exactly what they are told to do", and cannot
"understand" what code the programmer intended to
write.
Machine languages are the only languages understood by computers. While easily understood by
computers, machine languages are almost impossible for humans to use because they consist entirely
of numbers (binary bits i.e. 0 or 1).
The disadvantages of Machine Level Language are:
The advantage of Machine Level Language is the speed of execution of the programs. As the
programs are written in binary form, there is no need of assemblers or compilers to convert the codes
to machine readable form so the execution is fast. This also leads to smaller file sizes.
i. Program written in assembly language are simpler than program written in machine
codes as use of binary codes to represent operational codes is replaced by words
(mnemonics).
ii. Program written for a family of microprocessors need not be rewritten i.e.
machine dependence is somewhat reduced.
iii. Less knowledge of CPU architecture compared to machine level language is required.
High level programming languages are those programming language which use normal everyday
word to represent the executable operational codes. These types of programming languages follow
strictly followed rule for writing these instruction. This rule is known as syntax. High level languages
are easy to learn as they avoid the need to understand the complex CPU architecture and also because
the commands are in plain understandable English form. These programs written in plain English
form following syntax are converted in machine understandable form using either compilers or
interpreters.
i. The additional process of compilation needs more machine time than the straight
assembly process.
ii. There is no control of hardware part while writing high level programs.
iii. The programs have to be compiled every time a change is made.
Compiler:
A compiler is a computer program (or set of programs) that transforms source code (program) written
in a computer language (the source language) into another computer language (the target language,
often having a binary form known as object code). The most common reason for wanting to transform
source code is to create an executable program. The name "compiler" is primarily used for programs
that translate source code from a high-level programming language to a machine level language.
Assembler:
An assembler is used to translate assembly language statements into the target computer's machine
code. The assembler performs a more or less a one-to-one mapping from mnemonic statements into
machine instructions and data.
Interpreter:
An interpreter is a computer program which reads source code written in a high-level programming
language, transforms the code to machine code, and executes the machine code. Using an interpreter,
a single source file can produce equal results even in vastly different systems. Using a compiler, a
single source file can produce equal results only if it is compiled to distinct, system-specific
executables. It converts a high level language into an intermediate code which will be immediately
executed. The main disadvantage of interpreters is that when a program is interpreted, it typically runs
more slowly than if it had been compiled. The difference in speeds could be tiny or great; often an
order of magnitude and sometimes more. It generally takes longer to run a program under an
interpreter than to run the compiled code but it can take less time to interpret it than the total time
required to compile and run it.
QBASIC
The full form of BASIC is Beginner's All-purpose Symbolic Instruction Code.
In 1985 Microsoft released a version of BASIC called QBasic with its MS-DOS 5.0 operating system.
It resembles BASIC in its statements, but is more structured -and- flexible at the same time. For
instance, line numbers are not required but can be used - as well as "line labels" with text names. It’s a
great "learner’s language”.
QBASIC certainly is the easiest programming language of all to learn. Also, when knowing this
language, other programming languages are very easy to learn.
They refer to different types of data that can be used in QBASIC. They are basically of five different
types namely, integer, long integer, single precision floating point number, double precision floating
point number and string.
Integer is the first and most important data type. This data type is the most used data type of all.
Since an integer contains 16 bits, you can only store values from –32768 up to 32767.
Long integer is the second. Long integers are not used that often, since mostly integers work
fine. Long integers are actually the same as normal integers, so only non-decimal numbers. However,
a long integer contains 32 bits, allowing you to store values from –2147483648 to 2147483647.
Single precision, which can contain decimal numbers. Single precision are 32 bit and can
store values from around 9 e40 to 9 e-39.
Double precision, which can also contain decimal numbers. The only difference is that
doubles are 64 bits, thus providing space for values from around 9e305 to 9e-310.
String, can contain characters like ‘A’ or ‘ƒ’. Strings are always notated with double
quotation marks around them. (“ ”).They can also include numbers and symbols as long as they are
included within the double quotation (“”) .The length of the character-list can range up to 64k.
STRING "hello, this is a string"
INTEGER 5 or -5
LONG 92883
SINGLE 39.293211
DOUBLE 983288.18111112
Data types are declared adding postfix. The postfixes are as follows:
Integer:%
Long Integer: &
Single Precision: ! or write nothing
Double Precision: #
String: $
(FOR SYNTAX OF DIFFERENT STATEMENTS REFER TO THE
CLASS NOTES)
About loops:
Loops are used to repeat a block of statement. They are repeated desired number of times or until a
condition becomes true or while a condition is satisfied.
FOR counter = start TO end [STEP factor] Runs commands between the For and
… Next while a counter is running. The
NEXT counter ‘loop’ will be exited if the counter
reaches the specified end-value. The
counter starts at start and ends at end,
and is increased every loop by factor.
Functions:
Functions statements used in QBASIC are DEF FN statement, FUNCTION END FUNCTION
statement and SUB END SUB statement.
A variable used only within the routine or function it is defined in is called local variable. When the
function is finished and control is returned back to the part of the program that called it, the local
variables no longer exist. If a variable can be used by all the modules and functions within the
program, such variables are referred to as global variables. The common practice is to keep global
variables to a minimum, because they can be manipulated by any subroutine in the program at any
time.
Array:
Arrays are defined as systematic arrangement of similar data types. Arrays are declared at the start of
the program so that the compiler at run time can assign the array declared number of memory spaces.
Arrays share the same variable name. In other words, they can be referred to as an ordered
arrangement of data elements. A vector is a one dimensional array; a matrix is a two-dimensional
array. Arrays are also referred to as subscripted variables. An array can also be defined as a
systematic arrangement of objects, usually in rows and columns. Arrays can be single dimensions as
in marks(45) or multi dimensional as in r(3,2) which is a two dimensional array, y(1,2,3) which is a
three dimensional array, record(1,2,3,4,.....,n) which is a n-dimensional array. If the array sixe is of
more than two dimension, such type of array is referred to as multi dimensional array.
Sorting Techniques:
An internal sort is any data sorting process that takes place entirely within the main memory
of a computer. This is possible whenever the data to be sorted is small enough to all be held in the
main memory. For sorting larger datasets, it may be necessary to hold only a chunk of data in memory
at a time, since it won't all fit. The rest of the data is normally held on some larger, but slower
medium, like a hard-disk. Any reading or writing of data to and from this slower media can slow the
sorting process considerably.
External sorting is a term for a class of sorting algorithms that can handle massive amounts
of data. External sorting is required when the data being sorted does not fit into the main memory of a
computing device (usually RAM) and a slower kind of memory (usually a hard drive) needs to be
used.
The difference between internal and external sorting is that internal sorting is done in internal
memory whereas external sorting is done in external memory like hard disk or magnetic tape.
Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to
be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.
The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
(The arrangement of inputted 10numbers in ascending order is an example of bubble sort.)
Insertion sort is a simple sorting algorithm that is relatively efficient for small lists and
mostly-sorted lists, and often is used as part of more sophisticated algorithms. It works by taking
elements from the list one by one and inserting them in their correct position into a new sorted list.
Shell sort was invented by Donald Shell in 1959. It improves upon bubble sort and insertion
sort by moving out of order elements more than one position at a time. One implementation can be
described as arranging the data sequence in a two-dimensional array and then sorting the columns of
the array using insertion sort.
Merge sort takes advantage of the ease of merging already sorted lists into a new sorted list.
It starts by comparing every two elements (i.e., 1 with 2, then 3 with 4...) and swapping them if the
first should come after the second. It then merges each of the resulting lists of two into lists of four,
then merges those lists of four, and so on; until at last two lists are merged into the final sorted list.