CMP 102 Introduction To Problem Solving Techniques2
CMP 102 Introduction To Problem Solving Techniques2
COURSE CONTENTS
Problem Solving Strategies; Role of Algorithm in Problem Solving Process; Implementation
Strategies, Concepts and Properties of Algorithm.
OBJECTIVES
The specific objectives of this course are to:
1. Understand the concept of Problem solving.
2. Understand steps involved in algorithm development.
3. Understand the concept of Algorithm.
4. Develop Algorithm for simple problem.
5. Understand the concept of Flowchart development.
6. Draw the symbols used in Flowcharts.
7. Understand Basic program for problem solving.
LEARNING OUTCOMES
The students should be able to:
1. Discuss concept of Problem solving.
2. Explain the steps involved in problem solving.
3. Explain steps involved in algorithm development.
4. Explain the concept of Flowchart development.
5. Know the Symbols used in Flow Charts.
6. Explain the concept of Pseudo Code.
7. Explain BASIC programming language and its importance.
8. Explain various data types in BASIC.
9. Use BASIC program as a working vehicle for problem solving.
10. Write short computer programs in BASIC
11. Debug a simple computer programs in BASIC
TEXT BOOK(S):
1. O’connell, S. (2007). Introduction to problem solving. Portsmouth: Heinemann.
2. Noggle, J. H. (1992). QuickBASIC programming for scientists and engineers. CRC
Press.
5. Write an algorithm which will test whether a given integer value is prime or not.
Algorithm prime testing:
Step 1: M ← 2
Step 2: read N
Step 3: MAX ← SQRT (N)
Step 4: While M < = MAX do
4.1 if (M* (N/M) = N
4.1.1 then
4.1.1.1 go to step 7
4.2. M ← M + 1
Step 5: Write “number is prime”
Step 6: go to step 8
Step 7: Write “number is not a prime”
Step 8: end.
7. Write an algorithm to find sum of given data values until negative value is entered.
Algorithm Find – Sum
Step 1: SUM ← 0
Step 2: I ← 0
Step 3: read NEW VALUE
Step 4: While NEW VALUE < = 0 do
4.1 SUM ← SUM + NEW VALUE
4.2 I ← I + 1
4.3 read NEW VALUE
Step 5: Write “Sum of”, I, “data value is, “SUM
Step 6: END
8. Write an algorithm to calculate the perimeter and area of rectangle. Given its length and
width.
Step 1: Read length of the rectangle.
Step 2: Read width of the rectangle.
Step 3: Calculate perimeter of the rectangle using the formula perimeter =2* (length + width)
Step 4: Calculate area of the rectangle using the formula area = length*width.
Step 5: Print perimeter.
Step 6: Print area.
Step 7: Stop.
Flowchart
A flow chart is a step by step diagrammatic representation of the logic paths to solve a given
problem. Or A flowchart is visual or graphical representation of an algorithm.
The flowcharts are pictorial representation of the methods to b used to solve a given problem
and help a great deal to analyze the problem and plan its solution in a systematic and orderly
manner. A flowchart when translated in to a proper computer language, results in a complete
program.
Advantages of Flowcharts
1. The flowchart shows the logic of a problem displayed in pictorial fashion which felicitates
easier checking of an algorithm.
2. The Flowchart is good means of communication to other users. It is also a compact means
of recording an algorithm solution to a problem.
3. The flowchart allows the problem solver to break the problem into parts. These parts can be
connected to make master chart.
4. The flowchart is a permanent record of the solution which can be consulted at a later time.
Differences between Algorithm and Flowchart
Algorithm Flowchart
1. A method of representing the step-by-step 1. Flowchart is diagrammatic representation
logical procedure for solving a problem of an algorithm. It is constructed using
different types of boxes and symbols.
2. It contains step-by-step English 2. The flowchart employs a series of blocks
descriptions, each step representing a and arrows, each of which represents a
particular operation leading to solution of particular step in an algorithm
problem
3. These are particularly useful for small 3. These are useful for detailed
problems representations of complicated programs
4. For complex programs, algorithms prove 4. For complex programs, Flowcharts prove
to be Inadequate to be adequate
b. Input and output indicators: Parallelograms are used to represent input and output
operations. Statements like INPUT, READ and PRINT are represented in these Parallelograms.
c. Process Indicators: Rectangle is used to indicate any set of processing operation such as for
storing arithmetic operations.
d. Decision Makers: The diamond is used for indicating the step of decision making and
therefore known as decision box. Decision boxes are used to test the conditions or ask questions
and depending upon the answers, the appropriate actions are taken by the computer. The
decision box symbol is
e. Flow Lines: Flow lines indicate the direction being followed in the flowchart. In a
Flowchart, every line must have an arrow on it to indicate the direction. The arrows may be in
any direction
f. On- Page connectors: Circles are used to join the different parts of a flowchart and these
circles are called on-page connectors. The uses of these connectors give a neat shape to the
flowcharts. Ina complicated problems, a flowchart may run in to several pages. The parts of
the flowchart on different pages are to be joined with each other. The parts to be joined are
indicated by the circle.
g. Off-page connectors: This connector represents a break in the path of flowchart which is
too large to fit on a single page. It is similar to on-page connector. The connector symbol marks
where the algorithm ends on the first page and where it continues on the second.
2. Draw a flowchart to find out the biggest of the three unequal positive numbers.
3. Draw a flowchart for adding the integers from 1 to 100 and to print the sum.
Pseudo Code
The Pseudo code is neither an algorithm nor a program. It is an abstract form of a program. It
consists of English like statements which perform the specific operations. It is defined for an
algorithm. It does not use any graphical representation. In pseudo code, the program is
represented in terms of words and phrases, but the syntax of program is not strictly followed.
Advantages:
Easy to read
Easy to understand
Easy to modify.
Example: Write a pseudo code to perform the basic arithmetic operations.
Read n1, n2
Sum = n1 + n2
Diff = n1 – n2
Mult = n1 * n2
Quot = n1/n2
Print sum, diff, mult, quot
End.
PROGRAMMING IN BASIC
The original BASIC was first developed at Dartmouth College by John Kemeny, Mary Keller,
and Thomas Kurtz and introduced on May 1, 1964. BASIC is short for Beginner’s All-purpose
Symbolic Instruction Code and is an easy-to-understand programming language that was
popular during 1970 – 1980. Today, BASIC is not used to develop programs, but is sometimes
used to help teach the fundamentals of programming.
The BASIC language was originally designed as a simplified version of FORTRAN for use in
teaching programming. From the simple beginnings, the language has grown to become a very
popular multi-purpose language available on a wide variety of machines.
There are many versions of the Basic language:
1. Ms-Basic
2. GW- Basic
3. QBasic-Quick Basic
4. Just BASIC (JBasic)
5. BASIC Plus
6. BASIC A
7. Turbo BASIC
BASIC KEYWORDS
1. Remark Statement (REM): This is a remark. It does not affect the program in terms
of instruction or command line. REM: It is used in explaining what the program is all
about.
Example,
10 REM. Program to calculate average of numbers.
This statement will appear as comment on the screen
2. Assignment Statement: This keyword is used in assigning values to variables, e.g.
READ, DATA, LET, INPUT.
READ statement: This is a statement used in giving values to variables. Usually
READ statement has DATA statement with it.
Example:
10 REM PROGRAM TO SUM NUMBERS
20 READ X
30 DATA 2
40 READ Y
50 DATA 3
60 READ Z
70 DATA 5
80 SUM=X+Y+Z
90 PRINT "The sum is:", SUM
LET statement: This statement is used in attaching strings of fixed characters and
numeric data in BASIC.
Examples,
10 REM PROGRAM TO SUM NUMBERS
20 READ X
30 DATA 2
40 READ Y
50 DATA 3
60 READ Z
70 DATA 5
80 LET SUM=X+Y+Z
90 PRINT "The sum is:", SUM
INPUT/ PRINT statements: INPUT statement is used assigning values to variables.
PRINT statement is used displaying values to the screen. The PRINT statement is
usually followed by a list of items to be displayed and each item is separated by a
comma (,) or semi-colon (;).
Example 1:
10 REM PROGRAM TO SUM NUMBERS
20 input "Enter first number:"; X
30 INPUT "Enter second number:"; Y
40 INPUT "Enter third number:"; Z
50 PRINT X, Y, Z
60 PRINT "X=",X,"Y=",Y,"Z=",Z
70 LET SUM=X+Y+Z
80 PRINT "The sum is:", SUM
Example 2:
PRINT 512 + 478
Program output:
990
If you enclose the expression with quotation marks, the expression becomes a string and
isn't evaluated. For example:
PRINT "512 + 478"
Output:
512 + 478
You can use multiple print statements in your program.
PRINT "Hello"
PRINT "World"
Output:
Hello
World
To place World onto the previous line, place a semi-colon after PRINT "Hello".
PRINT "Hello";
PRINT "World"
Output:
HelloWorld
Also, if you put a comma instead of a semi-colon on the first line, the program will insert
spaces between the two words.
PRINT "Hello",
PRINT "World"
Output:
Hello World
Program Terminator: These keywords are used to terminate the program, e.g. STOP
and END.
a) STOP statement: This statement is used to terminate a program.
b) END statement: This statement is used to terminate a program.
Example:
10 REM PROGRAM TO SUM NUMBERS
20 input "Enter first number:"; X
30 INPUT "Enter second number:"; Y
40 INPUT "Enter third number:"; Z
50 PRINT X, Y, Z
60 PRINT "X=",X,"Y=",Y,"Z=",Z
70 LET SUM=X+Y+Z
80 PRINT "The sum is:", SUM
90 STOP
Assignments
1. Write a BASIC program to calculate the product of three numbers
2. Write a program to calculate the area of a circle. (Area= πr2)
3. Write a program to calculate simple interest
4. Write a BASIC program to calculate the sum of two numbers
5. Write a BASIC program to find the average of 3 Numbers
6. Write a BASIC program to calculate the area of a triangle with base
7. Write a program to find the product of four numbers using the ‘LET’ statement
8. Write a program to calculate the average of five numbers using the ‘INPUT’ statement
VARIABLES
A variable is a piece of data kept in the computer's memory (RAM). The location of a variable
in RAM is called the "address."
The following program prints the variable X to the screen:
print X
Since the variable hasn't been assigned a number, the value of the variable is 0. So, the output
of the program is:
0
This next program sets X to 15, and then prints the variable:
X = 15
print X
This time, the output is:
15
Also, you can use multiple variables in your program.
X = 82
Y = 101
Z = 79
PRINT X
PRINT Y
PRINT Z
Output:
82
101
79
EXPRESSIONS
If you pass an expression to a variable, the expression is evaluated and the variable is set to
that value.
x = 500 + (10 * 7)
PRINT x
Output:
570
STRINGS
If you add a dollar sign ($) to the end of a variable, the variable is a string.
X$ = "Hello World!"
PRINT X$
Output:
Hello World!
PRINT text$
PRINT num
CONTROL STRUCTURE
Control structure/statements are used in program to direct the order of execution of the
program. Normally, statements in a program are executed one after the other in the order in
which they’re written. This process is called sequential execution. Various Basic statements,
enable you to specify that the next statement to execute is not necessarily the next one in
sequence. This is called transfer of control. Structured programs are clearer, easier to debug
and modify, and more likely to be bug free in the first place.
Programs could be written in terms of only three control statements:
the selection statement
the Jump statement
the repetition statement.
SELECTION STATEMENT
The selection structure are known as decision making structures. It require that the programmer
specify one or more conditions to be evaluated or tested by the program, along with a statement
or statements to be executed if the condition is determined to be true, and optionally, other
statements to be executed if the condition is determined to be false.
There are three basic types of selection statements:
i. If-then (Single-Selection) Statement
The if statement either performs (selects) an action, if a condition is true, or skips it, if the
condition is false. The if-then statement is the simplest form of control statement, frequently
used in decision making and changing the control flow of the program execution.
Example:
x=5
IF x = 5 THEN PRINT "x equals 5"
Since X does equal 5 in this case, the program outputs:
x equals 5
Expression signs
You can also enter the following statements, instead of the equals sign:
x < 5 (x is less than 5)
x > 5 (x is greater than 5)
Run the following:
x = 16
IF (x > 5) THEN PRINT "x is greater than 5"
Output:
x is greater than 5
You can also combine the signs like this:
x <= 5 (x is less than or equal to 5)
x >= 5 (x is greater than or equal to 5)
x <> 5 (x does not equal 5)
Run the following example:
CLS
x=5
IF (x >= 5) THEN PRINT "x is greater than or equal to 5"
IF (x <= 5) THEN PRINT "x is less than or equal to 5"
IF (x <> 5) THEN PRINT "x does not equal 5"
Output:
x is greater than or equal to 5
x is less than or equal to 5
ii. if-then-else (Double Selection) Statement
The if-then-else statement performs an action if a condition is true and performs a different
action if the condition is false. The if-then single-selection statement performs an indicated
action only when the condition is true; otherwise, the action is skipped. The if-then-else double-
selection statement allows you to specify an action to perform when the condition is true and a
different action when the condition is false.
Using the ELSE command, you can have the program perform a different action if the statement
is false.
x=3
IF x = 5 THEN PRINT "Yes" ELSE PRINT "No"
Output
No
END IF
END IF allows you to have multiple commands after the IF...THEN statement, but they must
start on the line after the IF statement. END IF should appear right after the list of commands.
x=5
IF (x = 5) THEN
INPUT a$
PRINT a$
END IF
Output:
32
Example 1:
x=6
IF (x = 5) THEN
PRINT "Statement 1 is true"
ELSEIF (x = 6) THEN
PRINT "Statement 2 is true"
END IF
Output:
Statement 2 is true
Example 2:
You can have multiple ELSEIF commands, along with ELSE.
x=8
IF (x = 5) THEN
PRINT "Statement 1 is true"
ELSEIF (x = 6) THEN
PRINT "Statement 2 is true"
ELSEIF (x = 7) THEN
PRINT "Statement 3 is true"
ELSE
PRINT "No above statements are true"
END IF
Output:
No above statements are true
Strings in IF...THEN
So far in this chapter, we've only been dealing with numbers, but you can also use strings with
the IF...THEN command.
x$ = "Hello"
IF (x$ = "Hello" OR x$ = "World") THEN PRINT x$
Output:
Hello
Output:
Hello World
JUMP STATEMENT
The GOTO and GOSUB commands enables you to jump to certain positions in your program.
Labels are used to specify what point in the program to continue execution.
GOTO
To use GOTO, place a label somewhere in your program, and then enter.
GOTO <label>
Example
Run the following example program:
PRINT "1"
GOTO TheLabel
PRINT "2"
TheLabel:
PRINT "3"
GOSUB
The GOSUB command is the same as GOTO, except when it encounters a RETURN statement,
the program "returns" back to the GOSUB command. In other words, RETURN continues
program execution immediately after the previous GOSUB statement.
PRINT "1"
GOSUB TheLabel
PRINT "2"
END
TheLabel:
PRINT "3"
RETURN
PRINT "1"
GOTO 10
PRINT "2"
10 PRINT "3" (Notice the line number)
GUESSING GAME
The following is a simple guessing game:
CLS
start:
PRINT "Guess a number between 1 and 10: ";
INPUT num
IF (num < 1 OR num > 10) THEN
PRINT "That is not between 1 and 10"
GOTO start
END IF
IF (num = 6) THEN
PRINT "Correct!!!"
ELSE
PRINT "Try again"
PRINT
GOTO start
END IF
Output:
10
11
12
13
14
WHILE...WEND
The WHILE...WEND commands continue a loop until a specified expression is false.
To use WHILE...WEND:
1. Place an expression after WHILE
2. Enter a list of commands
3. Place WEND at the end
Example:
x = 10
WHILE x < 15
PRINT x
x=x+1
WEND
Output (same as in previous example):
10
11
12
13
14
DO...LOOP
DO...LOOP is exactly the same as WHILE...WEND, except it has at least two slight
advantages.
With DO...LOOP you can:
1. Loop until an expression is true
2. Loop at least one time regardless of whether the expression is true or not.
To use DO...LOOP:
1. Specify whether the loop continues "while" the expression is true or "until" the
expression is true, using the WHILE and UNTIL statements, respectively.
2. Place an expression after WHILE/UNTIL
3. Enter a list of commands
4. Place LOOP at the end
The following uses the WHILE statement:
x = 10
DO WHILE x < 15
PRINT x
x=x+1
LOOP
If you place the expression at the end of the loop instead, the program goes through the loop at
least once.
x = 32
DO
PRINT x
x=x+1
LOOP WHILE x < 5
This is the output because the loop was only gone through one time:
32
FOR...NEXT
FOR...NEXT provides an easier way to create a loop.
Example:
FOR x = 1 TO 5
PRINT x
NEXT x
Output:
1
2
3
4
5
Also, you can use the STEP attribute to specify how much X will be increased each time
through the loop.
FOR x = 1 TO 5 STEP 2
PRINT x
NEXT x
Output:
1
3
5
STOPPING LOOPS
To stop a loop prematurely, use the EXIT command, followed by either FOR or DO.
Example:
FOR x = 1 TO 5
PRINT x
IF (x = 3) THEN EXIT FOR
NEXT x
Output:
1
2
3
(NOTE: This command only works with the DO...LOOP and FOR...NEXT commands, not
with WHILE...WEND or IF...GOTO.)
MATHEMATICS FUNCTIONS
Basic provides several functions to do mathematical calculations. A few of them are discussed
here.
SQR
Use SQR to find the "square root" of a number.
PRINT SQR(1)
PRINT SQR(4)
PRINT SQR(9)
PRINT SQR(16)
PRINT SQR(25)
Output:
1
2
3
4
5
ABS
ABS returns the absolute value of a number. In other words, ABS converts a negative number
to a positive number (if you pass a positive number, ABS does nothing).
PRINT ABS(12)
PRINT ABS(-12)
Output:
12
12
Output:
.7071067
.8660254
6137956
1.570796 (Same as PI / 2)
ARRAYS
An array is a list of variables of the same type. Arrays are useful for organizing multiple
variables. To create an array, use the DIM (dimension) command.
The following example does not use arrays:
a=2
b=4
c=6
d=8
e = 10
PRINT a, b, c, d, e
Output:
2 4 6 8 10
Output:
2 4 6 8 10
STRINGS
You can also create an array of string variables.
DIM vars$(5)
vars$(1) = "Two"
vars$(2) = "Four"
vars$(3) = "Six"
vars$(4) = "Eight"
vars$(5) = "Ten"
PRINT vars$(1), vars$(2), vars$(3), vars$(4), vars$(5)
Output:
Two Four Six Eight Ten
SUB GetText
PRINT "Enter some text:";
INPUT text$
PRINT "The text you entered was: "; text$
END SUB
SUB GetText
PRINT "Enter some text:";
INPUT text$
PRINT "The text you entered was: "; text$
END SUB
FUNCTIONS
A function is the same as a subroutine, except it returns a value. Also, you must leave out the
CALL command.
To return a value, set a variable with the same name as the function.
PRINT Add(10, 7)
Output:
17
Since a function can return a value, the name of the function can end with special characters
(see Variable types, Using special characters).
' Notice the dollar sign ($) after "Add." It means
' the function returns a string.
Output:
HelloWorld