0% found this document useful (0 votes)
5 views

Basic programming civic nd1

Basic programming language

Uploaded by

lukasamuel041
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Basic programming civic nd1

Basic programming language

Uploaded by

lukasamuel041
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Basic programming civil engineering ND11

Computer programming using Q-Basic (ICT


202)

CONCEPT OF PROGRAMMING
A program is a set of instructions that tells the computer what to do. Computer
programming (often shortened to programming or coding), is the process of
writing, testing, debugging/troubleshooting and maintaining act of instructions
(source code) for solving a problem with the computer. A source code is written in
an acceptable computer programming language. The code may be a modification
of an existing source or something completely new.
The purpose of programming is to create a program that exhibits a certain
described behavior (customization). The process of writing source code requires
expertise in many different subjects, including knowledge of the application
domain. Alternatively, Programming is the craft of transforming requirements into
something that a computer can execute. Problem solving on computer is a task of
1
expressing the solution to the problem in terms of simple concepts, operations and
computer code (program) to obtain the results. To achieve this aim, you may
proceed as follows.
1. First, understand the problem clearly: - Decide what you want to be calculated
by the computer. What will be the input data required? (if any). This is the problem
formulation.
2. Write the steps of computation that are necessary to arrive at the solution. This is
setting up the algorithm.
3. Prepare a flowchart corresponding to the algorithm.
4. Develop the computer program. Test and run it on the computer.
There is an ongoing debate on the extent to which the writing of programs is an art,
a craft or an
engineering discipline. Good programming is generally considered to be the
measured application art, craft and engineering, with the goal of producing an
efficient and maintainable software (program) solution. The discipline differs from
many other technical professions in that programmers generally do not need to be
licensed or pass any standardized (or governmentally regulated) certification tests
in order to call themselves “programmers” or even “software engineers”.

Concept of Algorithm
An algorithm is a set of instructions to obtain the solution of a given problem or
this is broadly referring to the systematic steps or procedures used to accomplish a
task. It can also be defined as a sequence of instructions, which are followed in
order to complete a task. Computer needs precise and well-defined instructions for
finding solution of problems. If there is any ambiguity, the computer will not yield
the right results. It is essential that all the stages of solution of a given problem be
specified in details, correctly and clearly moreover, the steps must also be
organized rightly so that a unique solution is obtained.

A typical programming task can be divided into two phases:


(a) Problem solving phase
In this stage an ordered sequence of steps that describe solution of the problem is
produced.
Their sequence of steps can be called anti-Algorithm
(b) Implementation Phase
In this phase, the program is implemented in some programming languages.

2
Algorithm may be set up for any type of problems, mathematical/scientific or
business. Normally
algorithms for mathematical and scientific problems involve mathematical
formulas. Algorithms for business problems are generally descriptive and have
little use of formula.

FUNCTION OF ALGORITHM
1. An algorithm is used to gives sense of direction to the steps of solving
problem.
2. It provides the step-by-step solution to a problem
3. It is used to prove that a problem has a solution.

Features of an Algorithm
1. It should be simple
2. It should be clear with no ambiguity
3. It should head to unique solution of the problem
4. It should involve a finite number of steps to arrive at a solution
5. It should have the capability to handle unexpected situation.

Methods of Representing Algorithm


Algorithms are statements of steps involved in solving a particular problem. The
steps to the solutions are
broken into series of logical steps in English related form. Programs are written to
solve real life problems. There can’t be a solution if there is no recognized problem
and once a problem exist, one must take certain
step in order to get a desired solution. The following methods could be used to
represent an algorithm.
Methods of English like form
Methods of Flowchart
Methods of Pseudo code
Methods of Decision table
Methods of Data Flow Diagram (DFD)

ALGORITHM FOR PROBLEM SOLVING


Example1: find the average of three numbers.
Answer
 Enter any three numbers A, B, and C
 Add the three numbers: A+B+C

3
 Divide the sum by three.
 Write out the result.
Example2: find the average age of 30 students in a class.
Answer
 Obtain the age of each student
 Sum up the ages
 Divide total age by 30
 Record the average age.
Example3: X and Y are related by equation y=3x-7. calculate the value of Y, given
any value of X
Answer
 Obtain the value of x
 Multiple X by 3
 Subtract 7
 Write down the value of Y.

Example 4 Write an Algorithm to determine a student’s final grade and indicate


whether it is passing or failing
Answer
 The final grade is calculated as the average of four marks.
 Solution
 Input a set of 4 marks
 Calculate their average by summing and dividing by 4.
 If average is below 50
 Print “Fail”
 else
 Print “Pass”

FLOWCHART
Flowchart is a representation of the algorithm using standard symbols. It can also
be defined as a diagrammatical representation of essential of symbols and arrows
of flows lines. It is graphical representation of an algorithm. Each symbols have a
new function. The Algorithm steps determine which symbol to use to represent it
in the flow each step is linked to another step by using the directional arrows.

FLOW CHART SYMBOLS

oval: this is used to show the start/stop of the flowchart

4
Diamond: this is used for decision making.

Parallelogram: this is used to show input and output of


flowchart

Circle: this used as connector.

Rectangle: for processing or instruction of the flowchart.

Hexagon: for preparations and loops of the flowchart.

Arrow: gives direction of flow in the flowchart.

GUIDELINES FOR SOLVING PROBLEM USING FLOWCHART.


1. Flowchart should flow from top to bottom.
2. If the flowchart becomes complex, use the connecting block
3. Avoid intersecting flow lines
4. Use meaningful description in the symbol.

USES OF FLOWCHARTS
1 It gives us an opportunity to see the entire system as a whole.
2 It makes us to examine all possible logical outcomes in any process.
3 It provides a tool for communicating i.e., a flowchart helps to explain the system
to others.
4 To provide insight into alternative solutions.
5 It allows us to see what will happen if we change the values of the variable in the
system.

ADVANTAGES OF USING FLOWCHART


1. Communication: flowcharts are visual aids for communicating the logic of a
system to all
concerned.
2. Documentation: flowcharts are a means of documentation
3 The analyst/ programmers may leave the arrangement or they may forget the
logic of the program.
4 Changes to the procedure are more easily catered for (modification).
5 Flowchart can be understood by new staff coming to the company

5
6 Analysis: flowcharts help to clarify the logic of a system i.e.; the overall picture
of the organization can be seen.
7 Consistency: A flowchart is a consistent system of recording. It brightens at the
relationships between different parts of a system.

DISADVANTAGES OF USING FLOWCHART


1. Complex logic: - Where the logic of a problem is complex, the flowchart
quickly becomes clustered and lacks clarity.
2. Alterations: - If alterations are required the flowchart may require redrawing
completely.
3. Reproduction: - As the flowchart symbols cannot be typed, reproduction of
flowchart is often a problem.

Flowchart example
1. Draw a flowchart of the program to calculate the number of two integers

start

Read n1, n2

Sum=n1+n2

Print sum

Stop

INTRODUCTION TO QBASIC

BASIC stands for Beginner’s All Purpose Symbolic Instruction Code. It was
invented in 1963, at Dartmouth College, by the mathematicians John
George Kemeny and Tom Kurtzas.

6
BASIC is an interpreter which means it reads every line, translates it and
lets the computer execute it before reading another. Each instruction starts
with a line number.
FEATURES OF QBASIC

1. It is a user-friendly language.
2. It is widely known and accepted programming language.
3. It is one of the most flexible languages, as modification can easily be
done in already existing program.
4. Language is easy since the variables can be named easily and uses
simple English phrases with mathematical expressions.

RULES OF QBASIC
Every programming language has a set of rules that have to be followed
while writing a program, following are some rules of QBASIC language:
1. All QBasic programs are made up of series of statements, which are
executed in the order in which they are written.
2. Every statement should have at least one QBasic command word.
The words that BASIC recognizes are called keywords.
3. All the command words have to be written using some standard rules,
which are called “Syntax Rules”. Syntax is the grammar of writing the
statement in a language. Syntax Errors are generated when improper
syntax is detected.

QBASIC DATA

Data is a collection of facts and figures that is entered into the computer
through the keyboard. Data is of two types:

1. CONSTANT: Data whose value does not change or remains fixed.


There are two types of constants:
(a) NUMERIC CONSTANT: Numbers - negative or positive used
for mathematical calculations
e.g. –10, 20, 0
(b) ALPHANUMERIC CONSTANT / STRING: Numbers or
alphabets written within double quotes (inverted commas “ “ ).

7
e.g., “Computer”, “Operating System”

2. VARIABLE: Data whose value is not constant and may change due to
some calculation during the program execution. It is a location in the
computer’s memory, which stores the values. Depending on what value is
held, Variables are of two types:
(a) NUMERIC VARIABLE: The variable that holds a Numeric
Constant for arithmetic calculations (+, - ,*, / ) is called a
Numeric Variable. e.g. A = 50, here A is the Numeric Variable

(b) ALPHANUMERIC VARIABLE: The variable that holds an


Alphanumeric Constant, which cannot be used for arithmetic
calculations, is called Alphanumeric Variable or String Variable.
An Alphanumeric variable must end with a $ sign and the
Alphanumeric constant must be enclosed in inverted commas.
e.g. Name$ = “Akanksha”, here Name$ is an Alphanumeric
Variable

QBASIC Operator
Operators in QBasic are used to perform different mathematical,
logical and comparison operations. There are three types of
operators in QBasic:

1. Arithmetic Operators:
A computer performs many arithmetic operations and calculations
with the help of arithmetic operators.

2. Relational Operators
These operators are used to compare two values using any of the
given relational operators. This can be called a QBasic comparison
operator.

3.LOGICAL OPERATORS

8
Logical operators in QBasic are used to perform logical operations on
numeric values. QBasic Logical operators are used to combine two or
more relational expressions and return a single value as TRUE or FALSE
in a decision. The logical operators are: AND, OR, NOT.

TYPES OF MODE IN QBASIC


Once QBASIC program is loaded into the computer memory, it displays Ok
prompt. Ok means it ready to accept the commands. QBASIC can be made
to translate your instructions in two modes:

• Direct Mode
• Program Mode

1. Direct Mode: The accepts single line instructions from the user and
the output is viewed as soon as enter key is pressed. The instructions
are not stored in the memory. This mode can be used to do quick
calculation. They do not have line numbers. E.g. Print 3+4
Print “This is the Direct mode in QBasic”
2. Program Mode: The mode is used to type a program which is stored
in the memory. They have line numbers. We have to give the
command to get the output. e.g.
10 Print 3+4
20 End
RUN
Programs are built up with set of instructions or commands. Every
programming language has its own SYNTAX (rules) and COMMANDS.
COMMAND/KEYWORDS IN QBASIC AND THEIR FUNCTIONS:

The following commands do not need line number.

1. LIST - The command is used to list the program on the screen.


2. RUN - The command is used to execute the program.
3. LLIST - The command is used to list of programs as a hardcopy.
4. LPRINT- The command is used to get the output of the program on
the hard copy.
9
5. NEW - The command is used clear the memory of the existing
program.
6. SYSTEM – The command is used to take you back to dos prompt.
7. PRINT AND CLS command can also be used without a line number.
Print is used to view the display on the screen and CLS to clear the
screen.
8. RME - The command is used to show the position of the mistake.
9. SAVE -The keyword is used to save the program.
E.g. SAVE “PROGRAM1” QBasic will automatically add a period and
an extension “bas” to the filename.
10. LOAD - The keyword is used to LOAD the program from
the disk to the memory. E.g. LOAD”PROGRAM1”

QBASIC COMMANDS

1. CLS: This command is used to clear the screen.

2. PRINT: Print command is used to display the output on the screen.


E.g. Print “HELLO WORLD!!!”

Print 80 * 8

Print – Only Print command will leave blank space.

Print Tab(10) ”Navrachana” – will print Navrachana on 10


column.

2. REM: It stands for Remark. It gives an explanation of the program or


of the statements in the program thereby making the program more
understandable to the reader. The computer does not execute this
statement since whatever is written after REM is ignored by the
compiler. REM can be used anywhere and many times in a program.

3. LET: It assigns a value to a variable in a program. It stores a value in


the memory location.
SYNTAX: Let <Variable>=<Constant / Variable or Expression>

e.g. Let A = 15…….. Assigning constant to a variable


Let A = B………Assigning variable to a variable
10
Let C = A + B…. Assigning an expression to a variable
Using Let with Numeric Variables: Let A = 50, here A is a
Numeric Variable and ‘50’ is a Numeric Constant and value ‘50’
is assigned to A and stored in the computer’s memory for
further calculations.
Using Let with Alphanumeric Variable: Let N$ = “QBasic
Program”, here N$ is an Alphanumeric Variable and “QBasic
Program” is the Alphanumeric Constant assigned to N$.

NOTE: A numeric data should be assigned to a Numeric variable and an


alphanumeric data to an alphanumeric variable otherwise “TYPE
MISMATCH” error is displayed.

4. END: This command is usually given at the end of the program.


Statements written after end are not executed since the program
terminates execution on reading this command.

5. INPUT: This statement allows the user to enter a value for the
variable while running the program. A question mark (?) appears on
the output screen waiting for the user to enter a relevant data and
then press enter key. Once the Return key or Enter key is pressed
the data is stored in the variable. SYNTAX : INPUT < VARIABLE >

e.g. Input A……Enter Numeric constant

Input N$…..Enter Alphanumeric constant

Input “Enter name:”;N$….Giving relevant message to


avoid erroneous data
input

6. DELETE <LINE NO.>: To delete a line number in a program . e.g.


Delete 10 will delete line number 10

Delete 30-50 will delete all line numbers between 30 to 50.

11
Print with Semi-Colon (;) Semi-colon placed after the message to be
displayed, leaves no space between two messages.
e.g. Print “This is an example”;” of

QBasic program” output: This is an

example of QBasic program

Print with Comma( , ): The screen of the computer is made of 80 columns


and 40 rows. The columns are divided into five (5) zones of 14 columns
each. Comma placed after the message prints the message zone wise on
the screen.

QBASIC REMINDER

A QBASIC program consists of lines containing


1. A line number
2. A QBASIC keyword like PRINT,END etc
3. Each program line begins with positive number.
4. No two lines should have same number.

RUNNING A PROGRAM
RUN is the command used to execute the program and get the output on
the screen.

WRITING A NEW PROGRAM


It is possible to overwrite lines with the new statements, but if you want to
write a totally new program use a NEW command.

12
13

You might also like